1 #ifndef BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP
2 #define BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 #pragma warning( disable : 4800 )
13 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
14 // basic_binary_iprimitive.hpp
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.
20 // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE
21 // ON PLATFORM APART FROM THE ONE THEY ARE CREATED ON
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)
28 // See http://www.boost.org for updates, documentation, and revision history.
33 #include <cstring> // std::memcpy
34 #include <cstddef> // std::size_t
35 #include <streambuf> // basic_streambuf
38 #include <boost/config.hpp>
39 #if defined(BOOST_NO_STDC_NAMESPACE)
46 #include <boost/cstdint.hpp>
47 #include <boost/scoped_ptr.hpp>
48 #include <boost/serialization/throw_exception.hpp>
49 //#include <boost/limits.hpp>
50 //#include <boost/io/ios_state.hpp>
52 #include <boost/archive/basic_streambuf_locale_saver.hpp>
53 #include <boost/archive/archive_exception.hpp>
54 #include <boost/archive/detail/auto_link_archive.hpp>
55 #include <boost/mpl/placeholders.hpp>
56 #include <boost/serialization/is_bitwise_serializable.hpp>
57 #include <boost/serialization/array.hpp>
58 #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
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
68 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
69 friend class load_access
;
74 std::basic_streambuf
<Elem
, Tr
> & m_sb
;
75 // return a pointer to the most derived class
77 return static_cast<Archive
*>(this);
80 #ifndef BOOST_NO_STD_LOCALE
81 boost::scoped_ptr
<std::locale
> archive_locale
;
82 basic_streambuf_locale_saver
<Elem
, Tr
> locale_saver
;
85 // main template for serilization of primitive types
88 load_binary(& t
, sizeof(T
));
91 /////////////////////////////////////////////////////////
92 // fundamental types that need special treatment
94 // trap usage of invalid uninitialized boolean
96 load_binary(& t
, sizeof(t
));
98 assert(0 == i
|| 1 == i
);
99 (void)i
; // warning suppression for release builds.
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
);
107 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
109 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
112 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
114 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
115 basic_binary_iprimitive(
116 std::basic_streambuf
<Elem
, Tr
> & sb
,
119 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
120 ~basic_binary_iprimitive();
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
{
127 #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
129 typedef BOOST_DEDUCED_TYPENAME
boost::serialization::is_bitwise_serializable
<T
>::type type
;
132 struct apply
: public boost::serialization::is_bitwise_serializable
<T
> {};
136 // the optimized load_array dispatches to load_binary
137 template <class ValueType
>
138 void load_array(serialization::array
<ValueType
>& a
, unsigned int)
140 load_binary(a
.address(),a
.count()*sizeof(ValueType
));
144 load_binary(void *address
, std::size_t count
);
147 template<class Archive
, class Elem
, class Tr
>
149 basic_binary_iprimitive
<Archive
, Elem
, Tr
>::load_binary(
153 // note: an optimizer should eliminate the following for char files
154 std::streamsize s
= count
/ sizeof(Elem
);
155 std::streamsize scount
= m_sb
.sgetn(
156 static_cast<Elem
*>(address
),
160 boost::serialization::throw_exception(
161 archive_exception(archive_exception::stream_error
)
163 // note: an optimizer should eliminate the following for char files
164 s
= count
% sizeof(Elem
);
167 // boost::serialization::throw_exception(
168 // archive_exception(archive_exception::stream_error)
171 scount
= m_sb
.sgetn(& t
, 1);
173 boost::serialization::throw_exception(
174 archive_exception(archive_exception::stream_error
)
176 std::memcpy(static_cast<char*>(address
) + (count
- s
), &t
, s
);
180 } // namespace archive
183 #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
185 #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP