Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/archive/impl/basic_binary_iprimitive.ipp @ 47

Last change on this file since 47 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 5.7 KB
Line 
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 <cassert>
12#include <cstddef> // size_t
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/detail/workaround.hpp> // fixup for RogueWave
24
25#include <boost/throw_exception.hpp>
26#include <boost/scoped_ptr.hpp>
27
28#include <boost/archive/archive_exception.hpp>
29#include <boost/archive/codecvt_null.hpp>
30#include <boost/archive/add_facet.hpp>
31
32namespace boost {
33namespace archive {
34
35//////////////////////////////////////////////////////////////////////
36// implementation of basic_binary_iprimitive
37
38template<class Archive, class Elem, class Tr>
39BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
40basic_binary_iprimitive<Archive, Elem, Tr>::init()
41{
42    // Detect  attempts to pass native binary archives across
43    // incompatible platforms. This is not fool proof but its
44    // better than nothing.
45    unsigned char size;
46    this->This()->load(size);
47    if(sizeof(int) != size)
48        boost::throw_exception(
49            archive_exception(archive_exception::incompatible_native_format)
50        );
51    this->This()->load(size);
52    if(sizeof(long) != size)
53        boost::throw_exception(
54            archive_exception(archive_exception::incompatible_native_format)
55        );
56    this->This()->load(size);
57    if(sizeof(float) != size)
58        boost::throw_exception(
59            archive_exception(archive_exception::incompatible_native_format)
60        );
61    this->This()->load(size);
62    if(sizeof(double) != size)
63        boost::throw_exception(
64            archive_exception(archive_exception::incompatible_native_format)
65        );
66
67    // for checking endian
68    int i;
69    this->This()->load(i);
70    if(1 != i)
71        boost::throw_exception(
72            archive_exception(archive_exception::incompatible_native_format)
73        );
74}
75
76template<class Archive, class Elem, class Tr>
77BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
78basic_binary_iprimitive<Archive, Elem, Tr>::load(wchar_t * ws)
79{
80    std::size_t l;
81    this->This()->load(l);
82    load_binary(ws, l);
83    ws[l / sizeof(wchar_t)] = L'\0';
84}
85
86template<class Archive, class Elem, class Tr>
87BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
88basic_binary_iprimitive<Archive, Elem, Tr>::load(std::string & s)
89{
90    std::size_t l;
91    this->This()->load(l);
92    // borland de-allocator fixup
93    #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
94    if(NULL != s.data())
95    #endif
96        s.resize(l);
97    // note breaking a rule here - could be a problem on some platform
98    load_binary(const_cast<char *>(s.data()), l);
99}
100
101#ifndef BOOST_NO_CWCHAR
102template<class Archive, class Elem, class Tr>
103BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
104basic_binary_iprimitive<Archive, Elem, Tr>::load(char * s)
105{
106    std::size_t l;
107    this->This()->load(l);
108    load_binary(s, l);
109    s[l] = '\0';
110}
111#endif
112
113#ifndef BOOST_NO_STD_WSTRING
114template<class Archive, class Elem, class Tr>
115BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
116basic_binary_iprimitive<Archive, Elem, Tr>::load(std::wstring & ws)
117{
118    std::size_t l;
119    this->This()->load(l);
120    // borland de-allocator fixup
121    #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
122    if(NULL != ws.data())
123    #endif
124        ws.resize(l);
125    // note breaking a rule here - is could be a problem on some platform
126    load_binary(const_cast<wchar_t *>(ws.data()), l * sizeof(wchar_t) / sizeof(char));
127}
128#endif
129
130template<class Archive, class Elem, class Tr>
131BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
132basic_binary_iprimitive<Archive, Elem, Tr>::basic_binary_iprimitive(
133    std::basic_streambuf<Elem, Tr> & sb,
134    bool no_codecvt
135) :
136    m_sb(sb),
137    archive_locale(NULL),
138    locale_saver(m_sb)
139{
140    if(! no_codecvt){
141        archive_locale.reset(
142            boost::archive::add_facet(
143                std::locale::classic(),
144                new codecvt_null<Elem>
145            )
146        );
147        m_sb.pubimbue(* archive_locale);
148    }
149}
150
151// some libraries including stl and libcomo fail if the
152// buffer isn't flushed before the code_cvt facet is changed.
153// I think this is a bug.  We explicity invoke sync to when
154// we're done with the streambuf to work around this problem.
155// Note that sync is a protected member of stream buff so we
156// have to invoke it through a contrived derived class.
157namespace detail {
158// note: use "using" to get past msvc bug
159using namespace std;
160template<class Elem, class Tr>
161class input_streambuf_access : public std::basic_streambuf<Elem, Tr> {
162    public:
163        virtual int sync(){
164#if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
165            return this->basic_streambuf::sync();
166#else
167            return this->basic_streambuf<Elem, Tr>::sync();
168#endif
169        }
170};
171} // detail
172
173// scoped_ptr requires that archive_locale be a complete type at time of
174// destruction so define destructor here rather than in the header
175template<class Archive, class Elem, class Tr>
176BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
177basic_binary_iprimitive<Archive, Elem, Tr>::~basic_binary_iprimitive(){
178    // push back unread characters
179    int result = static_cast<detail::input_streambuf_access<Elem, Tr> &>(
180        m_sb
181    ).sync();
182    if(0 != result){
183        boost::throw_exception(
184            archive_exception(archive_exception::stream_error)
185        );
186    }
187}
188
189} // namespace archive
190} // namespace boost
Note: See TracBrowser for help on using the repository browser.