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 | #include <streambuf> // basic_streambuf |
---|
31 | #include <string> |
---|
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/limits.hpp> |
---|
42 | //#include <boost/io/ios_state.hpp> |
---|
43 | #include <boost/scoped_ptr.hpp> |
---|
44 | #include <boost/throw_exception.hpp> |
---|
45 | |
---|
46 | #include <boost/archive/basic_streambuf_locale_saver.hpp> |
---|
47 | #include <boost/archive/archive_exception.hpp> |
---|
48 | #include <boost/archive/detail/auto_link_archive.hpp> |
---|
49 | #include <boost/archive/detail/abi_prefix.hpp> // must be the last header |
---|
50 | |
---|
51 | namespace boost { |
---|
52 | namespace archive { |
---|
53 | |
---|
54 | ///////////////////////////////////////////////////////////////////////// |
---|
55 | // class basic_binary_oprimitive - binary output of prmitives |
---|
56 | |
---|
57 | template<class Archive, class Elem, class Tr> |
---|
58 | class basic_binary_oprimitive |
---|
59 | { |
---|
60 | #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
---|
61 | friend class save_access; |
---|
62 | protected: |
---|
63 | #else |
---|
64 | public: |
---|
65 | #endif |
---|
66 | std::basic_streambuf<Elem, Tr> & m_sb; |
---|
67 | // return a pointer to the most derived class |
---|
68 | Archive * This(){ |
---|
69 | return static_cast<Archive *>(this); |
---|
70 | } |
---|
71 | boost::scoped_ptr<std::locale> archive_locale; |
---|
72 | basic_streambuf_locale_saver<Elem, Tr> locale_saver; |
---|
73 | |
---|
74 | // default saving of primitives. |
---|
75 | template<class T> |
---|
76 | void save(const T & t) |
---|
77 | { |
---|
78 | save_binary(& t, sizeof(T)); |
---|
79 | } |
---|
80 | |
---|
81 | ///////////////////////////////////////////////////////// |
---|
82 | // fundamental types that need special treatment |
---|
83 | |
---|
84 | // trap usage of invalid uninitialized boolean which would |
---|
85 | // otherwise crash on load. |
---|
86 | void save(const bool t){ |
---|
87 | int i = t; |
---|
88 | assert(0 == i || 1 == i); |
---|
89 | save_binary(& t, sizeof(t)); |
---|
90 | } |
---|
91 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
92 | save(const std::string &s); |
---|
93 | #ifndef BOOST_NO_STD_WSTRING |
---|
94 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
95 | save(const std::wstring &ws); |
---|
96 | #endif |
---|
97 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
98 | save(const char * t); |
---|
99 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
100 | save(const wchar_t * t); |
---|
101 | |
---|
102 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
103 | init(); |
---|
104 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) |
---|
105 | basic_binary_oprimitive( |
---|
106 | std::basic_streambuf<Elem, Tr> & sb, |
---|
107 | bool no_codecvt |
---|
108 | ); |
---|
109 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) |
---|
110 | ~basic_binary_oprimitive(); |
---|
111 | public: |
---|
112 | void save_binary(const void *address, std::size_t count); |
---|
113 | }; |
---|
114 | |
---|
115 | template<class Archive, class Elem, class Tr> |
---|
116 | inline void |
---|
117 | basic_binary_oprimitive<Archive, Elem, Tr>::save_binary( |
---|
118 | const void *address, |
---|
119 | std::size_t count |
---|
120 | ){ |
---|
121 | //assert( |
---|
122 | // static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)()) >= count |
---|
123 | //); |
---|
124 | // note: if the following assertions fail |
---|
125 | // a likely cause is that the output stream is set to "text" |
---|
126 | // mode where by cr characters recieve special treatment. |
---|
127 | // be sure that the output stream is opened with ios::binary |
---|
128 | //if(os.fail()) |
---|
129 | // boost::throw_exception(archive_exception(archive_exception::stream_error)); |
---|
130 | // figure number of elements to output - round up |
---|
131 | count = ( count + sizeof(Elem) - 1) |
---|
132 | / sizeof(Elem); |
---|
133 | std::streamsize scount = m_sb.sputn( |
---|
134 | static_cast<const Elem *>(address), |
---|
135 | count |
---|
136 | ); |
---|
137 | if(count != static_cast<std::size_t>(scount)) |
---|
138 | boost::throw_exception(archive_exception(archive_exception::stream_error)); |
---|
139 | //os.write( |
---|
140 | // static_cast<const BOOST_DEDUCED_TYPENAME OStream::char_type *>(address), |
---|
141 | // count |
---|
142 | //); |
---|
143 | //assert(os.good()); |
---|
144 | } |
---|
145 | |
---|
146 | } //namespace boost |
---|
147 | } //namespace archive |
---|
148 | |
---|
149 | #include <boost/archive/detail/abi_suffix.hpp> // pop pragams |
---|
150 | |
---|
151 | #endif // BOOST_ARCHIVE_BASIC_BINARY_OPRIMITIVE_HPP |
---|