Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / serialization / array_wrapper.hpp
1 #ifndef BOOST_SERIALIZATION_ARRAY_WRAPPER_HPP
2 #define BOOST_SERIALIZATION_ARRAY_WRAPPER_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/serialization/array_optimization.hpp>
24 #include <boost/mpl/always.hpp>
25 #include <boost/mpl/apply.hpp>
26 #include <boost/mpl/bool_fwd.hpp>
27 #include <boost/type_traits/remove_const.hpp>
28
29 namespace boost { namespace serialization {
30
31 template<class T>
32 class array_wrapper :
33     public wrapper_traits<const array_wrapper< T > >
34 {
35 private:
36     array_wrapper & operator=(const array_wrapper & rhs);
37     // note: I would like to make the copy constructor private but this breaks
38     // make_array.  So I make make_array a friend
39     template<class Tx, class S>
40     friend const boost::serialization::array_wrapper<Tx> make_array(Tx * t, S s);
41 public:
42
43     array_wrapper(const array_wrapper & rhs) :
44         m_t(rhs.m_t),
45         m_element_count(rhs.m_element_count)
46     {}
47 public:
48     array_wrapper(T * t, std::size_t s) :
49         m_t(t),
50         m_element_count(s)
51     {}
52
53     // default implementation
54     template<class Archive>
55     void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const
56     {
57       // default implemention does the loop
58       std::size_t c = count();
59       T * t = address();
60       while(0 < c--)
61             ar & boost::serialization::make_nvp("item", *t++);
62     }
63
64     // optimized implementation
65     template<class Archive>
66     void serialize_optimized(Archive &ar, const unsigned int version, mpl::true_ )
67     {
68       boost::serialization::split_member(ar, *this, version);
69     }
70
71     // default implementation
72     template<class Archive>
73     void save(Archive &ar, const unsigned int version) const
74     {
75       ar.save_array(*this,version);
76     }
77
78     // default implementation
79     template<class Archive>
80     void load(Archive &ar, const unsigned int version)
81     {
82       ar.load_array(*this,version);
83     }
84     
85     // default implementation
86     template<class Archive>
87     void serialize(Archive &ar, const unsigned int version)
88     {
89       typedef typename 
90           boost::serialization::use_array_optimization<Archive>::template apply<
91                     typename remove_const< T >::type 
92                 >::type use_optimized;
93       serialize_optimized(ar,version,use_optimized());
94     }
95     
96     T * address() const
97     {
98       return m_t;
99     }
100
101     std::size_t count() const
102     {
103       return m_element_count;
104     }
105
106 private:
107     T * const m_t;
108     const std::size_t m_element_count;
109 };
110
111 template<class T, class S>
112 inline
113 const array_wrapper< T > make_array(T* t, S s){
114     const array_wrapper< T > a(t, s);
115     return a;
116 }
117
118 } } // end namespace boost::serialization
119
120
121 #endif //BOOST_SERIALIZATION_ARRAY_WRAPPER_HPP