fix doc example typo
[boost.git] / boost / serialization / array.hpp
blobb9dc32dad53a7621107d1dcf5b71de6a2163d7d4
1 #ifndef BOOST_SERIALIZATION_ARRAY_HPP
2 #define BOOST_SERIALIZATION_ARRAY_HPP
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)
9 #include <boost/serialization/nvp.hpp>
10 #include <boost/serialization/split_member.hpp>
11 #include <boost/serialization/wrapper.hpp>
12 #include <boost/mpl/always.hpp>
13 #include <boost/mpl/apply.hpp>
14 #include <boost/mpl/bool.hpp>
15 #include <boost/type_traits/remove_const.hpp>
16 #include <boost/array.hpp>
17 #include <iostream>
19 #include <cstddef> // std::size_t
20 #include <cstddef>
21 #include <boost/config.hpp> // msvc 6.0 needs this for warning suppression
22 #if defined(BOOST_NO_STDC_NAMESPACE)
23 namespace std{
24 using ::size_t;
25 } // namespace std
26 #endif
28 namespace boost { namespace serialization {
30 // traits to specify whether to use an optimized array serialization
32 #ifdef __BORLANDC__
33 // workaround for Borland compiler
34 template <class Archive>
35 struct use_array_optimization {
36 template <class T> struct apply : boost::mpl::false_ {};
39 #else
40 template <class Archive>
41 struct use_array_optimization : boost::mpl::always<boost::mpl::false_> {};
42 #endif
44 template<class T>
45 class array
46 : public wrapper_traits<array<T> >
48 public:
49 typedef T value_type;
51 array(value_type* t, std::size_t s) :
52 m_t(t),
53 m_element_count(s)
56 // default implementation
57 template<class Archive>
58 void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const
60 // default implemention does the loop
61 std::size_t c = count();
62 value_type * t = address();
63 while(0 < c--)
64 ar & boost::serialization::make_nvp("item", *t++);
67 // optimized implementation
68 template<class Archive>
69 void serialize_optimized(Archive &ar, const unsigned int version, mpl::true_ )
71 boost::serialization::split_member(ar, *this, version);
74 // default implementation
75 template<class Archive>
76 void save(Archive &ar, const unsigned int version) const
78 ar.save_array(*this,version);
81 // default implementation
82 template<class Archive>
83 void load(Archive &ar, const unsigned int version)
85 ar.load_array(*this,version);
88 // default implementation
89 template<class Archive>
90 void serialize(Archive &ar, const unsigned int version)
92 typedef BOOST_DEDUCED_TYPENAME
93 boost::serialization::use_array_optimization<Archive>::template apply<
94 BOOST_DEDUCED_TYPENAME remove_const<T>::type
95 >::type use_optimized;
96 serialize_optimized(ar,version,use_optimized());
99 value_type* address() const
101 return m_t;
104 std::size_t count() const
106 return m_element_count;
109 private:
110 value_type* m_t;
111 std::size_t const m_element_count;
114 template<class T>
115 inline
116 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
117 const
118 #endif
119 array<T> make_array( T* t, std::size_t s){
120 return array<T>(t, s);
123 template <class Archive, class T, std::size_t N>
124 void serialize(Archive& ar, boost::array<T,N>& a, const unsigned int /* version */)
126 ar & boost::serialization::make_nvp("elems",a.elems);
131 } } // end namespace boost::serialization
133 #ifdef __BORLANDC__
134 // ignore optimizations for Borland
135 #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive)
136 #else
137 #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive) \
138 namespace boost { namespace serialization { \
139 template <> struct use_array_optimization<Archive> { \
140 template <class ValueType> \
141 struct apply : boost::mpl::apply1<Archive::use_array_optimization \
142 , BOOST_DEDUCED_TYPENAME boost::remove_const<ValueType>::type \
143 >::type {}; \
144 }; }}
145 #endif // __BORLANDC__
147 #endif //BOOST_SERIALIZATION_ARRAY_HPP