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