array.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <boost/serialization/nvp.hpp>
  8. #include <boost/serialization/split_member.hpp>
  9. #include <boost/serialization/wrapper.hpp>
  10. #include <boost/mpl/always.hpp>
  11. #include <boost/mpl/apply.hpp>
  12. #include <boost/mpl/bool.hpp>
  13. #include <boost/type_traits/remove_const.hpp>
  14. #include <boost/array.hpp>
  15. #include <iostream>
  16. #include <cstddef> // std::size_t
  17. #include <cstddef>
  18. #include <boost/config.hpp> // msvc 6.0 needs this for warning suppression
  19. #if defined(BOOST_NO_STDC_NAMESPACE)
  20. namespace std{
  21. using ::size_t;
  22. } // namespace std
  23. #endif
  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<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. // default implementation
  47. template<class Archive>
  48. void serialize_optimized(Archive &ar, const unsigned int, mpl::false_ ) const
  49. {
  50. // default implemention does the loop
  51. std::size_t c = count();
  52. value_type * t = address();
  53. while(0 < c--)
  54. ar & boost::serialization::make_nvp("item", *t++);
  55. }
  56. // optimized implementation
  57. template<class Archive>
  58. void serialize_optimized(Archive &ar, const unsigned int version, mpl::true_ )
  59. {
  60. boost::serialization::split_member(ar, *this, version);
  61. }
  62. // default implementation
  63. template<class Archive>
  64. void save(Archive &ar, const unsigned int version) const
  65. {
  66. ar.save_array(*this,version);
  67. }
  68. // default implementation
  69. template<class Archive>
  70. void load(Archive &ar, const unsigned int version)
  71. {
  72. ar.load_array(*this,version);
  73. }
  74. // default implementation
  75. template<class Archive>
  76. void serialize(Archive &ar, const unsigned int version)
  77. {
  78. typedef BOOST_DEDUCED_TYPENAME
  79. boost::serialization::use_array_optimization<Archive>::template apply<
  80. BOOST_DEDUCED_TYPENAME remove_const<T>::type
  81. >::type use_optimized;
  82. serialize_optimized(ar,version,use_optimized());
  83. }
  84. value_type* address() const
  85. {
  86. return m_t;
  87. }
  88. std::size_t count() const
  89. {
  90. return m_element_count;
  91. }
  92. private:
  93. value_type* m_t;
  94. std::size_t const m_element_count;
  95. };
  96. template<class T>
  97. inline
  98. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  99. const
  100. #endif
  101. array<T> make_array( T* t, std::size_t s){
  102. return array<T>(t, s);
  103. }
  104. template <class Archive, class T, std::size_t N>
  105. void serialize(Archive& ar, boost::array<T,N>& a, const unsigned int /* version */)
  106. {
  107. ar & boost::serialization::make_nvp("elems",a.elems);
  108. }
  109. } } // end namespace boost::serialization
  110. #ifdef __BORLANDC__
  111. // ignore optimizations for Borland
  112. #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive)
  113. #else
  114. #define BOOST_SERIALIZATION_USE_ARRAY_OPTIMIZATION(Archive) \
  115. namespace boost { namespace serialization { \
  116. template <> struct use_array_optimization<Archive> { \
  117. template <class ValueType> \
  118. struct apply : boost::mpl::apply1<Archive::use_array_optimization \
  119. , BOOST_DEDUCED_TYPENAME boost::remove_const<ValueType>::type \
  120. >::type {}; \
  121. }; }}
  122. #endif // __BORLANDC__
  123. #endif //BOOST_SERIALIZATION_ARRAY_HPP