source: NonGTP/Boost/boost/archive/impl/basic_binary_iprimitive.ipp @ 857

Revision 857, 4.8 KB checked in by igarcia, 19 years ago (diff)
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 IStream>
39BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
40basic_binary_iprimitive<Archive, IStream>::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 IStream>
77BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
78basic_binary_iprimitive<Archive, IStream>::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 IStream>
87BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
88basic_binary_iprimitive<Archive, IStream>::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 IStream>
103BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
104basic_binary_iprimitive<Archive, IStream>::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 IStream>
115BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
116basic_binary_iprimitive<Archive, IStream>::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 IStream>
131BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
132basic_binary_iprimitive<Archive, IStream>::basic_binary_iprimitive(
133    IStream &is_,
134    bool no_codecvt
135) :
136    is(is_),
137    archive_locale(NULL),
138    locale_saver(is)
139{
140    if(! no_codecvt){
141        archive_locale.reset(
142            boost::archive::add_facet(
143                std::locale::classic(),
144                new codecvt_null<BOOST_DEDUCED_TYPENAME IStream::char_type>
145            )
146        );
147        is.imbue(* archive_locale);
148    }
149}
150
151// scoped_ptr requires that archive_locale be a complete type at time of
152// destruction so define destructor here rather than in the header
153template<class Archive, class IStream>
154BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
155basic_binary_iprimitive<Archive, IStream>::~basic_binary_iprimitive(){
156}
157
158} // namespace archive
159} // namespace boost
Note: See TracBrowser for help on using the repository browser.