basic_binary_oarchive.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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)
  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/integer.hpp>
  23. #include <boost/integer_traits.hpp>
  24. #include <boost/archive/detail/common_oarchive.hpp>
  25. #include <boost/serialization/string.hpp>
  26. #include <boost/serialization/collection_size_type.hpp>
  27. #include <boost/serialization/item_version_type.hpp>
  28. #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
  29. #ifdef BOOST_MSVC
  30. # pragma warning(push)
  31. # pragma warning(disable : 4511 4512)
  32. #endif
  33. namespace boost {
  34. namespace archive {
  35. namespace detail {
  36. template<class Archive> class interface_oarchive;
  37. } // namespace detail
  38. //////////////////////////////////////////////////////////////////////
  39. // class basic_binary_oarchive - write serialized objects to a binary output stream
  40. // note: this archive has no pretensions to portability. Archive format
  41. // may vary across machine architectures and compilers. About the only
  42. // guarentee is that an archive created with this code will be readable
  43. // by a program built with the same tools for the same machne. This class
  44. // does have the virtue of buiding the smalles archive in the minimum amount
  45. // of time. So under some circumstances it may be he right choice.
  46. template<class Archive>
  47. class BOOST_SYMBOL_VISIBLE basic_binary_oarchive :
  48. public detail::common_oarchive<Archive>
  49. {
  50. #ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  51. public:
  52. #else
  53. protected:
  54. #if BOOST_WORKAROUND(BOOST_MSVC, < 1500)
  55. // for some inexplicable reason insertion of "class" generates compile erro
  56. // on msvc 7.1
  57. friend detail::interface_oarchive<Archive>;
  58. #else
  59. friend class detail::interface_oarchive<Archive>;
  60. #endif
  61. #endif
  62. // any datatype not specifed below will be handled by base class
  63. typedef detail::common_oarchive<Archive> detail_common_oarchive;
  64. template<class T>
  65. void save_override(const T & t){
  66. this->detail_common_oarchive::save_override(t);
  67. }
  68. // include these to trap a change in binary format which
  69. // isn't specifically handled
  70. BOOST_STATIC_ASSERT(sizeof(tracking_type) == sizeof(bool));
  71. // upto 32K classes
  72. BOOST_STATIC_ASSERT(sizeof(class_id_type) == sizeof(int_least16_t));
  73. BOOST_STATIC_ASSERT(sizeof(class_id_reference_type) == sizeof(int_least16_t));
  74. // upto 2G objects
  75. BOOST_STATIC_ASSERT(sizeof(object_id_type) == sizeof(uint_least32_t));
  76. BOOST_STATIC_ASSERT(sizeof(object_reference_type) == sizeof(uint_least32_t));
  77. // binary files don't include the optional information
  78. void save_override(const class_id_optional_type & /* t */){}
  79. // enable this if we decide to support generation of previous versions
  80. #if 0
  81. void save_override(const boost::archive::version_type & t){
  82. library_version_type lvt = this->get_library_version();
  83. if(boost::archive::library_version_type(7) < lvt){
  84. this->detail_common_oarchive::save_override(t);
  85. }
  86. else
  87. if(boost::archive::library_version_type(6) < lvt){
  88. const boost::uint_least16_t x = t;
  89. * this->This() << x;
  90. }
  91. else{
  92. const unsigned int x = t;
  93. * this->This() << x;
  94. }
  95. }
  96. void save_override(const boost::serialization::item_version_type & t){
  97. library_version_type lvt = this->get_library_version();
  98. if(boost::archive::library_version_type(7) < lvt){
  99. this->detail_common_oarchive::save_override(t);
  100. }
  101. else
  102. if(boost::archive::library_version_type(6) < lvt){
  103. const boost::uint_least16_t x = t;
  104. * this->This() << x;
  105. }
  106. else{
  107. const unsigned int x = t;
  108. * this->This() << x;
  109. }
  110. }
  111. void save_override(class_id_type & t){
  112. library_version_type lvt = this->get_library_version();
  113. if(boost::archive::library_version_type(7) < lvt){
  114. this->detail_common_oarchive::save_override(t);
  115. }
  116. else
  117. if(boost::archive::library_version_type(6) < lvt){
  118. const boost::int_least16_t x = t;
  119. * this->This() << x;
  120. }
  121. else{
  122. const int x = t;
  123. * this->This() << x;
  124. }
  125. }
  126. void save_override(class_id_reference_type & t){
  127. save_override(static_cast<class_id_type &>(t));
  128. }
  129. #endif
  130. // explicitly convert to char * to avoid compile ambiguities
  131. void save_override(const class_name_type & t){
  132. const std::string s(t);
  133. * this->This() << s;
  134. }
  135. #if 0
  136. void save_override(const serialization::collection_size_type & t){
  137. if (get_library_version() < boost::archive::library_version_type(6)){
  138. unsigned int x=0;
  139. * this->This() >> x;
  140. t = serialization::collection_size_type(x);
  141. }
  142. else{
  143. * this->This() >> t;
  144. }
  145. }
  146. #endif
  147. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  148. init();
  149. basic_binary_oarchive(unsigned int flags) :
  150. detail::common_oarchive<Archive>(flags)
  151. {}
  152. };
  153. } // namespace archive
  154. } // namespace boost
  155. #ifdef BOOST_MSVC
  156. #pragma warning(pop)
  157. #endif
  158. #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  159. #endif // BOOST_ARCHIVE_BASIC_BINARY_OARCHIVE_HPP