1 #ifndef BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP
2 #define BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP
4 // MS compatible compilers support #pragma once
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.
31 #include <boost/assert.hpp>
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/serialization/throw_exception.hpp>
48 #include <boost/integer.hpp>
49 #include <boost/integer_traits.hpp>
51 #include <boost/serialization/is_bitwise_serializable.hpp>
52 #include <boost/serialization/array_wrapper.hpp>
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
63 /////////////////////////////////////////////////////////////////////////////
64 // class binary_iarchive - read serialized objects from a input binary stream
65 template<class Archive, class Elem, class Tr>
66 class BOOST_SYMBOL_VISIBLE basic_binary_iprimitive {
67 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
68 friend class load_access;
73 std::basic_streambuf<Elem, Tr> & m_sb;
74 // return a pointer to the most derived class
76 return static_cast<Archive *>(this);
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;
92 // main template for serilization of primitive types
95 load_binary(& t, sizeof(T));
98 /////////////////////////////////////////////////////////
99 // fundamental types that need special treatment
101 // trap usage of invalid uninitialized boolean
103 load_binary(& t, sizeof(t));
105 BOOST_ASSERT(0 == i || 1 == i);
106 (void)i; // warning suppression for release builds.
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);
114 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
116 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
119 BOOST_ARCHIVE_OR_WARCHIVE_DECL void
121 BOOST_ARCHIVE_OR_WARCHIVE_DECL
122 basic_binary_iprimitive(
123 std::basic_streambuf<Elem, Tr> & sb,
126 BOOST_ARCHIVE_OR_WARCHIVE_DECL
127 ~basic_binary_iprimitive();
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 {
134 #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
136 typedef typename boost::serialization::is_bitwise_serializable< T >::type type;
139 struct apply : public boost::serialization::is_bitwise_serializable< T > {};
143 // the optimized load_array dispatches to load_binary
144 template <class ValueType>
145 void load_array(serialization::array_wrapper<ValueType>& a, unsigned int)
147 load_binary(a.address(),a.count()*sizeof(ValueType));
151 load_binary(void *address, std::size_t count);
154 template<class Archive, class Elem, class Tr>
156 basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
160 // note: an optimizer should eliminate the following for char files
162 static_cast<std::streamsize>(count / sizeof(Elem))
163 <= boost::integer_traits<std::streamsize>::const_max
165 std::streamsize s = static_cast<std::streamsize>(count / sizeof(Elem));
166 std::streamsize scount = m_sb.sgetn(
167 static_cast<Elem *>(address),
171 boost::serialization::throw_exception(
172 archive_exception(archive_exception::input_stream_error)
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));
179 // boost::serialization::throw_exception(
180 // archive_exception(archive_exception::stream_error)
183 scount = m_sb.sgetn(& t, 1);
185 boost::serialization::throw_exception(
186 archive_exception(archive_exception::input_stream_error)
188 std::memcpy(static_cast<char*>(address) + (count - s), &t, static_cast<std::size_t>(s));
192 } // namespace archive
195 #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
197 #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP