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