array.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef BOOST_SERIALIZATION_ARRAY_HPP
  2. #define BOOST_SERIALIZATION_ARRAY_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 <cstddef> // std::size_t
  9. #include <cstddef>
  10. #include <boost/config.hpp> // msvc 6.0 needs this for warning suppression
  11. #if defined(BOOST_NO_STDC_NAMESPACE)
  12. namespace std{
  13. using ::size_t;
  14. } // namespace std
  15. #endif
  16. #include <boost/serialization/nvp.hpp>
  17. #include <boost/serialization/split_member.hpp>
  18. #include <boost/serialization/wrapper.hpp>
  19. #include <boost/mpl/always.hpp>
  20. #include <boost/mpl/apply.hpp>
  21. #include <boost/mpl/bool.hpp>
  22. #include <boost/type_traits/remove_const.hpp>
  23. #include <boost/array.hpp>
  24. namespace boost { namespace serialization {
  25. // traits to specify whether to use an optimized array serialization
  26. #ifdef __BORLANDC__
  27. // workaround for Borland compiler
  28. template <class Archive>
  29. struct use_array_optimization {
  30. template <class T> struct apply : boost::mpl::false_ {};
  31. };
  32. #else
  33. template <class Archive>
  34. struct use_array_optimization : boost::mpl::always<boost::mpl::false_> {};
  35. #endif
  36. template<class T>
  37. class array :
  38. public wrapper_traits<const array< T > >
  39. {
  40. public:
  41. typedef T value_type;
  42. array(value_type* t, std::size_t s) :
  43. m_t(t),
  44. m_element_count(s)
  45. {}
  46. array(const array & rhs) :
  47. m_t(rhs.m_t),
  48. m_element_count(rhs.m_element_count)
  49. {}
  50. array & operator=(const array & rhs){
  51. m_t = rhs.m_t;
  52. m_element_count = rhs.m_element_count;
  53. }
  54. // default implementation
  55. template<class Archive>
  56. void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const
  57. {
  58. // default implemention does the loop
  59. std::size_t c = count();
  60. value_type * t = address();
  61. while(0 < c--)
  62. ar & boost::serialization::make_nvp("item", *t++);
  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. // default implementation
  71. template<class Archive>
  72. void save(Archive &ar, const unsigned int version) const
  73. {
  74. ar.save_array(*this,version);
  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. // default implementation
  83. template<class Archive>
  84. void serialize(Archive &ar, const unsigned int version)
  85. {
  86. typedef BOOST_DEDUCED_TYPENAME
  87. boost::serialization::use_array_optimization<Archive>::template apply<
  88. BOOST_DEDUCED_TYPENAME remove_const< T >::type
  89. >::type use_optimized;
  90. serialize_optimized(ar,version,use_optimized());
  91. }
  92. value_type* address() const
  93. {
  94. return m_t;
  95. }
  96. std::size_t count() const
  97. {
  98. return m_element_count;
  99. }
  100. private:
  101. value_type* m_t;
  102. std::size_t m_element_count;
  103. };
  104. template<class T>
  105. inline
  106. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  107. const
  108. #endif
  109. array< T > make_array( T* t, std::size_t s){
  110. return array< T >(t, s);
  111. }
  112. template <class Archive, class T, std::size_t N>
  113. void serialize(Archive& ar, boost::array<T,N>& a, const unsigned int /* version */)
  114. {
  115. ar & boost::serialization::make_nvp("elems",a.elems);
  116. }
  117. } } // end namespace boost::serialization
  118. #ifdef __BORLANDC__
  119. // ignore optimizations for Borland
  120. #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive)
  121. #else
  122. #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive) \
  123. namespace boost { namespace serialization { \
  124. template <> struct use_array_optimization<Archive> { \
  125. template <class ValueType> \
  126. struct apply : boost::mpl::apply1<Archive::use_array_optimization \
  127. , BOOST_DEDUCED_TYPENAME boost::remove_const<ValueType>::type \
  128. >::type {}; \
  129. }; }}
  130. #endif // __BORLANDC__
  131. #endif //BOOST_SERIALIZATION_ARRAY_HPP