1 | #ifndef BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP |
---|
2 | #define BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP |
---|
3 | |
---|
4 | // MS compatible compilers support #pragma once |
---|
5 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
---|
6 | # pragma once |
---|
7 | #endif |
---|
8 | |
---|
9 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
---|
10 | // basic_binary_iprimitive.hpp |
---|
11 | // |
---|
12 | // archives stored as native binary - this should be the fastest way |
---|
13 | // to archive the state of a group of obects. It makes no attempt to |
---|
14 | // convert to any canonical form. |
---|
15 | |
---|
16 | // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE |
---|
17 | // ON PLATFORM APART FROM THE ONE THEY ARE CREATED ON |
---|
18 | |
---|
19 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
---|
20 | // Use, modification and distribution is subject to the Boost Software |
---|
21 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
22 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
23 | |
---|
24 | // See http://www.boost.org for updates, documentation, and revision history. |
---|
25 | |
---|
26 | #include <iosfwd> |
---|
27 | #include <cassert> |
---|
28 | #include <locale> |
---|
29 | #include <cstring> // std::memcpy |
---|
30 | #include <cstddef> // std::size_t |
---|
31 | #include <streambuf> // basic_streambuf |
---|
32 | #include <string> |
---|
33 | |
---|
34 | #include <boost/config.hpp> |
---|
35 | #if defined(BOOST_NO_STDC_NAMESPACE) |
---|
36 | namespace std{ |
---|
37 | using ::memcpy; |
---|
38 | using ::size_t; |
---|
39 | } // namespace std |
---|
40 | #endif |
---|
41 | |
---|
42 | #include <boost/cstdint.hpp> |
---|
43 | #include <boost/scoped_ptr.hpp> |
---|
44 | #include <boost/throw_exception.hpp> |
---|
45 | //#include <boost/limits.hpp> |
---|
46 | //#include <boost/io/ios_state.hpp> |
---|
47 | |
---|
48 | #include <boost/archive/basic_streambuf_locale_saver.hpp> |
---|
49 | #include <boost/archive/archive_exception.hpp> |
---|
50 | #include <boost/archive/detail/auto_link_archive.hpp> |
---|
51 | #include <boost/archive/detail/abi_prefix.hpp> // must be the last header |
---|
52 | |
---|
53 | namespace boost { |
---|
54 | namespace archive { |
---|
55 | |
---|
56 | ///////////////////////////////////////////////////////////////////////////// |
---|
57 | // class binary_iarchive - read serialized objects from a input binary stream |
---|
58 | template<class Archive, class Elem, class Tr> |
---|
59 | class basic_binary_iprimitive |
---|
60 | { |
---|
61 | #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
---|
62 | friend class load_access; |
---|
63 | protected: |
---|
64 | #else |
---|
65 | public: |
---|
66 | #endif |
---|
67 | std::basic_streambuf<Elem, Tr> & m_sb; |
---|
68 | // return a pointer to the most derived class |
---|
69 | Archive * This(){ |
---|
70 | return static_cast<Archive *>(this); |
---|
71 | } |
---|
72 | boost::scoped_ptr<std::locale> archive_locale; |
---|
73 | basic_streambuf_locale_saver<Elem, Tr> locale_saver; |
---|
74 | |
---|
75 | // main template for serilization of primitive types |
---|
76 | template<class T> |
---|
77 | void load(T & t){ |
---|
78 | load_binary(& t, sizeof(T)); |
---|
79 | } |
---|
80 | |
---|
81 | ///////////////////////////////////////////////////////// |
---|
82 | // fundamental types that need special treatment |
---|
83 | |
---|
84 | // trap usage of invalid uninitialized boolean |
---|
85 | void load(bool & t){ |
---|
86 | load_binary(& t, sizeof(t)); |
---|
87 | int i = t; |
---|
88 | assert(0 == i || 1 == i); |
---|
89 | } |
---|
90 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
91 | load(std::string &s); |
---|
92 | #ifndef BOOST_NO_STD_WSTRING |
---|
93 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
94 | load(std::wstring &ws); |
---|
95 | #endif |
---|
96 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
97 | load(char * t); |
---|
98 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
99 | load(wchar_t * t); |
---|
100 | |
---|
101 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
102 | init(); |
---|
103 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) |
---|
104 | basic_binary_iprimitive( |
---|
105 | std::basic_streambuf<Elem, Tr> & sb, |
---|
106 | bool no_codecvt |
---|
107 | ); |
---|
108 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) |
---|
109 | ~basic_binary_iprimitive(); |
---|
110 | public: |
---|
111 | void |
---|
112 | load_binary(void *address, std::size_t count); |
---|
113 | }; |
---|
114 | |
---|
115 | template<class Archive, class Elem, class Tr> |
---|
116 | inline void |
---|
117 | basic_binary_iprimitive<Archive, Elem, Tr>::load_binary( |
---|
118 | void *address, |
---|
119 | std::size_t count |
---|
120 | ){ |
---|
121 | #if 0 |
---|
122 | assert( |
---|
123 | static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)()) >= count |
---|
124 | ); |
---|
125 | //if(is.fail()) |
---|
126 | // boost::throw_exception(archive_exception(archive_exception::stream_error)); |
---|
127 | // note: an optimizer should eliminate the following for char files |
---|
128 | std::size_t s = count / sizeof(BOOST_DEDUCED_TYPENAME IStream::char_type); |
---|
129 | is.read( |
---|
130 | static_cast<BOOST_DEDUCED_TYPENAME IStream::char_type *>(address), |
---|
131 | s |
---|
132 | ); |
---|
133 | // note: an optimizer should eliminate the following for char files |
---|
134 | s = count % sizeof(BOOST_DEDUCED_TYPENAME IStream::char_type); |
---|
135 | if(0 < s){ |
---|
136 | if(is.fail()) |
---|
137 | boost::throw_exception(archive_exception(archive_exception::stream_error)); |
---|
138 | BOOST_DEDUCED_TYPENAME IStream::char_type t; |
---|
139 | is.read(& t, 1); |
---|
140 | std::memcpy(address, &t, s); |
---|
141 | } |
---|
142 | #endif |
---|
143 | // note: an optimizer should eliminate the following for char files |
---|
144 | std::streamsize s = count / sizeof(Elem); |
---|
145 | std::streamsize scount = m_sb.sgetn( |
---|
146 | static_cast<Elem *>(address), |
---|
147 | s |
---|
148 | ); |
---|
149 | if(count != static_cast<std::size_t>(s)) |
---|
150 | boost::throw_exception( |
---|
151 | archive_exception(archive_exception::stream_error) |
---|
152 | ); |
---|
153 | // note: an optimizer should eliminate the following for char files |
---|
154 | s = count % sizeof(Elem); |
---|
155 | if(0 < s){ |
---|
156 | // if(is.fail()) |
---|
157 | // boost::throw_exception(archive_exception(archive_exception::stream_error)); |
---|
158 | Elem t; |
---|
159 | scount = m_sb.sgetn(& t, 1); |
---|
160 | if(count != 1) |
---|
161 | boost::throw_exception( |
---|
162 | archive_exception(archive_exception::stream_error) |
---|
163 | ); |
---|
164 | std::memcpy(address, &t, s); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | } // namespace archive |
---|
169 | } // namespace boost |
---|
170 | |
---|
171 | #include <boost/archive/detail/abi_suffix.hpp> // pop pragams |
---|
172 | |
---|
173 | #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP |
---|