]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef BOOST_SERIALIZATION_ARRAY_HPP |
2 | #define BOOST_SERIALIZATION_ARRAY_HPP | |
3 | ||
4 | // (C) Copyright 2005 Matthias Troyer and Dave Abrahams | |
5 | // Use, modification and distribution is subject to the Boost Software | |
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | //#include <iostream> | |
10 | ||
11 | #include <boost/config.hpp> // msvc 6.0 needs this for warning suppression | |
12 | ||
13 | #if defined(BOOST_NO_STDC_NAMESPACE) | |
14 | namespace std{ | |
15 | using ::size_t; | |
16 | } // namespace std | |
17 | #endif | |
18 | ||
19 | #include <boost/serialization/nvp.hpp> | |
20 | #include <boost/serialization/split_member.hpp> | |
21 | #include <boost/serialization/wrapper.hpp> | |
22 | #include <boost/serialization/collection_size_type.hpp> | |
23 | #include <boost/mpl/always.hpp> | |
24 | #include <boost/mpl/apply.hpp> | |
25 | #include <boost/mpl/bool_fwd.hpp> | |
26 | #include <boost/type_traits/remove_const.hpp> | |
27 | #include <boost/type_traits/is_integral.hpp> | |
28 | #include <boost/static_assert.hpp> | |
29 | ||
30 | namespace boost { namespace serialization { | |
31 | ||
32 | // traits to specify whether to use an optimized array serialization | |
33 | ||
34 | template <class Archive> | |
35 | struct use_array_optimization : boost::mpl::always<boost::mpl::false_> {}; | |
36 | ||
37 | template<class T> | |
38 | class array_wrapper : | |
39 | public wrapper_traits<const array_wrapper< T > > | |
40 | { | |
41 | private: | |
42 | array_wrapper & operator=(const array_wrapper & rhs); | |
43 | public: | |
44 | // note: I would like to make the copy constructor private but this breaks | |
45 | // make_array. So I try to make make_array a friend - but that doesn't | |
46 | // build. Need a C++ guru to explain this! | |
47 | template<class S> | |
48 | friend const boost::serialization::array_wrapper<T> make_array( T* t, S s); | |
49 | ||
50 | array_wrapper(const array_wrapper & rhs) : | |
51 | m_t(rhs.m_t), | |
52 | m_element_count(rhs.m_element_count) | |
53 | {} | |
54 | public: | |
55 | array_wrapper(T * t, std::size_t s) : | |
56 | m_t(t), | |
57 | m_element_count(s) | |
58 | {} | |
59 | ||
60 | // default implementation | |
61 | template<class Archive> | |
62 | void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const | |
63 | { | |
64 | // default implemention does the loop | |
65 | std::size_t c = count(); | |
66 | T * t = address(); | |
67 | while(0 < c--) | |
68 | ar & boost::serialization::make_nvp("item", *t++); | |
69 | } | |
70 | ||
71 | // optimized implementation | |
72 | template<class Archive> | |
73 | void serialize_optimized(Archive &ar, const unsigned int version, mpl::true_ ) | |
74 | { | |
75 | boost::serialization::split_member(ar, *this, version); | |
76 | } | |
77 | ||
78 | // default implementation | |
79 | template<class Archive> | |
80 | void save(Archive &ar, const unsigned int version) const | |
81 | { | |
82 | ar.save_array(*this,version); | |
83 | } | |
84 | ||
85 | // default implementation | |
86 | template<class Archive> | |
87 | void load(Archive &ar, const unsigned int version) | |
88 | { | |
89 | ar.load_array(*this,version); | |
90 | } | |
91 | ||
92 | // default implementation | |
93 | template<class Archive> | |
94 | void serialize(Archive &ar, const unsigned int version) | |
95 | { | |
96 | typedef typename | |
97 | boost::serialization::use_array_optimization<Archive>::template apply< | |
98 | typename remove_const< T >::type | |
99 | >::type use_optimized; | |
100 | serialize_optimized(ar,version,use_optimized()); | |
101 | } | |
102 | ||
103 | T * address() const | |
104 | { | |
105 | return m_t; | |
106 | } | |
107 | ||
108 | std::size_t count() const | |
109 | { | |
110 | return m_element_count; | |
111 | } | |
112 | ||
113 | private: | |
114 | T * const m_t; | |
115 | const std::size_t m_element_count; | |
116 | }; | |
117 | ||
118 | template<class T, class S> | |
119 | inline | |
120 | const array_wrapper< T > make_array( T* t, S s){ | |
121 | const array_wrapper< T > a(t, s); | |
122 | return a; | |
123 | } | |
124 | ||
125 | } } // end namespace boost::serialization | |
126 | ||
127 | // I can't figure out why BOOST_NO_CXX11_HDR_ARRAY | |
128 | // has been set for clang-11. So just make sure | |
129 | // it's reset now. Needs further research!!! | |
130 | ||
131 | #if defined(_LIBCPP_VERSION) | |
132 | #undef BOOST_NO_CXX11_HDR_ARRAY | |
133 | #endif | |
134 | ||
135 | #ifndef BOOST_NO_CXX11_HDR_ARRAY | |
136 | #include <array> | |
137 | namespace boost { namespace serialization { | |
138 | // implement serialization for std::array | |
139 | template <class Archive, class T, std::size_t N> | |
140 | void serialize(Archive& ar, std::array<T,N>& a, const unsigned int /* version */) | |
141 | { | |
142 | ar & boost::serialization::make_nvp( | |
143 | "elems", | |
144 | *static_cast<T (*)[N]>(static_cast<void *>(a.data())) | |
145 | ); | |
146 | ||
147 | } | |
148 | } } // end namespace boost::serialization | |
149 | #endif | |
150 | ||
151 | #include <boost/array.hpp> | |
152 | ||
153 | namespace boost { namespace serialization { | |
154 | // implement serialization for boost::array | |
155 | template <class Archive, class T, std::size_t N> | |
156 | void serialize(Archive& ar, boost::array<T,N>& a, const unsigned int /* version */) | |
157 | { | |
158 | ar & boost::serialization::make_nvp("elems", a.elems); | |
159 | } | |
160 | ||
161 | } } // end namespace boost::serialization | |
162 | ||
163 | #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive) \ | |
164 | namespace boost { namespace serialization { \ | |
165 | template <> struct use_array_optimization<Archive> { \ | |
166 | template <class ValueType> \ | |
167 | struct apply : boost::mpl::apply1<Archive::use_array_optimization \ | |
168 | , typename boost::remove_const<ValueType>::type \ | |
169 | >::type {}; \ | |
170 | }; }} | |
171 | ||
172 | #endif //BOOST_SERIALIZATION_ARRAY_HPP |