basic_binary_oarchive.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef BOOST_ARCHIVE_BASIC_BINARY_OARCHIVE_HPP
  2. #define BOOST_ARCHIVE_BASIC_BINARY_OARCHIVE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // basic_binary_oarchive.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. // archives stored as native binary - this should be the fastest way
  15. // to archive the state of a group of obects. It makes no attempt to
  16. // convert to any canonical form.
  17. // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE
  18. // ON PLATFORM APART FROM THE ONE THEY ARE CREATE ON
  19. #include <boost/assert.hpp>
  20. #include <boost/config.hpp>
  21. #include <boost/detail/workaround.hpp>
  22. #include <boost/serialization/pfto.hpp>
  23. #include <boost/integer.hpp>
  24. #include <boost/integer_traits.hpp>
  25. #include <boost/archive/detail/common_oarchive.hpp>
  26. #include <boost/serialization/string.hpp>
  27. #include <boost/serialization/collection_size_type.hpp>
  28. #include <boost/serialization/item_version_type.hpp>
  29. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  30. #ifdef BOOST_MSVC
  31. # pragma warning(push)
  32. # pragma warning(disable : 4511 4512)
  33. #endif
  34. namespace boost {
  35. namespace archive {
  36. //////////////////////////////////////////////////////////////////////
  37. // class basic_binary_oarchive - write serialized objects to a binary output stream
  38. // note: this archive has no pretensions to portability. Archive format
  39. // may vary across machine architectures and compilers. About the only
  40. // guarentee is that an archive created with this code will be readable
  41. // by a program built with the same tools for the same machne. This class
  42. // does have the virtue of buiding the smalles archive in the minimum amount
  43. // of time. So under some circumstances it may be he right choice.
  44. template<class Archive>
  45. class basic_binary_oarchive :
  46. public archive::detail::common_oarchive<Archive>
  47. {
  48. protected:
  49. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  50. public:
  51. #elif defined(BOOST_MSVC)
  52. // for some inexplicable reason insertion of "class" generates compile erro
  53. // on msvc 7.1
  54. friend detail::interface_oarchive<Archive>;
  55. #else
  56. friend class detail::interface_oarchive<Archive>;
  57. #endif
  58. // any datatype not specifed below will be handled by base class
  59. typedef detail::common_oarchive<Archive> detail_common_oarchive;
  60. template<class T>
  61. void save_override(const T & t, BOOST_PFTO int version){
  62. this->detail_common_oarchive::save_override(t, static_cast<int>(version));
  63. }
  64. // include these to trap a change in binary format which
  65. // isn't specifically handled
  66. BOOST_STATIC_ASSERT(sizeof(tracking_type) == sizeof(bool));
  67. // upto 32K classes
  68. BOOST_STATIC_ASSERT(sizeof(class_id_type) == sizeof(int_least16_t));
  69. BOOST_STATIC_ASSERT(sizeof(class_id_reference_type) == sizeof(int_least16_t));
  70. // upto 2G objects
  71. BOOST_STATIC_ASSERT(sizeof(object_id_type) == sizeof(uint_least32_t));
  72. BOOST_STATIC_ASSERT(sizeof(object_reference_type) == sizeof(uint_least32_t));
  73. // binary files don't include the optional information
  74. void save_override(const class_id_optional_type & /* t */, int){}
  75. // enable this if we decide to support generation of previous versions
  76. #if 0
  77. void save_override(const boost::archive::version_type & t, int version){
  78. library_version_type lvt = this->get_library_version();
  79. if(boost::archive::library_version_type(7) < lvt){
  80. this->detail_common_oarchive::save_override(t, version);
  81. }
  82. else
  83. if(boost::archive::library_version_type(6) < lvt){
  84. const boost::uint_least16_t x = t;
  85. * this->This() << x;
  86. }
  87. else{
  88. const unsigned int x = t;
  89. * this->This() << x;
  90. }
  91. }
  92. void save_override(const boost::serialization::item_version_type & t, int version){
  93. library_version_type lvt = this->get_library_version();
  94. if(boost::archive::library_version_type(7) < lvt){
  95. this->detail_common_oarchive::save_override(t, version);
  96. }
  97. else
  98. if(boost::archive::library_version_type(6) < lvt){
  99. const boost::uint_least16_t x = t;
  100. * this->This() << x;
  101. }
  102. else{
  103. const unsigned int x = t;
  104. * this->This() << x;
  105. }
  106. }
  107. void save_override(class_id_type & t, int version){
  108. library_version_type lvt = this->get_library_version();
  109. if(boost::archive::library_version_type(7) < lvt){
  110. this->detail_common_oarchive::save_override(t, version);
  111. }
  112. else
  113. if(boost::archive::library_version_type(6) < lvt){
  114. const boost::int_least16_t x = t;
  115. * this->This() << x;
  116. }
  117. else{
  118. const int x = t;
  119. * this->This() << x;
  120. }
  121. }
  122. void save_override(class_id_reference_type & t, int version){
  123. save_override(static_cast<class_id_type &>(t), version);
  124. }
  125. #endif
  126. // explicitly convert to char * to avoid compile ambiguities
  127. void save_override(const class_name_type & t, int){
  128. const std::string s(t);
  129. * this->This() << s;
  130. }
  131. #if 0
  132. void save_override(const serialization::collection_size_type & t, int){
  133. if (get_library_version() < boost::archive::library_version_type(6)){
  134. unsigned int x=0;
  135. * this->This() >> x;
  136. t = serialization::collection_size_type(x);
  137. }
  138. else{
  139. * this->This() >> t;
  140. }
  141. }
  142. #endif
  143. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  144. init();
  145. basic_binary_oarchive(unsigned int flags) :
  146. detail::common_oarchive<Archive>(flags)
  147. {}
  148. };
  149. } // namespace archive
  150. } // namespace boost
  151. #ifdef BOOST_MSVC
  152. #pragma warning(pop)
  153. #endif
  154. #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  155. #endif // BOOST_ARCHIVE_BASIC_BINARY_OARCHIVE_HPP