1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // basic_binary_oprimitive.ipp:
4 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org for updates, documentation, and revision history.
12 #include <cstddef> // NULL
15 #include <boost/config.hpp>
17 #if defined(BOOST_NO_STDC_NAMESPACE) && ! defined(__LIBCOMO__)
23 #ifndef BOOST_NO_CWCHAR
25 #ifdef BOOST_NO_STDC_NAMESPACE
26 namespace std{ using ::wcslen; }
30 #include <boost/detail/workaround.hpp>
32 #include <boost/archive/add_facet.hpp>
33 #include <boost/archive/codecvt_null.hpp>
38 //////////////////////////////////////////////////////////////////////
39 // implementation of basic_binary_oprimitive
41 template<class Archive, class Elem, class Tr>
42 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
43 basic_binary_oprimitive<Archive, Elem, Tr>::init()
45 // record native sizes of fundamental types
46 // this is to permit detection of attempts to pass
47 // native binary archives accross incompatible machines.
48 // This is not foolproof but its better than nothing.
49 this->This()->save(static_cast<unsigned char>(sizeof(int)));
50 this->This()->save(static_cast<unsigned char>(sizeof(long)));
51 this->This()->save(static_cast<unsigned char>(sizeof(float)));
52 this->This()->save(static_cast<unsigned char>(sizeof(double)));
53 // for checking endianness
54 this->This()->save(int(1));
57 template<class Archive, class Elem, class Tr>
58 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
59 basic_binary_oprimitive<Archive, Elem, Tr>::save(const char * s)
61 std::size_t l = std::strlen(s);
62 this->This()->save(l);
66 template<class Archive, class Elem, class Tr>
67 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
68 basic_binary_oprimitive<Archive, Elem, Tr>::save(const std::string &s)
70 std::size_t l = static_cast<std::size_t>(s.size());
71 this->This()->save(l);
72 save_binary(s.data(), l);
75 #ifndef BOOST_NO_CWCHAR
76 template<class Archive, class Elem, class Tr>
77 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
78 basic_binary_oprimitive<Archive, Elem, Tr>::save(const wchar_t * ws)
80 std::size_t l = std::wcslen(ws);
81 this->This()->save(l);
82 save_binary(ws, l * sizeof(wchar_t) / sizeof(char));
86 #ifndef BOOST_NO_STD_WSTRING
87 template<class Archive, class Elem, class Tr>
88 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
89 basic_binary_oprimitive<Archive, Elem, Tr>::save(const std::wstring &ws)
91 std::size_t l = ws.size();
92 this->This()->save(l);
93 save_binary(ws.data(), l * sizeof(wchar_t) / sizeof(char));
97 template<class Archive, class Elem, class Tr>
98 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
99 basic_binary_oprimitive<Archive, Elem, Tr>::basic_binary_oprimitive(
100 std::basic_streambuf<Elem, Tr> & sb,
103 #ifndef BOOST_NO_STD_LOCALE
105 archive_locale(NULL),
109 archive_locale.reset(
111 std::locale::classic(),
112 new codecvt_null<Elem>
115 m_sb.pubimbue(* archive_locale);
123 // some libraries including stl and libcomo fail if the
124 // buffer isn't flushed before the code_cvt facet is changed.
125 // I think this is a bug. We explicity invoke sync to when
126 // we're done with the streambuf to work around this problem.
127 // Note that sync is a protected member of stream buff so we
128 // have to invoke it through a contrived derived class.
130 // note: use "using" to get past msvc bug
132 template<class Elem, class Tr>
133 class output_streambuf_access : public std::basic_streambuf<Elem, Tr> {
136 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
137 return this->basic_streambuf::sync();
139 return this->basic_streambuf<Elem, Tr>::sync();
145 // scoped_ptr requires that g be a complete type at time of
146 // destruction so define destructor here rather than in the header
147 template<class Archive, class Elem, class Tr>
148 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
149 basic_binary_oprimitive<Archive, Elem, Tr>::~basic_binary_oprimitive(){
151 //destructor can't throw
153 static_cast<detail::output_streambuf_access<Elem, Tr> &>(m_sb).sync();
159 } // namespace archive