demo_fast_archive.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // demo_fast_binary_archive.cpp
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  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. // should pass compilation and execution
  8. #include <sstream>
  9. #include <boost/static_assert.hpp>
  10. #include <boost/type_traits/is_array.hpp>
  11. #define BOOST_ARCHIVE_SOURCE
  12. #include <boost/archive/binary_oarchive_impl.hpp>
  13. #include <boost/archive/binary_iarchive_impl.hpp>
  14. #include <boost/archive/detail/register_archive.hpp>
  15. // include template definitions for base classes used. Otherwise
  16. // you'll get link failure with undefined symbols
  17. #include <boost/archive/impl/basic_binary_oprimitive.ipp>
  18. #include <boost/archive/impl/basic_binary_iprimitive.ipp>
  19. #include <boost/archive/impl/basic_binary_oarchive.ipp>
  20. #include <boost/archive/impl/basic_binary_iarchive.ipp>
  21. using namespace boost::archive;
  22. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  23. // "Fast" output binary archive. This is a variation of the native binary
  24. class fast_binary_oarchive :
  25. // don't derive from binary_oarchive !!!
  26. public binary_oarchive_impl<
  27. fast_binary_oarchive,
  28. std::ostream::char_type,
  29. std::ostream::traits_type
  30. >
  31. {
  32. typedef fast_binary_oarchive derived_t;
  33. typedef binary_oarchive_impl<
  34. fast_binary_oarchive,
  35. std::ostream::char_type,
  36. std::ostream::traits_type
  37. > base_t;
  38. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  39. public:
  40. #else
  41. friend class boost::archive::detail::interface_oarchive<derived_t>;
  42. friend class basic_binary_oarchive<derived_t>;
  43. friend class basic_binary_oprimitive<
  44. derived_t,
  45. std::ostream::char_type,
  46. std::ostream::traits_type
  47. >;
  48. friend class boost::archive::save_access;
  49. #endif
  50. // add base class to the places considered when matching
  51. // save function to a specific set of arguments. Note, this didn't
  52. // work on my MSVC 7.0 system using
  53. // binary_oarchive_impl<derived_t>::load_override;
  54. // so we use the sure-fire method below. This failed to work as well
  55. template<class T>
  56. void save_override(T & t){
  57. base_t::save_override(t);
  58. // verify that this program is in fact working by making sure
  59. // that arrays are getting passed here
  60. BOOST_STATIC_ASSERT(! (boost::is_array<T>::value) );
  61. }
  62. template<int N>
  63. void save_override(const int (& t)[N]){
  64. save_binary(t, sizeof(t));
  65. }
  66. template<int N>
  67. void save_override(const unsigned int (& t)[N]){
  68. save_binary(t, sizeof(t));
  69. }
  70. template<int N>
  71. void save_override(const long (& t)[N]){
  72. save_binary(t, sizeof(t));
  73. }
  74. template<int N>
  75. void save_override(const unsigned long (& t)[N]){
  76. save_binary(t, sizeof(t));
  77. }
  78. public:
  79. fast_binary_oarchive(std::ostream & os, unsigned flags = 0) :
  80. base_t(os, flags)
  81. {}
  82. fast_binary_oarchive(std::streambuf & bsb, unsigned int flags = 0) :
  83. base_t(bsb, flags)
  84. {}
  85. };
  86. // required by export
  87. BOOST_SERIALIZATION_REGISTER_ARCHIVE(fast_binary_oarchive)
  88. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  89. // "Fast" input binary archive. This is a variation of the native binary
  90. class fast_binary_iarchive :
  91. // don't derive from binary_oarchive !!!
  92. public binary_iarchive_impl<
  93. fast_binary_iarchive,
  94. std::istream::char_type,
  95. std::istream::traits_type
  96. >
  97. {
  98. typedef fast_binary_iarchive derived_t;
  99. typedef binary_iarchive_impl<
  100. fast_binary_iarchive,
  101. std::istream::char_type,
  102. std::istream::traits_type
  103. > base_t;
  104. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  105. public:
  106. #else
  107. friend class boost::archive::detail::interface_iarchive<derived_t>;
  108. friend class basic_binary_iarchive<derived_t>;
  109. friend class basic_binary_iprimitive<
  110. derived_t,
  111. std::ostream::char_type,
  112. std::ostream::traits_type
  113. >;
  114. friend class boost::archive::load_access;
  115. #endif
  116. // add base class to the places considered when matching
  117. // save function to a specific set of arguments. Note, this didn't
  118. // work on my MSVC 7.0 system using
  119. // binary_oarchive_impl<derived_t>::load_override;
  120. // so we use the sure-fire method below. This failed to work as well
  121. template<class T>
  122. void load_override(T & t){
  123. base_t::load_override(t);
  124. BOOST_STATIC_ASSERT(! (boost::is_array<T>::value) );
  125. }
  126. template<int N>
  127. void load_override(int (& t)[N]){
  128. load_binary(t, sizeof(t));
  129. }
  130. template<int N>
  131. void load_override(unsigned int (& t)[N]){
  132. load_binary(t, sizeof(t));
  133. }
  134. template<int N>
  135. void load_override(long (& t)[N]){
  136. load_binary(t, sizeof(t));
  137. }
  138. template<int N>
  139. void load_override(unsigned long (& t)[N]){
  140. load_binary(t, sizeof(t));
  141. }
  142. public:
  143. fast_binary_iarchive(std::istream & is, unsigned int flags = 0) :
  144. base_t(is, flags)
  145. {}
  146. fast_binary_iarchive(std::streambuf & bsb, unsigned int flags = 0) :
  147. base_t(bsb, flags)
  148. {}
  149. };
  150. // required by export
  151. BOOST_SERIALIZATION_REGISTER_ARCHIVE(fast_binary_iarchive)
  152. int main( int argc, char* argv[] )
  153. {
  154. const int a[3] = {1, 2, 3};
  155. int a1[3] = {4, 5, 6};
  156. std::stringstream ss;
  157. {
  158. fast_binary_oarchive pboa(ss);
  159. pboa << a;
  160. }
  161. {
  162. fast_binary_iarchive pbia(ss);
  163. pbia >> a1;
  164. }
  165. return (a[0] != a1[0]) || (a[1] != a1[1]) || (a[2] != a1[2]);
  166. }