• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/serialization/is_bitwise_serializable.hpp>
47 #include <boost/serialization/array_wrapper.hpp>
48 
49 #include <boost/archive/basic_streambuf_locale_saver.hpp>
50 #include <boost/archive/codecvt_null.hpp>
51 #include <boost/archive/archive_exception.hpp>
52 #include <boost/archive/detail/auto_link_archive.hpp>
53 #include <boost/archive/detail/abi_prefix.hpp> // must be the last header
54 
55 namespace boost {
56 namespace archive {
57 
58 /////////////////////////////////////////////////////////////////////////
59 // class basic_binary_oprimitive - binary output of prmitives
60 
61 template<class Archive, class Elem, class Tr>
62 class BOOST_SYMBOL_VISIBLE basic_binary_oprimitive {
63 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
64     friend class save_access;
65 protected:
66 #else
67 public:
68 #endif
69     std::basic_streambuf<Elem, Tr> & m_sb;
70     // return a pointer to the most derived class
This()71     Archive * This(){
72         return static_cast<Archive *>(this);
73     }
74     #ifndef BOOST_NO_STD_LOCALE
75     // note order! - if you change this, libstd++ will fail!
76     // a) create new locale with new codecvt facet
77     // b) save current locale
78     // c) change locale to new one
79     // d) use stream buffer
80     // e) change locale back to original
81     // f) destroy new codecvt facet
82     boost::archive::codecvt_null<Elem> codecvt_null_facet;
83     basic_streambuf_locale_saver<Elem, Tr> locale_saver;
84     std::locale archive_locale;
85     #endif
86     // default saving of primitives.
87     template<class T>
save(const T & t)88     void save(const T & t)
89     {
90         save_binary(& t, sizeof(T));
91     }
92 
93     /////////////////////////////////////////////////////////
94     // fundamental types that need special treatment
95 
96     // trap usage of invalid uninitialized boolean which would
97     // otherwise crash on load.
save(const bool t)98     void save(const bool t){
99         BOOST_ASSERT(0 == static_cast<int>(t) || 1 == static_cast<int>(t));
100         save_binary(& t, sizeof(t));
101     }
102     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
103     save(const std::string &s);
104     #ifndef BOOST_NO_STD_WSTRING
105     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
106     save(const std::wstring &ws);
107     #endif
108     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
109     save(const char * t);
110     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
111     save(const wchar_t * t);
112 
113     BOOST_ARCHIVE_OR_WARCHIVE_DECL void
114     init();
115 
116     BOOST_ARCHIVE_OR_WARCHIVE_DECL
117     basic_binary_oprimitive(
118         std::basic_streambuf<Elem, Tr> & sb,
119         bool no_codecvt
120     );
121     BOOST_ARCHIVE_OR_WARCHIVE_DECL
122     ~basic_binary_oprimitive();
123 public:
124 
125     // we provide an optimized save for all fundamental types
126     // typedef serialization::is_bitwise_serializable<mpl::_1>
127     // use_array_optimization;
128     // workaround without using mpl lambdas
129     struct use_array_optimization {
130         template <class T>
131         #if defined(BOOST_NO_DEPENDENT_NESTED_DERIVATIONS)
132             struct apply {
133                 typedef typename boost::serialization::is_bitwise_serializable< T >::type type;
134             };
135         #else
136             struct apply : public boost::serialization::is_bitwise_serializable< T > {};
137         #endif
138     };
139 
140     // the optimized save_array dispatches to save_binary
141     template <class ValueType>
save_array(boost::serialization::array_wrapper<ValueType> const & a,unsigned int)142     void save_array(boost::serialization::array_wrapper<ValueType> const& a, unsigned int)
143     {
144       save_binary(a.address(),a.count()*sizeof(ValueType));
145     }
146 
147     void save_binary(const void *address, std::size_t count);
148 };
149 
150 template<class Archive, class Elem, class Tr>
151 inline void
save_binary(const void * address,std::size_t count)152 basic_binary_oprimitive<Archive, Elem, Tr>::save_binary(
153     const void *address,
154     std::size_t count
155 ){
156     // BOOST_ASSERT(count <= std::size_t(boost::integer_traits<std::streamsize>::const_max));
157     // note: if the following assertions fail
158     // a likely cause is that the output stream is set to "text"
159     // mode where by cr characters recieve special treatment.
160     // be sure that the output stream is opened with ios::binary
161     //if(os.fail())
162     //    boost::serialization::throw_exception(
163     //        archive_exception(archive_exception::output_stream_error)
164     //    );
165     // figure number of elements to output - round up
166     count = ( count + sizeof(Elem) - 1) / sizeof(Elem);
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