array_wrapper.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef BOOST_SERIALIZATION_ARRAY_WRAPPER_HPP
  2. #define BOOST_SERIALIZATION_ARRAY_WRAPPER_HPP
  3. // (C) Copyright 2005 Matthias Troyer and Dave Abrahams
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/config.hpp> // msvc 6.0 needs this for warning suppression
  8. #if defined(BOOST_NO_STDC_NAMESPACE)
  9. namespace std{
  10. using ::size_t;
  11. } // namespace std
  12. #endif
  13. #include <boost/serialization/nvp.hpp>
  14. #include <boost/serialization/split_member.hpp>
  15. #include <boost/serialization/wrapper.hpp>
  16. #include <boost/serialization/collection_size_type.hpp>
  17. #include <boost/serialization/array_optimization.hpp>
  18. #include <boost/mpl/always.hpp>
  19. #include <boost/mpl/apply.hpp>
  20. #include <boost/mpl/bool_fwd.hpp>
  21. #include <boost/type_traits/remove_const.hpp>
  22. namespace boost { namespace serialization {
  23. template<class T>
  24. class array_wrapper :
  25. public wrapper_traits<const array_wrapper< T > >
  26. {
  27. private:
  28. array_wrapper & operator=(const array_wrapper & rhs);
  29. // note: I would like to make the copy constructor private but this breaks
  30. // make_array. So I make make_array a friend
  31. template<class Tx, class S>
  32. friend const boost::serialization::array_wrapper<Tx> make_array(Tx * t, S s);
  33. public:
  34. array_wrapper(const array_wrapper & rhs) :
  35. m_t(rhs.m_t),
  36. m_element_count(rhs.m_element_count)
  37. {}
  38. public:
  39. array_wrapper(T * t, std::size_t s) :
  40. m_t(t),
  41. m_element_count(s)
  42. {}
  43. // default implementation
  44. template<class Archive>
  45. void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const
  46. {
  47. // default implemention does the loop
  48. std::size_t c = count();
  49. T * t = address();
  50. while(0 < c--)
  51. ar & boost::serialization::make_nvp("item", *t++);
  52. }
  53. // optimized implementation
  54. template<class Archive>
  55. void serialize_optimized(Archive &ar, const unsigned int version, mpl::true_ )
  56. {
  57. boost::serialization::split_member(ar, *this, version);
  58. }
  59. // default implementation
  60. template<class Archive>
  61. void save(Archive &ar, const unsigned int version) const
  62. {
  63. ar.save_array(*this,version);
  64. }
  65. // default implementation
  66. template<class Archive>
  67. void load(Archive &ar, const unsigned int version)
  68. {
  69. ar.load_array(*this,version);
  70. }
  71. // default implementation
  72. template<class Archive>
  73. void serialize(Archive &ar, const unsigned int version)
  74. {
  75. typedef typename
  76. boost::serialization::use_array_optimization<Archive>::template apply<
  77. typename remove_const< T >::type
  78. >::type use_optimized;
  79. serialize_optimized(ar,version,use_optimized());
  80. }
  81. T * address() const
  82. {
  83. return m_t;
  84. }
  85. std::size_t count() const
  86. {
  87. return m_element_count;
  88. }
  89. private:
  90. T * const m_t;
  91. const std::size_t m_element_count;
  92. };
  93. template<class T, class S>
  94. inline
  95. const array_wrapper< T > make_array(T* t, S s){
  96. const array_wrapper< T > a(t, s);
  97. return a;
  98. }
  99. } } // end namespace boost::serialization
  100. #endif //BOOST_SERIALIZATION_ARRAY_WRAPPER_HPP