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