basic_binary_oprimitive.ipp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // basic_binary_oprimitive.ipp:
  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. // See http://www.boost.org for updates, documentation, and revision history.
  8. #include <ostream>
  9. #include <cstddef> // NULL
  10. #include <cstring>
  11. #include <boost/config.hpp>
  12. #if defined(BOOST_NO_STDC_NAMESPACE) && ! defined(__LIBCOMO__)
  13. namespace std{
  14. using ::strlen;
  15. } // namespace std
  16. #endif
  17. #ifndef BOOST_NO_CWCHAR
  18. #include <cwchar>
  19. #ifdef BOOST_NO_STDC_NAMESPACE
  20. namespace std{ using ::wcslen; }
  21. #endif
  22. #endif
  23. #include <boost/archive/basic_binary_oprimitive.hpp>
  24. #include <boost/core/no_exceptions_support.hpp>
  25. namespace boost {
  26. namespace archive {
  27. //////////////////////////////////////////////////////////////////////
  28. // implementation of basic_binary_oprimitive
  29. template<class Archive, class Elem, class Tr>
  30. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  31. basic_binary_oprimitive<Archive, Elem, Tr>::init()
  32. {
  33. // record native sizes of fundamental types
  34. // this is to permit detection of attempts to pass
  35. // native binary archives accross incompatible machines.
  36. // This is not foolproof but its better than nothing.
  37. this->This()->save(static_cast<unsigned char>(sizeof(int)));
  38. this->This()->save(static_cast<unsigned char>(sizeof(long)));
  39. this->This()->save(static_cast<unsigned char>(sizeof(float)));
  40. this->This()->save(static_cast<unsigned char>(sizeof(double)));
  41. // for checking endianness
  42. this->This()->save(int(1));
  43. }
  44. template<class Archive, class Elem, class Tr>
  45. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  46. basic_binary_oprimitive<Archive, Elem, Tr>::save(const char * s)
  47. {
  48. std::size_t l = std::strlen(s);
  49. this->This()->save(l);
  50. save_binary(s, l);
  51. }
  52. template<class Archive, class Elem, class Tr>
  53. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  54. basic_binary_oprimitive<Archive, Elem, Tr>::save(const std::string &s)
  55. {
  56. std::size_t l = static_cast<std::size_t>(s.size());
  57. this->This()->save(l);
  58. save_binary(s.data(), l);
  59. }
  60. #ifndef BOOST_NO_CWCHAR
  61. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  62. template<class Archive, class Elem, class Tr>
  63. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  64. basic_binary_oprimitive<Archive, Elem, Tr>::save(const wchar_t * ws)
  65. {
  66. std::size_t l = std::wcslen(ws);
  67. this->This()->save(l);
  68. save_binary(ws, l * sizeof(wchar_t) / sizeof(char));
  69. }
  70. #endif
  71. #ifndef BOOST_NO_STD_WSTRING
  72. template<class Archive, class Elem, class Tr>
  73. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  74. basic_binary_oprimitive<Archive, Elem, Tr>::save(const std::wstring &ws)
  75. {
  76. std::size_t l = ws.size();
  77. this->This()->save(l);
  78. save_binary(ws.data(), l * sizeof(wchar_t) / sizeof(char));
  79. }
  80. #endif
  81. #endif // BOOST_NO_CWCHAR
  82. template<class Archive, class Elem, class Tr>
  83. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  84. basic_binary_oprimitive<Archive, Elem, Tr>::basic_binary_oprimitive(
  85. std::basic_streambuf<Elem, Tr> & sb,
  86. bool no_codecvt
  87. ) :
  88. #ifndef BOOST_NO_STD_LOCALE
  89. m_sb(sb),
  90. codecvt_null_facet(1),
  91. locale_saver(m_sb),
  92. archive_locale(sb.getloc(), & codecvt_null_facet)
  93. {
  94. if(! no_codecvt){
  95. m_sb.pubsync();
  96. m_sb.pubimbue(archive_locale);
  97. }
  98. }
  99. #else
  100. m_sb(sb)
  101. {}
  102. #endif
  103. // scoped_ptr requires that g be a complete type at time of
  104. // destruction so define destructor here rather than in the header
  105. template<class Archive, class Elem, class Tr>
  106. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  107. basic_binary_oprimitive<Archive, Elem, Tr>::~basic_binary_oprimitive(){}
  108. } // namespace archive
  109. } // namespace boost