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