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 #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 obects. 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)
40 namespace std{
41 using ::memcpy;
42 using ::size_t;
43 } // namespace std
44 #endif
45
46 #include <boost/cstdint.hpp>
47 #include <boost/scoped_ptr.hpp>
48 #include <boost/serialization/throw_exception.hpp>
49 #include <boost/integer.hpp>
50 #include <boost/integer_traits.hpp>
51
52 #include <boost/archive/basic_streambuf_locale_saver.hpp>
53 #include <boost/archive/archive_exception.hpp>
54 #include <boost/mpl/placeholders.hpp>
55 #include <boost/serialization/is_bitwise_serializable.hpp>
56 #include <boost/serialization/array.hpp>
57 #include <boost/archive/detail/auto_link_archive.hpp>
58 #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
59
60 namespace boost {
61 namespace archive {
62
63 /////////////////////////////////////////////////////////////////////////////
64 // class binary_iarchive - read serialized objects from a input binary stream
65 template<class Archive, class Elem, class Tr>
66 class basic_binary_iprimitive
67 {
68 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
69 friend class load_access;
70 protected:
71 #else
72 public:
73 #endif
74 std::basic_streambuf<Elem, Tr> & m_sb;
75 // return a pointer to the most derived class
This()76 Archive * This(){
77 return static_cast<Archive *>(this);
78 }
79
80 #ifndef BOOST_NO_STD_LOCALE
81 boost::scoped_ptr<std::locale> archive_locale;
82 basic_streambuf_locale_saver<Elem, Tr> locale_saver;
83 #endif
84
85 // main template for serilization of primitive types
86 template<class T>
load(T & t)87 void load(T & t){
88 load_binary(& t, sizeof(T));
89 }
90
91 /////////////////////////////////////////////////////////
92 // fundamental types that need special treatment
93
94 // trap usage of invalid uninitialized boolean
load(bool & t)95 void load(bool & t){
96 load_binary(& t, sizeof(t));
97 int i = t;
98 BOOST_ASSERT(0 == i || 1 == i);
99 (void)i; // warning suppression for release builds.
100 }
101 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
102 load(std::string &s);
103 #ifndef BOOST_NO_STD_WSTRING
104 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
105 load(std::wstring &ws);
106 #endif
107 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
108 load(char * t);
109 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
110 load(wchar_t * t);
111
112 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
113 init();
114 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
115 basic_binary_iprimitive(
116 std::basic_streambuf<Elem, Tr> & sb,
117 bool no_codecvt
118 );
119 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
120 ~basic_binary_iprimitive();
121 public:
122 // we provide an optimized load for all fundamental types
123 // typedef serialization::is_bitwise_serializable<mpl::_1>
124 // use_array_optimization;
125 struct use_array_optimization {
126 template <class T>
127 #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
128 struct apply {
129 typedef BOOST_DEDUCED_TYPENAME boost::serialization::is_bitwise_serializable< T >::type type;
130 };
131 #else
132 struct apply : public boost::serialization::is_bitwise_serializable< T > {};
133 #endif
134 };
135
136 // the optimized load_array dispatches to load_binary
137 template <class ValueType>
load_array(serialization::array<ValueType> & a,unsigned int)138 void load_array(serialization::array<ValueType>& a, unsigned int)
139 {
140 load_binary(a.address(),a.count()*sizeof(ValueType));
141 }
142
143 void
144 load_binary(void *address, std::size_t count);
145 };
146
147 template<class Archive, class Elem, class Tr>
148 inline void
load_binary(void * address,std::size_t count)149 basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
150 void *address,
151 std::size_t count
152 ){
153 // note: an optimizer should eliminate the following for char files
154 BOOST_ASSERT(
155 static_cast<std::streamsize>(count / sizeof(Elem))
156 <= boost::integer_traits<std::streamsize>::const_max
157 );
158 std::streamsize s = static_cast<std::streamsize>(count / sizeof(Elem));
159 std::streamsize scount = m_sb.sgetn(
160 static_cast<Elem *>(address),
161 s
162 );
163 if(scount != s)
164 boost::serialization::throw_exception(
165 archive_exception(archive_exception::input_stream_error)
166 );
167 // note: an optimizer should eliminate the following for char files
168 BOOST_ASSERT(count % sizeof(Elem) <= boost::integer_traits<std::streamsize>::const_max);
169 s = static_cast<std::streamsize>(count % sizeof(Elem));
170 if(0 < s){
171 // if(is.fail())
172 // boost::serialization::throw_exception(
173 // archive_exception(archive_exception::stream_error)
174 // );
175 Elem t;
176 scount = m_sb.sgetn(& t, 1);
177 if(scount != 1)
178 boost::serialization::throw_exception(
179 archive_exception(archive_exception::input_stream_error)
180 );
181 std::memcpy(static_cast<char*>(address) + (count - s), &t, s);
182 }
183 }
184
185 } // namespace archive
186 } // namespace boost
187
188 #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
189
190 #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP
191