1 | #ifndef BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP |
---|
2 | #define BOOST_ARCHIVE_BINARY_IPRIMITIVE_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_iprimitive.hpp |
---|
11 | // |
---|
12 | // archives stored as native binary - this should be the fastest way |
---|
13 | // to archive the state of a group of obects. It makes no attempt to |
---|
14 | // convert to any canonical form. |
---|
15 | |
---|
16 | // IN GENERAL, ARCHIVES CREATED WITH THIS CLASS WILL NOT BE READABLE |
---|
17 | // ON PLATFORM APART FROM THE ONE THEY ARE CREATED ON |
---|
18 | |
---|
19 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
---|
20 | // Use, modification and distribution is subject to the Boost Software |
---|
21 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
22 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
23 | |
---|
24 | // See http://www.boost.org for updates, documentation, and revision history. |
---|
25 | |
---|
26 | #include <iosfwd> |
---|
27 | #include <cassert> |
---|
28 | #include <cstring> // std::memcpy |
---|
29 | #include <cstddef> // std::size_t |
---|
30 | |
---|
31 | #include <string> |
---|
32 | |
---|
33 | #include <boost/config.hpp> |
---|
34 | #if defined(BOOST_NO_STDC_NAMESPACE) |
---|
35 | namespace std{ |
---|
36 | using ::memcpy; |
---|
37 | using ::size_t; |
---|
38 | } // namespace std |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <boost/throw_exception.hpp> |
---|
42 | #include <boost/limits.hpp> |
---|
43 | #include <boost/cstdint.hpp> |
---|
44 | #include <boost/io/ios_state.hpp> |
---|
45 | #include <boost/scoped_ptr.hpp> |
---|
46 | |
---|
47 | #include <boost/archive/archive_exception.hpp> |
---|
48 | |
---|
49 | #include <boost/archive/detail/abi_prefix.hpp> // must be the last header |
---|
50 | |
---|
51 | namespace boost { |
---|
52 | namespace archive { |
---|
53 | |
---|
54 | ///////////////////////////////////////////////////////////////////////////// |
---|
55 | // class binary_iarchive - read serialized objects from a input binary stream |
---|
56 | template<class Archive, class IStream> |
---|
57 | class basic_binary_iprimitive |
---|
58 | { |
---|
59 | #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS |
---|
60 | friend class load_access; |
---|
61 | protected: |
---|
62 | #else |
---|
63 | public: |
---|
64 | #endif |
---|
65 | // return a pointer to the most derived class |
---|
66 | Archive * This(){ |
---|
67 | return static_cast<Archive *>(this); |
---|
68 | } |
---|
69 | // native streams are always handled as bytes |
---|
70 | IStream &is; |
---|
71 | boost::scoped_ptr<std::locale> archive_locale; |
---|
72 | io::basic_ios_locale_saver< |
---|
73 | BOOST_DEDUCED_TYPENAME IStream::char_type, BOOST_DEDUCED_TYPENAME IStream::traits_type |
---|
74 | > locale_saver; |
---|
75 | |
---|
76 | // main template for serilization of primitive types |
---|
77 | template<class T> |
---|
78 | void load(T & t){ |
---|
79 | load_binary(& t, sizeof(T)); |
---|
80 | } |
---|
81 | |
---|
82 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
83 | load(std::string &s); |
---|
84 | #ifndef BOOST_NO_STD_WSTRING |
---|
85 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
86 | load(std::wstring &ws); |
---|
87 | #endif |
---|
88 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
89 | load(char * t); |
---|
90 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
91 | load(wchar_t * t); |
---|
92 | |
---|
93 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(void) |
---|
94 | init(); |
---|
95 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) |
---|
96 | basic_binary_iprimitive(IStream &is_, bool no_codecvt); |
---|
97 | BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY()) |
---|
98 | ~basic_binary_iprimitive(); |
---|
99 | public: |
---|
100 | void |
---|
101 | load_binary(void *address, std::size_t count); |
---|
102 | }; |
---|
103 | |
---|
104 | template<class Archive, class IStream> |
---|
105 | inline void |
---|
106 | basic_binary_iprimitive<Archive, IStream>::load_binary( |
---|
107 | void *address, |
---|
108 | std::size_t count |
---|
109 | ){ |
---|
110 | assert( |
---|
111 | static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)()) >= count |
---|
112 | ); |
---|
113 | if(is.fail()) |
---|
114 | boost::throw_exception(archive_exception(archive_exception::stream_error)); |
---|
115 | // note: an optimizer should eliminate the following for char files |
---|
116 | std::size_t s = count / sizeof(BOOST_DEDUCED_TYPENAME IStream::char_type); |
---|
117 | is.read( |
---|
118 | static_cast<BOOST_DEDUCED_TYPENAME IStream::char_type *>(address), |
---|
119 | s |
---|
120 | ); |
---|
121 | // note: an optimizer should eliminate the following for char files |
---|
122 | s = count % sizeof(BOOST_DEDUCED_TYPENAME IStream::char_type); |
---|
123 | if(0 < s){ |
---|
124 | if(is.fail()) |
---|
125 | boost::throw_exception(archive_exception(archive_exception::stream_error)); |
---|
126 | BOOST_DEDUCED_TYPENAME IStream::char_type t; |
---|
127 | is.read(& t, 1); |
---|
128 | std::memcpy(address, &t, s); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | } // namespace archive |
---|
133 | } // namespace boost |
---|
134 | |
---|
135 | #include <boost/archive/detail/abi_suffix.hpp> // pop pragams |
---|
136 | |
---|
137 | #endif // BOOST_ARCHIVE_BINARY_IPRIMITIVE_HPP |
---|