array_wrapper.hpp 3.2 KB

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