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) && (_MSC_VER >= 1020)
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 <cassert>
28 #include <locale>
29 #include <cstddef> // size_t
30 
31 #include <boost/config.hpp>
32 #if defined(BOOST_NO_STDC_NAMESPACE)
33 namespace std{
34     using ::size_t;
35 } // namespace std
36 #endif
37 
38 #include <boost/cstdint.hpp>
39 #include <boost/limits.hpp>
40 #include <boost/io/ios_state.hpp>
41 #include <boost/scoped_ptr.hpp>
42 #include <boost/throw_exception.hpp>
43 
44 #include <boost/archive/archive_exception.hpp>
45 
46 namespace boost {
47 namespace archive {
48 
49 /////////////////////////////////////////////////////////////////////////
50 // class basic_binary_oprimitive - binary output of prmitives
51 
52 template<class Archive, class OStream>
53 class basic_binary_oprimitive
54 {
55 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
56     friend class save_access;
57 protected:
58 #else
59 public:
60 #endif
61     // return a pointer to the most derived class
This()62     Archive * This(){
63         return static_cast<Archive *>(this);
64     }
65     // native binary streams are handled as bytes
66     OStream &os;
67     boost::scoped_ptr<std::locale> archive_locale;
68     io::basic_ios_locale_saver<
69         BOOST_DEDUCED_TYPENAME OStream::char_type,
70         BOOST_DEDUCED_TYPENAME OStream::traits_type
71     > locale_saver;
72 
73     // default saving of primitives.
74     template<class T>
save(const T & t)75     void save(const T & t)
76     {
77         save_binary(& t, sizeof(T));
78     }
79 
80     BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
81     save(const std::string &s);
82     #ifndef BOOST_NO_STD_WSTRING
83     BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
84     save(const std::wstring &ws);
85     #endif
86     BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
87     save(const char * t);
88     BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
89     save(const wchar_t * t);
90 
91     BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
92     init();
93     BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
94     basic_binary_oprimitive(OStream & os, bool no_codecvt);
95     BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
96     ~basic_binary_oprimitive();
97 public:
98     void save_binary(const void *address, std::size_t count);
99 };
100 
101 template<class Archive, class OStream>
102 inline void
save_binary(const void * address,std::size_t count)103 basic_binary_oprimitive<Archive, OStream>::save_binary(
104     const void *address,
105     std::size_t count
106 ){
107     assert(
108         static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)()) >= count
109     );
110     // note: if the following assertions fail
111     // a likely cause is that the output stream is set to "text"
112     // mode where by cr characters recieve special treatment.
113     // be sure that the output stream is opened with ios::binary
114     if(os.fail())
115         boost::throw_exception(archive_exception(archive_exception::stream_error));
116     // figure number of elements to output - round up
117     count = ( count + sizeof(BOOST_DEDUCED_TYPENAME OStream::char_type) - 1)
118         / sizeof(BOOST_DEDUCED_TYPENAME OStream::char_type);
119     os.write(
120         static_cast<const BOOST_DEDUCED_TYPENAME OStream::char_type *>(address),
121         count
122     );
123     assert(os.good());
124 }
125 
126 } //namespace boost
127 } //namespace archive
128 
129 #endif // BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP
130