portable_binary_oarchive.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // portable_binary_oarchive.cpp
  3. // (C) Copyright 2002-7 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. // See http://www.boost.org for updates, documentation, and revision history.
  8. #include <ostream>
  9. #include <boost/predef/other/endian.h>
  10. #include "portable_binary_oarchive.hpp"
  11. void
  12. portable_binary_oarchive::save_impl(
  13. const boost::intmax_t l,
  14. const char maxsize
  15. ){
  16. char size = 0;
  17. if(l == 0){
  18. this->primitive_base_t::save(size);
  19. return;
  20. }
  21. boost::intmax_t ll;
  22. bool negative = (l < 0);
  23. if(negative)
  24. ll = -l;
  25. else
  26. ll = l;
  27. do{
  28. ll >>= CHAR_BIT;
  29. ++size;
  30. }while(ll != 0);
  31. this->primitive_base_t::save(
  32. static_cast<char>(negative ? -size : size)
  33. );
  34. if(negative)
  35. ll = -l;
  36. else
  37. ll = l;
  38. char * cptr = reinterpret_cast<char *>(& ll);
  39. #if BOOST_ENDIAN_BIG_BYTE
  40. cptr += (sizeof(boost::intmax_t) - size);
  41. if(m_flags & endian_little)
  42. reverse_bytes(size, cptr);
  43. #else
  44. if(m_flags & endian_big)
  45. reverse_bytes(size, cptr);
  46. #endif
  47. this->primitive_base_t::save_binary(cptr, size);
  48. }
  49. void
  50. portable_binary_oarchive::init(unsigned int flags) {
  51. if(m_flags == (endian_big | endian_little)){
  52. boost::serialization::throw_exception(
  53. portable_binary_oarchive_exception()
  54. );
  55. }
  56. if(0 == (flags & boost::archive::no_header)){
  57. // write signature in an archive version independent manner
  58. const std::string file_signature(
  59. boost::archive::BOOST_ARCHIVE_SIGNATURE()
  60. );
  61. * this << file_signature;
  62. // write library version
  63. const boost::archive::library_version_type v(
  64. boost::archive::BOOST_ARCHIVE_VERSION()
  65. );
  66. * this << v;
  67. }
  68. save(static_cast<unsigned char>(m_flags >> CHAR_BIT));
  69. }
  70. #include <boost/archive/impl/archive_serializer_map.ipp>
  71. #include <boost/archive/impl/basic_binary_oprimitive.ipp>
  72. namespace boost {
  73. namespace archive {
  74. namespace detail {
  75. template class archive_serializer_map<portable_binary_oarchive>;
  76. }
  77. template class basic_binary_oprimitive<
  78. portable_binary_oarchive,
  79. std::ostream::char_type,
  80. std::ostream::traits_type
  81. > ;
  82. } // namespace archive
  83. } // namespace boost