Chris@16
|
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
2 // basic_binary_iprimitive.ipp:
|
Chris@16
|
3
|
Chris@16
|
4 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
|
Chris@16
|
5 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/assert.hpp>
|
Chris@16
|
12 #include <cstddef> // size_t, NULL
|
Chris@16
|
13 #include <cstring> // memcpy
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/config.hpp>
|
Chris@16
|
16 #if defined(BOOST_NO_STDC_NAMESPACE)
|
Chris@16
|
17 namespace std{
|
Chris@16
|
18 using ::size_t;
|
Chris@16
|
19 using ::memcpy;
|
Chris@16
|
20 } // namespace std
|
Chris@16
|
21 #endif
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/detail/workaround.hpp> // fixup for RogueWave
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/serialization/throw_exception.hpp>
|
Chris@16
|
26
|
Chris@101
|
27 #include <boost/core/no_exceptions_support.hpp>
|
Chris@16
|
28 #include <boost/archive/archive_exception.hpp>
|
Chris@16
|
29 #include <boost/archive/codecvt_null.hpp>
|
Chris@16
|
30 #include <boost/archive/add_facet.hpp>
|
Chris@101
|
31 #include <boost/archive/basic_binary_iprimitive.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost {
|
Chris@16
|
34 namespace archive {
|
Chris@16
|
35
|
Chris@16
|
36 //////////////////////////////////////////////////////////////////////
|
Chris@16
|
37 // implementation of basic_binary_iprimitive
|
Chris@16
|
38
|
Chris@16
|
39 template<class Archive, class Elem, class Tr>
|
Chris@16
|
40 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
41 basic_binary_iprimitive<Archive, Elem, Tr>::init()
|
Chris@16
|
42 {
|
Chris@16
|
43 // Detect attempts to pass native binary archives across
|
Chris@16
|
44 // incompatible platforms. This is not fool proof but its
|
Chris@16
|
45 // better than nothing.
|
Chris@16
|
46 unsigned char size;
|
Chris@16
|
47 this->This()->load(size);
|
Chris@16
|
48 if(sizeof(int) != size)
|
Chris@16
|
49 boost::serialization::throw_exception(
|
Chris@16
|
50 archive_exception(
|
Chris@16
|
51 archive_exception::incompatible_native_format,
|
Chris@16
|
52 "size of int"
|
Chris@16
|
53 )
|
Chris@16
|
54 );
|
Chris@16
|
55 this->This()->load(size);
|
Chris@16
|
56 if(sizeof(long) != size)
|
Chris@16
|
57 boost::serialization::throw_exception(
|
Chris@16
|
58 archive_exception(
|
Chris@16
|
59 archive_exception::incompatible_native_format,
|
Chris@16
|
60 "size of long"
|
Chris@16
|
61 )
|
Chris@16
|
62 );
|
Chris@16
|
63 this->This()->load(size);
|
Chris@16
|
64 if(sizeof(float) != size)
|
Chris@16
|
65 boost::serialization::throw_exception(
|
Chris@16
|
66 archive_exception(
|
Chris@16
|
67 archive_exception::incompatible_native_format,
|
Chris@16
|
68 "size of float"
|
Chris@16
|
69 )
|
Chris@16
|
70 );
|
Chris@16
|
71 this->This()->load(size);
|
Chris@16
|
72 if(sizeof(double) != size)
|
Chris@16
|
73 boost::serialization::throw_exception(
|
Chris@16
|
74 archive_exception(
|
Chris@16
|
75 archive_exception::incompatible_native_format,
|
Chris@16
|
76 "size of double"
|
Chris@16
|
77 )
|
Chris@16
|
78 );
|
Chris@16
|
79
|
Chris@16
|
80 // for checking endian
|
Chris@16
|
81 int i;
|
Chris@16
|
82 this->This()->load(i);
|
Chris@16
|
83 if(1 != i)
|
Chris@16
|
84 boost::serialization::throw_exception(
|
Chris@16
|
85 archive_exception(
|
Chris@16
|
86 archive_exception::incompatible_native_format,
|
Chris@16
|
87 "endian setting"
|
Chris@16
|
88 )
|
Chris@16
|
89 );
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 template<class Archive, class Elem, class Tr>
|
Chris@16
|
93 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
94 basic_binary_iprimitive<Archive, Elem, Tr>::load(wchar_t * ws)
|
Chris@16
|
95 {
|
Chris@16
|
96 std::size_t l; // number of wchar_t !!!
|
Chris@16
|
97 this->This()->load(l);
|
Chris@16
|
98 load_binary(ws, l * sizeof(wchar_t) / sizeof(char));
|
Chris@16
|
99 ws[l] = L'\0';
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 template<class Archive, class Elem, class Tr>
|
Chris@16
|
103 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
104 basic_binary_iprimitive<Archive, Elem, Tr>::load(std::string & s)
|
Chris@16
|
105 {
|
Chris@16
|
106 std::size_t l;
|
Chris@16
|
107 this->This()->load(l);
|
Chris@16
|
108 // borland de-allocator fixup
|
Chris@16
|
109 #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
|
Chris@16
|
110 if(NULL != s.data())
|
Chris@16
|
111 #endif
|
Chris@16
|
112 s.resize(l);
|
Chris@16
|
113 // note breaking a rule here - could be a problem on some platform
|
Chris@16
|
114 if(0 < l)
|
Chris@16
|
115 load_binary(&(*s.begin()), l);
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 #ifndef BOOST_NO_CWCHAR
|
Chris@16
|
119 template<class Archive, class Elem, class Tr>
|
Chris@16
|
120 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
121 basic_binary_iprimitive<Archive, Elem, Tr>::load(char * s)
|
Chris@16
|
122 {
|
Chris@16
|
123 std::size_t l;
|
Chris@16
|
124 this->This()->load(l);
|
Chris@16
|
125 load_binary(s, l);
|
Chris@16
|
126 s[l] = '\0';
|
Chris@16
|
127 }
|
Chris@16
|
128 #endif
|
Chris@16
|
129
|
Chris@16
|
130 #ifndef BOOST_NO_STD_WSTRING
|
Chris@16
|
131 template<class Archive, class Elem, class Tr>
|
Chris@16
|
132 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
133 basic_binary_iprimitive<Archive, Elem, Tr>::load(std::wstring & ws)
|
Chris@16
|
134 {
|
Chris@16
|
135 std::size_t l;
|
Chris@16
|
136 this->This()->load(l);
|
Chris@16
|
137 // borland de-allocator fixup
|
Chris@16
|
138 #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
|
Chris@16
|
139 if(NULL != ws.data())
|
Chris@16
|
140 #endif
|
Chris@16
|
141 ws.resize(l);
|
Chris@16
|
142 // note breaking a rule here - is could be a problem on some platform
|
Chris@16
|
143 load_binary(const_cast<wchar_t *>(ws.data()), l * sizeof(wchar_t) / sizeof(char));
|
Chris@16
|
144 }
|
Chris@16
|
145 #endif
|
Chris@16
|
146
|
Chris@16
|
147 template<class Archive, class Elem, class Tr>
|
Chris@16
|
148 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
Chris@16
|
149 basic_binary_iprimitive<Archive, Elem, Tr>::basic_binary_iprimitive(
|
Chris@16
|
150 std::basic_streambuf<Elem, Tr> & sb,
|
Chris@16
|
151 bool no_codecvt
|
Chris@16
|
152 ) :
|
Chris@16
|
153 #ifndef BOOST_NO_STD_LOCALE
|
Chris@16
|
154 m_sb(sb),
|
Chris@16
|
155 locale_saver(m_sb)
|
Chris@16
|
156 {
|
Chris@16
|
157 if(! no_codecvt){
|
Chris@16
|
158 archive_locale.reset(
|
Chris@101
|
159 add_facet(
|
Chris@16
|
160 std::locale::classic(),
|
Chris@16
|
161 new codecvt_null<Elem>
|
Chris@16
|
162 )
|
Chris@16
|
163 );
|
Chris@101
|
164 //m_sb.pubimbue(* archive_locale);
|
Chris@16
|
165 }
|
Chris@16
|
166 }
|
Chris@16
|
167 #else
|
Chris@16
|
168 m_sb(sb)
|
Chris@16
|
169 {}
|
Chris@16
|
170 #endif
|
Chris@16
|
171
|
Chris@16
|
172 // some libraries including stl and libcomo fail if the
|
Chris@16
|
173 // buffer isn't flushed before the code_cvt facet is changed.
|
Chris@16
|
174 // I think this is a bug. We explicity invoke sync to when
|
Chris@16
|
175 // we're done with the streambuf to work around this problem.
|
Chris@16
|
176 // Note that sync is a protected member of stream buff so we
|
Chris@16
|
177 // have to invoke it through a contrived derived class.
|
Chris@16
|
178 namespace detail {
|
Chris@16
|
179 // note: use "using" to get past msvc bug
|
Chris@16
|
180 using namespace std;
|
Chris@16
|
181 template<class Elem, class Tr>
|
Chris@16
|
182 class input_streambuf_access : public std::basic_streambuf<Elem, Tr> {
|
Chris@16
|
183 public:
|
Chris@16
|
184 virtual int sync(){
|
Chris@16
|
185 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
|
Chris@16
|
186 return this->basic_streambuf::sync();
|
Chris@16
|
187 #else
|
Chris@16
|
188 return this->basic_streambuf<Elem, Tr>::sync();
|
Chris@16
|
189 #endif
|
Chris@16
|
190 }
|
Chris@16
|
191 };
|
Chris@16
|
192 } // detail
|
Chris@16
|
193
|
Chris@16
|
194 // scoped_ptr requires that archive_locale be a complete type at time of
|
Chris@16
|
195 // destruction so define destructor here rather than in the header
|
Chris@16
|
196 template<class Archive, class Elem, class Tr>
|
Chris@16
|
197 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
Chris@16
|
198 basic_binary_iprimitive<Archive, Elem, Tr>::~basic_binary_iprimitive(){
|
Chris@16
|
199 // push back unread characters
|
Chris@16
|
200 //destructor can't throw !
|
Chris@101
|
201 BOOST_TRY{
|
Chris@16
|
202 static_cast<detail::input_streambuf_access<Elem, Tr> &>(m_sb).sync();
|
Chris@16
|
203 }
|
Chris@101
|
204 BOOST_CATCH(...){
|
Chris@16
|
205 }
|
Chris@101
|
206 BOOST_CATCH_END
|
Chris@16
|
207 }
|
Chris@16
|
208
|
Chris@16
|
209 } // namespace archive
|
Chris@16
|
210 } // namespace boost
|