portable_binary_oarchive.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef PORTABLE_BINARY_OARCHIVE_HPP
  2. #define PORTABLE_BINARY_OARCHIVE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. #if defined(_MSC_VER)
  8. #pragma warning( push )
  9. #pragma warning( disable : 4244 )
  10. #endif
  11. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  12. // portable_binary_oarchive.hpp
  13. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  14. // Use, modification and distribution is subject to the Boost Software
  15. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  16. // http://www.boost.org/LICENSE_1_0.txt)
  17. // See http://www.boost.org for updates, documentation, and revision history.
  18. #include <ostream>
  19. #include <boost/serialization/string.hpp>
  20. #include <boost/archive/archive_exception.hpp>
  21. #include <boost/archive/basic_binary_oprimitive.hpp>
  22. #include <boost/archive/detail/common_oarchive.hpp>
  23. #include <boost/archive/detail/register_archive.hpp>
  24. #include "portable_binary_archive.hpp"
  25. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  26. // exception to be thrown if integer read from archive doesn't fit
  27. // variable being loaded
  28. class portable_binary_oarchive_exception :
  29. public boost::archive::archive_exception
  30. {
  31. public:
  32. typedef enum {
  33. invalid_flags
  34. } exception_code;
  35. portable_binary_oarchive_exception(exception_code c = invalid_flags )
  36. {}
  37. virtual const char *what( ) const throw( )
  38. {
  39. const char *msg = "programmer error";
  40. switch(code){
  41. case invalid_flags:
  42. msg = "cannot be both big and little endian";
  43. default:
  44. boost::archive::archive_exception::what();
  45. }
  46. return msg;
  47. }
  48. };
  49. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  50. // "Portable" output binary archive. This is a variation of the native binary
  51. // archive. it addresses integer size and endienness so that binary archives can
  52. // be passed across systems. Note:floating point types not addressed here
  53. class portable_binary_oarchive :
  54. public boost::archive::basic_binary_oprimitive<
  55. portable_binary_oarchive,
  56. std::ostream::char_type,
  57. std::ostream::traits_type
  58. >,
  59. public boost::archive::detail::common_oarchive<
  60. portable_binary_oarchive
  61. >
  62. {
  63. typedef boost::archive::basic_binary_oprimitive<
  64. portable_binary_oarchive,
  65. std::ostream::char_type,
  66. std::ostream::traits_type
  67. > primitive_base_t;
  68. typedef boost::archive::detail::common_oarchive<
  69. portable_binary_oarchive
  70. > archive_base_t;
  71. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  72. public:
  73. #else
  74. friend archive_base_t;
  75. friend primitive_base_t; // since with override save below
  76. friend class boost::archive::detail::interface_oarchive<
  77. portable_binary_oarchive
  78. >;
  79. friend class boost::archive::save_access;
  80. protected:
  81. #endif
  82. unsigned int m_flags;
  83. void save_impl(const boost::intmax_t l, const char maxsize);
  84. // add base class to the places considered when matching
  85. // save function to a specific set of arguments. Note, this didn't
  86. // work on my MSVC 7.0 system so we use the sure-fire method below
  87. // using archive_base_t::save;
  88. // default fall through for any types not specified here
  89. template<class T>
  90. void save(const T & t){
  91. save_impl(t, sizeof(T));
  92. }
  93. void save(const std::string & t){
  94. this->primitive_base_t::save(t);
  95. }
  96. #ifndef BOOST_NO_STD_WSTRING
  97. void save(const std::wstring & t){
  98. this->primitive_base_t::save(t);
  99. }
  100. #endif
  101. void save(const float & t){
  102. this->primitive_base_t::save(t);
  103. // floats not supported
  104. //BOOST_STATIC_ASSERT(false);
  105. }
  106. void save(const double & t){
  107. this->primitive_base_t::save(t);
  108. // doubles not supported
  109. //BOOST_STATIC_ASSERT(false);
  110. }
  111. void save(const char & t){
  112. this->primitive_base_t::save(t);
  113. }
  114. void save(const unsigned char & t){
  115. this->primitive_base_t::save(t);
  116. }
  117. // default processing - kick back to base class. Note the
  118. // extra stuff to get it passed borland compilers
  119. typedef boost::archive::detail::common_oarchive<portable_binary_oarchive>
  120. detail_common_oarchive;
  121. template<class T>
  122. void save_override(T & t){
  123. this->detail_common_oarchive::save_override(t);
  124. }
  125. // explicitly convert to char * to avoid compile ambiguities
  126. void save_override(const boost::archive::class_name_type & t){
  127. const std::string s(t);
  128. * this << s;
  129. }
  130. // binary files don't include the optional information
  131. void save_override(
  132. const boost::archive::class_id_optional_type & /* t */
  133. ){}
  134. void init(unsigned int flags);
  135. public:
  136. portable_binary_oarchive(std::ostream & os, unsigned flags = 0) :
  137. primitive_base_t(
  138. * os.rdbuf(),
  139. 0 != (flags & boost::archive::no_codecvt)
  140. ),
  141. archive_base_t(flags),
  142. m_flags(flags & (endian_big | endian_little))
  143. {
  144. init(flags);
  145. }
  146. portable_binary_oarchive(
  147. std::basic_streambuf<
  148. std::ostream::char_type,
  149. std::ostream::traits_type
  150. > & bsb,
  151. unsigned int flags
  152. ) :
  153. primitive_base_t(
  154. bsb,
  155. 0 != (flags & boost::archive::no_codecvt)
  156. ),
  157. archive_base_t(flags),
  158. m_flags(0)
  159. {
  160. init(flags);
  161. }
  162. };
  163. // required by export in boost version > 1.34
  164. #ifdef BOOST_SERIALIZATION_REGISTER_ARCHIVE
  165. BOOST_SERIALIZATION_REGISTER_ARCHIVE(portable_binary_oarchive)
  166. #endif
  167. // required by export in boost <= 1.34
  168. #define BOOST_ARCHIVE_CUSTOM_OARCHIVE_TYPES portable_binary_oarchive
  169. #if defined(_MSC_VER)
  170. #pragma warning( pop )
  171. #endif
  172. #endif // PORTABLE_BINARY_OARCHIVE_HPP