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 <boost/config.hpp> // msvc 6.0 needs this for warning suppression |
10 | |
11 | #include <iostream> |
12 | #include <cstddef> // std::size_t |
13 | #ifndef BOOST_NO_CXX11_HDR_ARRAY |
14 | #include <array> |
15 | #endif |
16 | |
17 | #if defined(BOOST_NO_STDC_NAMESPACE) |
18 | namespace std{ |
19 | using ::size_t; |
20 | } // namespace std |
21 | #endif |
22 | |
23 | #include <boost/serialization/nvp.hpp> |
24 | #include <boost/serialization/split_member.hpp> |
25 | #include <boost/serialization/wrapper.hpp> |
26 | #include <boost/mpl/always.hpp> |
27 | #include <boost/mpl/apply.hpp> |
28 | #include <boost/mpl/bool.hpp> |
29 | #include <boost/type_traits/remove_const.hpp> |
30 | #include <boost/array.hpp> |
31 | |
32 | namespace boost { namespace serialization { |
33 | |
34 | // traits to specify whether to use an optimized array serialization |
35 | |
36 | template <class Archive> |
37 | struct use_array_optimization : boost::mpl::always<boost::mpl::false_> {}; |
38 | |
39 | template<class T> |
40 | class array : |
41 | public wrapper_traits<const array< T > > |
42 | { |
43 | public: |
44 | typedef T value_type; |
45 | |
46 | array(value_type* t, std::size_t s) : |
47 | m_t(t), |
48 | m_element_count(s) |
49 | {} |
50 | array(const array & rhs) : |
51 | m_t(rhs.m_t), |
52 | m_element_count(rhs.m_element_count) |
53 | {} |
54 | array & operator=(const array & rhs){ |
55 | m_t = rhs.m_t; |
56 | m_element_count = rhs.m_element_count; |
57 | } |
58 | |
59 | // default implementation |
60 | template<class Archive> |
61 | void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const |
62 | { |
63 | // default implemention does the loop |
64 | std::size_t c = count(); |
65 | value_type * t = address(); |
66 | while(0 < c--) |
67 | ar & boost::serialization::make_nvp("item" , *t++); |
68 | } |
69 | |
70 | // optimized implementation |
71 | template<class Archive> |
72 | void serialize_optimized(Archive &ar, const unsigned int version, mpl::true_ ) |
73 | { |
74 | boost::serialization::split_member(ar, *this, version); |
75 | } |
76 | |
77 | // default implementation |
78 | template<class Archive> |
79 | void save(Archive &ar, const unsigned int version) const |
80 | { |
81 | ar.save_array(*this,version); |
82 | } |
83 | |
84 | // default implementation |
85 | template<class Archive> |
86 | void load(Archive &ar, const unsigned int version) |
87 | { |
88 | ar.load_array(*this,version); |
89 | } |
90 | |
91 | // default implementation |
92 | template<class Archive> |
93 | void serialize(Archive &ar, const unsigned int version) |
94 | { |
95 | typedef typename |
96 | boost::serialization::use_array_optimization<Archive>::template apply< |
97 | typename remove_const< T >::type |
98 | >::type use_optimized; |
99 | serialize_optimized(ar,version,use_optimized()); |
100 | } |
101 | |
102 | value_type* address() const |
103 | { |
104 | return m_t; |
105 | } |
106 | |
107 | std::size_t count() const |
108 | { |
109 | return m_element_count; |
110 | } |
111 | |
112 | private: |
113 | value_type* m_t; |
114 | std::size_t m_element_count; |
115 | }; |
116 | |
117 | template<class T> |
118 | inline |
119 | const array< T > make_array( T* t, std::size_t s){ |
120 | return array< T >(t, s); |
121 | } |
122 | |
123 | // implement serialization for boost::array |
124 | template <class Archive, class T, std::size_t N> |
125 | void serialize(Archive& ar, boost::array<T,N>& a, const unsigned int /* version */) |
126 | { |
127 | ar & boost::serialization::make_nvp("elems" , a.elems); |
128 | } |
129 | |
130 | #ifndef BOOST_NO_CXX11_HDR_ARRAY |
131 | // implement serialization for std::array |
132 | template <class Archive, class T, std::size_t N> |
133 | void serialize(Archive& ar, std::array<T,N>& a, const unsigned int /* version */) |
134 | { |
135 | ar & boost::serialization::make_nvp( |
136 | "elems" , |
137 | *static_cast<T (*)[N]>(static_cast<void *>(a.data())) |
138 | ); |
139 | |
140 | } |
141 | #endif |
142 | |
143 | } } // end namespace boost::serialization |
144 | |
145 | #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive) \ |
146 | namespace boost { namespace serialization { \ |
147 | template <> struct use_array_optimization<Archive> { \ |
148 | template <class ValueType> \ |
149 | struct apply : boost::mpl::apply1<Archive::use_array_optimization \ |
150 | , typename boost::remove_const<ValueType>::type \ |
151 | >::type {}; \ |
152 | }; }} |
153 | |
154 | #endif //BOOST_SERIALIZATION_ARRAY_HPP |
155 | |