1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
---|
2 | // basic_binary_oprimitive.ipp:
|
---|
3 |
|
---|
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)
|
---|
8 |
|
---|
9 | // See http://www.boost.org for updates, documentation, and revision history.
|
---|
10 |
|
---|
11 | #include <ostream>
|
---|
12 |
|
---|
13 | #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
|
---|
14 |
|
---|
15 | #include <cstring>
|
---|
16 | #if defined(BOOST_NO_STDC_NAMESPACE) && ! defined(__LIBCOMO__)
|
---|
17 | namespace std{
|
---|
18 | using ::strlen;
|
---|
19 | } // namespace std
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include <boost/scoped_ptr.hpp>
|
---|
23 |
|
---|
24 | #ifndef BOOST_NO_CWCHAR
|
---|
25 | #include <cwchar>
|
---|
26 | #ifdef BOOST_NO_STDC_NAMESPACE
|
---|
27 | namespace std{ using ::wcslen; }
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #endif
|
---|
31 | #include <boost/archive/codecvt_null.hpp>
|
---|
32 | #include <boost/archive/add_facet.hpp>
|
---|
33 | #include <boost/detail/no_exceptions_support.hpp>
|
---|
34 |
|
---|
35 | namespace boost {
|
---|
36 | namespace archive {
|
---|
37 |
|
---|
38 | //////////////////////////////////////////////////////////////////////
|
---|
39 | // implementation of basic_binary_oprimitive
|
---|
40 |
|
---|
41 | template<class Archive, class OStream>
|
---|
42 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
---|
43 | basic_binary_oprimitive<Archive, OStream>::init()
|
---|
44 | {
|
---|
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));
|
---|
55 | }
|
---|
56 |
|
---|
57 | template<class Archive, class OStream>
|
---|
58 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
---|
59 | basic_binary_oprimitive<Archive, OStream>::save(const char * s)
|
---|
60 | {
|
---|
61 | std::size_t l = std::strlen(s);
|
---|
62 | this->This()->save(l);
|
---|
63 | save_binary(s, l);
|
---|
64 | }
|
---|
65 |
|
---|
66 | template<class Archive, class OStream>
|
---|
67 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
---|
68 | basic_binary_oprimitive<Archive, OStream>::save(const std::string &s)
|
---|
69 | {
|
---|
70 | std::size_t l = static_cast<unsigned int>(s.size());
|
---|
71 | this->This()->save(l);
|
---|
72 | save_binary(s.data(), l);
|
---|
73 | }
|
---|
74 |
|
---|
75 | #ifndef BOOST_NO_CWCHAR
|
---|
76 | template<class Archive, class OStream>
|
---|
77 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
---|
78 | basic_binary_oprimitive<Archive, OStream>::save(const wchar_t * ws)
|
---|
79 | {
|
---|
80 | std::size_t l = std::wcslen(ws);
|
---|
81 | this->This()->save(l);
|
---|
82 | save_binary(ws, l * sizeof(wchar_t) / sizeof(char));
|
---|
83 | }
|
---|
84 |
|
---|
85 | #ifndef BOOST_NO_STD_WSTRING
|
---|
86 | template<class Archive, class OStream>
|
---|
87 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
---|
88 | basic_binary_oprimitive<Archive, OStream>::save(const std::wstring &ws)
|
---|
89 | {
|
---|
90 | std::size_t l = ws.size();
|
---|
91 | this->This()->save(l);
|
---|
92 | save_binary(ws.data(), l * sizeof(wchar_t) / sizeof(char));
|
---|
93 | }
|
---|
94 | #endif
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | template<class Archive, class OStream>
|
---|
98 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
---|
99 | basic_binary_oprimitive<Archive, OStream>::basic_binary_oprimitive(
|
---|
100 | OStream &os_,
|
---|
101 | bool no_codecvt
|
---|
102 | ) :
|
---|
103 | os(os_),
|
---|
104 | archive_locale(NULL),
|
---|
105 | locale_saver(os)
|
---|
106 | {
|
---|
107 | if(! no_codecvt){
|
---|
108 | archive_locale.reset(
|
---|
109 | add_facet(
|
---|
110 | std::locale::classic(),
|
---|
111 | new codecvt_null<BOOST_DEDUCED_TYPENAME OStream::char_type>
|
---|
112 | )
|
---|
113 | );
|
---|
114 | os.imbue(* archive_locale);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | // scoped_ptr requires that g be a complete type at time of
|
---|
119 | // destruction so define destructor here rather than in the header
|
---|
120 | template<class Archive, class OStream>
|
---|
121 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
---|
122 | basic_binary_oprimitive<Archive, OStream>::~basic_binary_oprimitive(){
|
---|
123 | BOOST_TRY {
|
---|
124 | os.flush();
|
---|
125 | }
|
---|
126 | BOOST_CATCH(...){}
|
---|
127 | BOOST_CATCH_END
|
---|
128 | }
|
---|
129 |
|
---|
130 | } // namespace archive
|
---|
131 | } // namespace boost
|
---|