Chris@16
|
1 #ifndef BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
|
Chris@16
|
2 #define BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
|
Chris@16
|
3
|
Chris@16
|
4 // MS compatible compilers support #pragma once
|
Chris@16
|
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
Chris@16
|
6 # pragma once
|
Chris@16
|
7 #endif
|
Chris@16
|
8
|
Chris@16
|
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
10 // basic_binary_oprimitive.hpp
|
Chris@16
|
11
|
Chris@16
|
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
|
Chris@16
|
13 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
15 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
16
|
Chris@16
|
17 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@16
|
18
|
Chris@16
|
19 // archives stored as native binary - this should be the fastest way
|
Chris@16
|
20 // to archive the state of a group of obects. It makes no attempt to
|
Chris@16
|
21 // convert to any canonical form.
|
Chris@16
|
22
|
Chris@16
|
23 // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE
|
Chris@16
|
24 // ON PLATFORM APART FROM THE ONE THEY ARE CREATE ON
|
Chris@16
|
25
|
Chris@16
|
26 #include <iosfwd>
|
Chris@16
|
27 #include <boost/assert.hpp>
|
Chris@16
|
28 #include <locale>
|
Chris@16
|
29 #include <streambuf> // basic_streambuf
|
Chris@16
|
30 #include <string>
|
Chris@16
|
31 #include <cstddef> // size_t
|
Chris@16
|
32
|
Chris@16
|
33 #include <boost/config.hpp>
|
Chris@16
|
34 #if defined(BOOST_NO_STDC_NAMESPACE)
|
Chris@16
|
35 namespace std{
|
Chris@16
|
36 using ::size_t;
|
Chris@16
|
37 } // namespace std
|
Chris@16
|
38 #endif
|
Chris@16
|
39
|
Chris@16
|
40 #include <boost/cstdint.hpp>
|
Chris@16
|
41 #include <boost/integer.hpp>
|
Chris@16
|
42 #include <boost/integer_traits.hpp>
|
Chris@16
|
43 #include <boost/scoped_ptr.hpp>
|
Chris@16
|
44 #include <boost/serialization/throw_exception.hpp>
|
Chris@16
|
45
|
Chris@16
|
46 #include <boost/archive/basic_streambuf_locale_saver.hpp>
|
Chris@16
|
47 #include <boost/archive/archive_exception.hpp>
|
Chris@16
|
48 #include <boost/serialization/is_bitwise_serializable.hpp>
|
Chris@16
|
49 #include <boost/mpl/placeholders.hpp>
|
Chris@16
|
50 #include <boost/serialization/array.hpp>
|
Chris@16
|
51 #include <boost/archive/detail/auto_link_archive.hpp>
|
Chris@16
|
52 #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
|
Chris@16
|
53
|
Chris@16
|
54 namespace boost {
|
Chris@16
|
55 namespace archive {
|
Chris@16
|
56
|
Chris@16
|
57 /////////////////////////////////////////////////////////////////////////
|
Chris@16
|
58 // class basic_binary_oprimitive - binary output of prmitives
|
Chris@16
|
59
|
Chris@16
|
60 template<class Archive, class Elem, class Tr>
|
Chris@16
|
61 class basic_binary_oprimitive
|
Chris@16
|
62 {
|
Chris@16
|
63 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
Chris@16
|
64 friend class save_access;
|
Chris@16
|
65 protected:
|
Chris@16
|
66 #else
|
Chris@16
|
67 public:
|
Chris@16
|
68 #endif
|
Chris@16
|
69 std::basic_streambuf<Elem, Tr> & m_sb;
|
Chris@16
|
70 // return a pointer to the most derived class
|
Chris@16
|
71 Archive * This(){
|
Chris@16
|
72 return static_cast<Archive *>(this);
|
Chris@16
|
73 }
|
Chris@16
|
74 #ifndef BOOST_NO_STD_LOCALE
|
Chris@16
|
75 boost::scoped_ptr<std::locale> archive_locale;
|
Chris@16
|
76 basic_streambuf_locale_saver<Elem, Tr> locale_saver;
|
Chris@16
|
77 #endif
|
Chris@16
|
78 // default saving of primitives.
|
Chris@16
|
79 template<class T>
|
Chris@16
|
80 void save(const T & t)
|
Chris@16
|
81 {
|
Chris@16
|
82 save_binary(& t, sizeof(T));
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 /////////////////////////////////////////////////////////
|
Chris@16
|
86 // fundamental types that need special treatment
|
Chris@16
|
87
|
Chris@16
|
88 // trap usage of invalid uninitialized boolean which would
|
Chris@16
|
89 // otherwise crash on load.
|
Chris@16
|
90 void save(const bool t){
|
Chris@16
|
91 BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
|
Chris@16
|
92 save_binary(& t, sizeof(t));
|
Chris@16
|
93 }
|
Chris@16
|
94 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
95 save(const std::string &s);
|
Chris@16
|
96 #ifndef BOOST_NO_STD_WSTRING
|
Chris@16
|
97 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
98 save(const std::wstring &ws);
|
Chris@16
|
99 #endif
|
Chris@16
|
100 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
101 save(const char * t);
|
Chris@16
|
102 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
103 save(const wchar_t * t);
|
Chris@16
|
104
|
Chris@16
|
105 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
106 init();
|
Chris@16
|
107
|
Chris@16
|
108 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
Chris@16
|
109 basic_binary_oprimitive(
|
Chris@16
|
110 std::basic_streambuf<Elem, Tr> & sb,
|
Chris@16
|
111 bool no_codecvt
|
Chris@16
|
112 );
|
Chris@16
|
113 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
Chris@16
|
114 ~basic_binary_oprimitive();
|
Chris@16
|
115 public:
|
Chris@16
|
116
|
Chris@16
|
117 // we provide an optimized save for all fundamental types
|
Chris@16
|
118 // typedef serialization::is_bitwise_serializable<mpl::_1>
|
Chris@16
|
119 // use_array_optimization;
|
Chris@16
|
120 // workaround without using mpl lambdas
|
Chris@16
|
121 struct use_array_optimization {
|
Chris@16
|
122 template <class T>
|
Chris@16
|
123 #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
|
Chris@16
|
124 struct apply {
|
Chris@16
|
125 typedef BOOST_DEDUCED_TYPENAME boost::serialization::is_bitwise_serializable< T >::type type;
|
Chris@16
|
126 };
|
Chris@16
|
127 #else
|
Chris@16
|
128 struct apply : public boost::serialization::is_bitwise_serializable< T > {};
|
Chris@16
|
129 #endif
|
Chris@16
|
130 };
|
Chris@16
|
131
|
Chris@16
|
132
|
Chris@16
|
133 // the optimized save_array dispatches to save_binary
|
Chris@16
|
134 template <class ValueType>
|
Chris@16
|
135 void save_array(boost::serialization::array<ValueType> const& a, unsigned int)
|
Chris@16
|
136 {
|
Chris@16
|
137 save_binary(a.address(),a.count()*sizeof(ValueType));
|
Chris@16
|
138 }
|
Chris@16
|
139
|
Chris@16
|
140 void save_binary(const void *address, std::size_t count);
|
Chris@16
|
141 };
|
Chris@16
|
142
|
Chris@16
|
143 template<class Archive, class Elem, class Tr>
|
Chris@16
|
144 inline void
|
Chris@16
|
145 basic_binary_oprimitive<Archive, Elem, Tr>::save_binary(
|
Chris@16
|
146 const void *address,
|
Chris@16
|
147 std::size_t count
|
Chris@16
|
148 ){
|
Chris@16
|
149 //BOOST_ASSERT(
|
Chris@16
|
150 // static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)()) >= count
|
Chris@16
|
151 //);
|
Chris@16
|
152 // note: if the following assertions fail
|
Chris@16
|
153 // a likely cause is that the output stream is set to "text"
|
Chris@16
|
154 // mode where by cr characters recieve special treatment.
|
Chris@16
|
155 // be sure that the output stream is opened with ios::binary
|
Chris@16
|
156 //if(os.fail())
|
Chris@16
|
157 // boost::serialization::throw_exception(
|
Chris@16
|
158 // archive_exception(archive_exception::output_stream_error)
|
Chris@16
|
159 // );
|
Chris@16
|
160 // figure number of elements to output - round up
|
Chris@16
|
161 count = ( count + sizeof(Elem) - 1)
|
Chris@16
|
162 / sizeof(Elem);
|
Chris@16
|
163 BOOST_ASSERT(count <= std::size_t(boost::integer_traits<std::streamsize>::const_max));
|
Chris@16
|
164 std::streamsize scount = m_sb.sputn(
|
Chris@16
|
165 static_cast<const Elem *>(address),
|
Chris@16
|
166 static_cast<std::streamsize>(count)
|
Chris@16
|
167 );
|
Chris@16
|
168 if(count != static_cast<std::size_t>(scount))
|
Chris@16
|
169 boost::serialization::throw_exception(
|
Chris@16
|
170 archive_exception(archive_exception::output_stream_error)
|
Chris@16
|
171 );
|
Chris@16
|
172 //os.write(
|
Chris@16
|
173 // static_cast<const BOOST_DEDUCED_TYPENAME OStream::char_type *>(address),
|
Chris@16
|
174 // count
|
Chris@16
|
175 //);
|
Chris@16
|
176 //BOOST_ASSERT(os.good());
|
Chris@16
|
177 }
|
Chris@16
|
178
|
Chris@16
|
179 } //namespace boost
|
Chris@16
|
180 } //namespace archive
|
Chris@16
|
181
|
Chris@16
|
182 #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
|
Chris@16
|
183
|
Chris@16
|
184 #endif // BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
|