1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// basic_binary_iprimitive.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 <boost/assert.hpp>
12#include <cstddef> // size_t, NULL
13#include <cstring> // memcpy
14
15#include <boost/config.hpp>
16#if defined(BOOST_NO_STDC_NAMESPACE)
17namespace std{
18    using ::size_t;
19    using ::memcpy;
20} // namespace std
21#endif
22
23#include <boost/serialization/throw_exception.hpp>
24#include <boost/core/no_exceptions_support.hpp>
25#include <boost/archive/archive_exception.hpp>
26#include <boost/archive/basic_binary_iprimitive.hpp>
27
28namespace boost {
29namespace archive {
30
31//////////////////////////////////////////////////////////////////////
32// implementation of basic_binary_iprimitive
33
34template<class Archive, class Elem, class Tr>
35BOOST_ARCHIVE_OR_WARCHIVE_DECL void
36basic_binary_iprimitive<Archive, Elem, Tr>::init()
37{
38    // Detect  attempts to pass native binary archives across
39    // incompatible platforms. This is not fool proof but its
40    // better than nothing.
41    unsigned char size;
42    this->This()->load(size);
43    if(sizeof(int) != size)
44        boost::serialization::throw_exception(
45            archive_exception(
46                archive_exception::incompatible_native_format,
47                "size of int"
48            )
49        );
50    this->This()->load(size);
51    if(sizeof(long) != size)
52        boost::serialization::throw_exception(
53            archive_exception(
54                archive_exception::incompatible_native_format,
55                "size of long"
56            )
57        );
58    this->This()->load(size);
59    if(sizeof(float) != size)
60        boost::serialization::throw_exception(
61            archive_exception(
62                archive_exception::incompatible_native_format,
63                "size of float"
64            )
65        );
66    this->This()->load(size);
67    if(sizeof(double) != size)
68        boost::serialization::throw_exception(
69            archive_exception(
70                archive_exception::incompatible_native_format,
71                "size of double"
72            )
73        );
74
75    // for checking endian
76    int i;
77    this->This()->load(i);
78    if(1 != i)
79        boost::serialization::throw_exception(
80            archive_exception(
81                archive_exception::incompatible_native_format,
82                "endian setting"
83            )
84        );
85}
86
87#ifndef BOOST_NO_CWCHAR
88#ifndef BOOST_NO_INTRINSIC_WCHAR_T
89template<class Archive, class Elem, class Tr>
90BOOST_ARCHIVE_OR_WARCHIVE_DECL void
91basic_binary_iprimitive<Archive, Elem, Tr>::load(wchar_t * ws)
92{
93    std::size_t l; // number of wchar_t !!!
94    this->This()->load(l);
95    load_binary(ws, l * sizeof(wchar_t) / sizeof(char));
96    ws[l] = L'\0';
97}
98#endif
99#endif
100
101template<class Archive, class Elem, class Tr>
102BOOST_ARCHIVE_OR_WARCHIVE_DECL void
103basic_binary_iprimitive<Archive, Elem, Tr>::load(std::string & s)
104{
105    std::size_t l;
106    this->This()->load(l);
107    // borland de-allocator fixup
108    #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
109    if(NULL != s.data())
110    #endif
111        s.resize(l);
112    // note breaking a rule here - could be a problem on some platform
113    if(0 < l)
114        load_binary(&(*s.begin()), l);
115}
116
117template<class Archive, class Elem, class Tr>
118BOOST_ARCHIVE_OR_WARCHIVE_DECL void
119basic_binary_iprimitive<Archive, Elem, Tr>::load(char * s)
120{
121    std::size_t l;
122    this->This()->load(l);
123    load_binary(s, l);
124    s[l] = '\0';
125}
126
127#ifndef BOOST_NO_STD_WSTRING
128template<class Archive, class Elem, class Tr>
129BOOST_ARCHIVE_OR_WARCHIVE_DECL void
130basic_binary_iprimitive<Archive, Elem, Tr>::load(std::wstring & ws)
131{
132    std::size_t l;
133    this->This()->load(l);
134    // borland de-allocator fixup
135    #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
136    if(NULL != ws.data())
137    #endif
138        ws.resize(l);
139    // note breaking a rule here - is could be a problem on some platform
140    load_binary(const_cast<wchar_t *>(ws.data()), l * sizeof(wchar_t) / sizeof(char));
141}
142#endif
143
144template<class Archive, class Elem, class Tr>
145BOOST_ARCHIVE_OR_WARCHIVE_DECL
146basic_binary_iprimitive<Archive, Elem, Tr>::basic_binary_iprimitive(
147    std::basic_streambuf<Elem, Tr> & sb,
148    bool no_codecvt
149) :
150#ifndef BOOST_NO_STD_LOCALE
151    m_sb(sb),
152    codecvt_null_facet(1),
153    locale_saver(m_sb),
154    archive_locale(sb.getloc(), & codecvt_null_facet)
155{
156    if(! no_codecvt){
157        m_sb.pubsync();
158        m_sb.pubimbue(archive_locale);
159    }
160}
161#else
162    m_sb(sb)
163{}
164#endif
165
166// scoped_ptr requires that g be a complete type at time of
167// destruction so define destructor here rather than in the header
168template<class Archive, class Elem, class Tr>
169BOOST_ARCHIVE_OR_WARCHIVE_DECL
170basic_binary_iprimitive<Archive, Elem, Tr>::~basic_binary_iprimitive(){}
171
172} // namespace archive
173} // namespace boost
174