fix doc example typo
[boost.git] / boost / archive / basic_binary_iprimitive.hpp
blob11f5b3890a38af37754a385be35dca4ac157e739
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)
6 # pragma once
7 #endif
9 #if defined(_MSC_VER)
10 #pragma warning( disable : 4800 )
11 #endif
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.
30 #include <iosfwd>
31 #include <cassert>
32 #include <locale>
33 #include <cstring> // std::memcpy
34 #include <cstddef> // std::size_t
35 #include <streambuf> // basic_streambuf
36 #include <string>
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
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
60 namespace boost {
61 namespace archive {
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;
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
76 Archive * This(){
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;
83 #endif
85 // main template for serilization of primitive types
86 template<class T>
87 void load(T & t){
88 load_binary(& t, sizeof(T));
91 /////////////////////////////////////////////////////////
92 // fundamental types that need special treatment
94 // trap usage of invalid uninitialized boolean
95 void load(bool & t){
96 load_binary(& t, sizeof(t));
97 int i = 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);
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);
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
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;
131 #else
132 struct apply : public boost::serialization::is_bitwise_serializable<T> {};
133 #endif
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));
143 void
144 load_binary(void *address, std::size_t count);
147 template<class Archive, class Elem, class Tr>
148 inline void
149 basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
150 void *address,
151 std::size_t count
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),
159 if(scount != s)
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);
165 if(0 < s){
166 // if(is.fail())
167 // boost::serialization::throw_exception(
168 // archive_exception(archive_exception::stream_error)
169 // );
170 Elem t;
171 scount = m_sb.sgetn(& t, 1);
172 if(scount != 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
181 } // namespace boost
183 #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
185 #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP