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@101
|
5 #if defined(_MSC_VER)
|
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@101
|
57 template<class Ch>
|
Chris@101
|
58 class codecvt_null;
|
Chris@101
|
59
|
Chris@16
|
60 /////////////////////////////////////////////////////////////////////////
|
Chris@16
|
61 // class basic_binary_oprimitive - binary output of prmitives
|
Chris@16
|
62
|
Chris@16
|
63 template<class Archive, class Elem, class Tr>
|
Chris@101
|
64 class basic_binary_oprimitive {
|
Chris@16
|
65 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
Chris@16
|
66 friend class save_access;
|
Chris@16
|
67 protected:
|
Chris@16
|
68 #else
|
Chris@16
|
69 public:
|
Chris@16
|
70 #endif
|
Chris@16
|
71 std::basic_streambuf<Elem, Tr> & m_sb;
|
Chris@16
|
72 // return a pointer to the most derived class
|
Chris@16
|
73 Archive * This(){
|
Chris@16
|
74 return static_cast<Archive *>(this);
|
Chris@16
|
75 }
|
Chris@16
|
76 #ifndef BOOST_NO_STD_LOCALE
|
Chris@101
|
77 boost::scoped_ptr<codecvt_null<Elem> > codecvt_facet;
|
Chris@16
|
78 boost::scoped_ptr<std::locale> archive_locale;
|
Chris@16
|
79 basic_streambuf_locale_saver<Elem, Tr> locale_saver;
|
Chris@16
|
80 #endif
|
Chris@16
|
81 // default saving of primitives.
|
Chris@16
|
82 template<class T>
|
Chris@16
|
83 void save(const T & t)
|
Chris@16
|
84 {
|
Chris@16
|
85 save_binary(& t, sizeof(T));
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 /////////////////////////////////////////////////////////
|
Chris@16
|
89 // fundamental types that need special treatment
|
Chris@16
|
90
|
Chris@16
|
91 // trap usage of invalid uninitialized boolean which would
|
Chris@16
|
92 // otherwise crash on load.
|
Chris@16
|
93 void save(const bool t){
|
Chris@16
|
94 BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
|
Chris@16
|
95 save_binary(& t, sizeof(t));
|
Chris@16
|
96 }
|
Chris@16
|
97 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
98 save(const std::string &s);
|
Chris@16
|
99 #ifndef BOOST_NO_STD_WSTRING
|
Chris@16
|
100 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
101 save(const std::wstring &ws);
|
Chris@16
|
102 #endif
|
Chris@16
|
103 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
104 save(const char * t);
|
Chris@16
|
105 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
106 save(const wchar_t * t);
|
Chris@16
|
107
|
Chris@16
|
108 BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
|
Chris@16
|
109 init();
|
Chris@16
|
110
|
Chris@16
|
111 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
Chris@16
|
112 basic_binary_oprimitive(
|
Chris@16
|
113 std::basic_streambuf<Elem, Tr> & sb,
|
Chris@16
|
114 bool no_codecvt
|
Chris@16
|
115 );
|
Chris@16
|
116 BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
|
Chris@16
|
117 ~basic_binary_oprimitive();
|
Chris@16
|
118 public:
|
Chris@16
|
119
|
Chris@16
|
120 // we provide an optimized save for all fundamental types
|
Chris@16
|
121 // typedef serialization::is_bitwise_serializable<mpl::_1>
|
Chris@16
|
122 // use_array_optimization;
|
Chris@16
|
123 // workaround without using mpl lambdas
|
Chris@16
|
124 struct use_array_optimization {
|
Chris@16
|
125 template <class T>
|
Chris@16
|
126 #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
|
Chris@16
|
127 struct apply {
|
Chris@101
|
128 typedef typename boost::serialization::is_bitwise_serializable< T >::type type;
|
Chris@16
|
129 };
|
Chris@16
|
130 #else
|
Chris@16
|
131 struct apply : public boost::serialization::is_bitwise_serializable< T > {};
|
Chris@16
|
132 #endif
|
Chris@16
|
133 };
|
Chris@16
|
134
|
Chris@16
|
135
|
Chris@16
|
136 // the optimized save_array dispatches to save_binary
|
Chris@16
|
137 template <class ValueType>
|
Chris@16
|
138 void save_array(boost::serialization::array<ValueType> const& a, unsigned int)
|
Chris@16
|
139 {
|
Chris@16
|
140 save_binary(a.address(),a.count()*sizeof(ValueType));
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 void save_binary(const void *address, std::size_t count);
|
Chris@16
|
144 };
|
Chris@16
|
145
|
Chris@16
|
146 template<class Archive, class Elem, class Tr>
|
Chris@16
|
147 inline void
|
Chris@16
|
148 basic_binary_oprimitive<Archive, Elem, Tr>::save_binary(
|
Chris@16
|
149 const void *address,
|
Chris@16
|
150 std::size_t count
|
Chris@16
|
151 ){
|
Chris@16
|
152 //BOOST_ASSERT(
|
Chris@16
|
153 // static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)()) >= count
|
Chris@16
|
154 //);
|
Chris@16
|
155 // note: if the following assertions fail
|
Chris@16
|
156 // a likely cause is that the output stream is set to "text"
|
Chris@16
|
157 // mode where by cr characters recieve special treatment.
|
Chris@16
|
158 // be sure that the output stream is opened with ios::binary
|
Chris@16
|
159 //if(os.fail())
|
Chris@16
|
160 // boost::serialization::throw_exception(
|
Chris@16
|
161 // archive_exception(archive_exception::output_stream_error)
|
Chris@16
|
162 // );
|
Chris@16
|
163 // figure number of elements to output - round up
|
Chris@16
|
164 count = ( count + sizeof(Elem) - 1)
|
Chris@16
|
165 / sizeof(Elem);
|
Chris@16
|
166 BOOST_ASSERT(count <= std::size_t(boost::integer_traits<std::streamsize>::const_max));
|
Chris@16
|
167 std::streamsize scount = m_sb.sputn(
|
Chris@16
|
168 static_cast<const Elem *>(address),
|
Chris@16
|
169 static_cast<std::streamsize>(count)
|
Chris@16
|
170 );
|
Chris@16
|
171 if(count != static_cast<std::size_t>(scount))
|
Chris@16
|
172 boost::serialization::throw_exception(
|
Chris@16
|
173 archive_exception(archive_exception::output_stream_error)
|
Chris@16
|
174 );
|
Chris@16
|
175 //os.write(
|
Chris@101
|
176 // static_cast<const typename OStream::char_type *>(address),
|
Chris@16
|
177 // count
|
Chris@16
|
178 //);
|
Chris@16
|
179 //BOOST_ASSERT(os.good());
|
Chris@16
|
180 }
|
Chris@16
|
181
|
Chris@16
|
182 } //namespace boost
|
Chris@16
|
183 } //namespace archive
|
Chris@16
|
184
|
Chris@16
|
185 #include <boost/archive/detail/abi_suffix.hpp> // pop pragmas
|
Chris@16
|
186
|
Chris@16
|
187 #endif // BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
|