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)
6# pragma once
7#endif
8
9#if defined(_MSC_VER)
10#pragma warning( disable : 4800 )
11#endif
12
13/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
14// basic_binary_iprimitive.hpp
15//
16// archives stored as native binary - this should be the fastest way
17// to archive the state of a group of objects. It makes no attempt to
18// convert to any canonical form.
19
20// IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE
21// ON PLATFORM APART FROM THE ONE THEY ARE CREATED ON
22
23// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
24// Use, modification and distribution is subject to the Boost Software
25// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
26// http://www.boost.org/LICENSE_1_0.txt)
27
28// See http://www.boost.org for updates, documentation, and revision history.
29
30#include <iosfwd>
31#include <boost/assert.hpp>
32#include <locale>
33#include <cstring> // std::memcpy
34#include <cstddef> // std::size_t
35#include <streambuf> // basic_streambuf
36#include <string>
37
38#include <boost/config.hpp>
39#if defined(BOOST_NO_STDC_NAMESPACE)
40namespace std{
41 using ::memcpy;
42 using ::size_t;
43} // namespace std
44#endif
45
46#include <boost/cstdint.hpp>
47#include <boost/serialization/throw_exception.hpp>
48#include <boost/integer.hpp>
49#include <boost/integer_traits.hpp>
50
51#include <boost/serialization/is_bitwise_serializable.hpp>
52#include <boost/serialization/array_wrapper.hpp>
53
54#include <boost/archive/basic_streambuf_locale_saver.hpp>
55#include <boost/archive/codecvt_null.hpp>
56#include <boost/archive/archive_exception.hpp>
57#include <boost/archive/detail/auto_link_archive.hpp>
58#include <boost/archive/detail/abi_prefix.hpp> // must be the last header
59
60namespace boost {
61namespace archive {
62
63/////////////////////////////////////////////////////////////////////////////
64// class binary_iarchive - read serialized objects from a input binary stream
65template<class Archive, class Elem, class Tr>
66class BOOST_SYMBOL_VISIBLE basic_binary_iprimitive {
67#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
68 friend class load_access;
69protected:
70#else
71public:
72#endif
73 std::basic_streambuf<Elem, Tr> & m_sb;
74 // return a pointer to the most derived class
75 Archive * This(){
76 return static_cast<Archive *>(this);
77 }
78
79 #ifndef BOOST_NO_STD_LOCALE
80 // note order! - if you change this, libstd++ will fail!
81 // a) create new locale with new codecvt facet
82 // b) save current locale
83 // c) change locale to new one
84 // d) use stream buffer
85 // e) change locale back to original
86 // f) destroy new codecvt facet
87 boost::archive::codecvt_null<Elem> codecvt_null_facet;
88 basic_streambuf_locale_saver<Elem, Tr> locale_saver;
89 std::locale archive_locale;
90 #endif
91
92 // main template for serialization of primitive types
93 template<class T>
94 void load(T & t){
95 load_binary(address: & t, count: sizeof(T));
96 }
97
98 /////////////////////////////////////////////////////////
99 // fundamental types that need special treatment
100
101 // trap usage of invalid uninitialized boolean
102 void load(bool & t){
103 load_binary(address: & t, count: sizeof(t));
104 int i = t;
105 BOOST_ASSERT(0 == i || 1 == i);
106 (void)i; // warning suppression for release builds.
107 }
108 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
109 load(std::string &s);
110 #ifndef BOOST_NO_STD_WSTRING
111 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
112 load(std::wstring &ws);
113 #endif
114 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
115 load(char * t);
116 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
117 load(wchar_t * t);
118
119 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
120 init();
121 BOOST_ARCHIVE_OR_WARCHIVE_DECL
122 basic_binary_iprimitive(
123 std::basic_streambuf<Elem, Tr> & sb,
124 bool no_codecvt
125 );
126 BOOST_ARCHIVE_OR_WARCHIVE_DECL
127 ~basic_binary_iprimitive();
128public:
129 // we provide an optimized load for all fundamental types
130 // typedef serialization::is_bitwise_serializable<mpl::_1>
131 // use_array_optimization;
132 struct use_array_optimization {
133 template <class T>
134 #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
135 struct apply {
136 typedef typename boost::serialization::is_bitwise_serializable< T >::type type;
137 };
138 #else
139 struct apply : public boost::serialization::is_bitwise_serializable< T > {};
140 #endif
141 };
142
143 // the optimized load_array dispatches to load_binary
144 template <class ValueType>
145 void load_array(serialization::array_wrapper<ValueType>& a, unsigned int)
146 {
147 load_binary(address: a.address(),count: a.count()*sizeof(ValueType));
148 }
149
150 void
151 load_binary(void *address, std::size_t count);
152};
153
154template<class Archive, class Elem, class Tr>
155inline void
156basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
157 void *address,
158 std::size_t count
159){
160 // note: an optimizer should eliminate the following for char files
161 BOOST_ASSERT(
162 static_cast<std::streamsize>(count / sizeof(Elem))
163 <= boost::integer_traits<std::streamsize>::const_max
164 );
165 std::streamsize s = static_cast<std::streamsize>(count / sizeof(Elem));
166 std::streamsize scount = m_sb.sgetn(
167 static_cast<Elem *>(address),
168 s
169 );
170 if(scount != s)
171 boost::serialization::throw_exception(
172 e: archive_exception(archive_exception::input_stream_error)
173 );
174 // note: an optimizer should eliminate the following for char files
175 BOOST_ASSERT(count % sizeof(Elem) <= boost::integer_traits<std::streamsize>::const_max);
176 s = static_cast<std::streamsize>(count % sizeof(Elem));
177 if(0 < s){
178// if(is.fail())
179// boost::serialization::throw_exception(
180// archive_exception(archive_exception::stream_error)
181// );
182 Elem t;
183 scount = m_sb.sgetn(& t, 1);
184 if(scount != 1)
185 boost::serialization::throw_exception(
186 e: archive_exception(archive_exception::input_stream_error)
187 );
188 std::memcpy(dest: static_cast<char*>(address) + (count - s), src: &t, n: static_cast<std::size_t>(s));
189 }
190}
191
192} // namespace archive
193} // namespace boost
194
195#include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
196
197#endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP
198

source code of boost/libs/serialization/include/boost/archive/basic_binary_iprimitive.hpp