1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // demo_fast_binary_archive.cpp
3
4 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 // should pass compilation and execution
10 #include <sstream>
11
12 #include <boost/static_assert.hpp>
13 #include <boost/type_traits/is_array.hpp>
14
15 #define BOOST_ARCHIVE_SOURCE
16 #include <boost/archive/binary_oarchive_impl.hpp>
17 #include <boost/archive/binary_iarchive_impl.hpp>
18 #include <boost/archive/detail/register_archive.hpp>
19
20 // include template definitions for base classes used. Otherwise
21 // you'll get link failure with undefined symbols
22 #include <boost/archive/impl/basic_binary_oprimitive.ipp>
23 #include <boost/archive/impl/basic_binary_iprimitive.ipp>
24 #include <boost/archive/impl/basic_binary_oarchive.ipp>
25 #include <boost/archive/impl/basic_binary_iarchive.ipp>
26
27 using namespace boost::archive;
28
29 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
30 // "Fast" output binary archive. This is a variation of the native binary
31 class fast_binary_oarchive :
32 // don't derive from binary_oarchive !!!
33 public binary_oarchive_impl<
34 fast_binary_oarchive,
35 std::ostream::char_type,
36 std::ostream::traits_type
37 >
38 {
39 typedef fast_binary_oarchive derived_t;
40 typedef binary_oarchive_impl<
41 fast_binary_oarchive,
42 std::ostream::char_type,
43 std::ostream::traits_type
44 > base_t;
45 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
46 public:
47 #else
48 friend class boost::archive::detail::interface_oarchive<derived_t>;
49 friend class basic_binary_oarchive<derived_t>;
50 friend class basic_binary_oprimitive<
51 derived_t,
52 std::ostream::char_type,
53 std::ostream::traits_type
54 >;
55 friend class boost::archive::save_access;
56 #endif
57 // add base class to the places considered when matching
58 // save function to a specific set of arguments. Note, this didn't
59 // work on my MSVC 7.0 system using
60 // binary_oarchive_impl<derived_t>::load_override;
61 // so we use the sure-fire method below. This failed to work as well
62 template<class T>
save_override(T & t)63 void save_override(T & t){
64 base_t::save_override(t);
65 // verify that this program is in fact working by making sure
66 // that arrays are getting passed here
67 BOOST_STATIC_ASSERT(! (boost::is_array<T>::value) );
68 }
69 template<int N>
save_override(const int (& t)[N])70 void save_override(const int (& t)[N]){
71 save_binary(t, sizeof(t));
72 }
73 template<int N>
save_override(const unsigned int (& t)[N])74 void save_override(const unsigned int (& t)[N]){
75 save_binary(t, sizeof(t));
76 }
77 template<int N>
save_override(const long (& t)[N])78 void save_override(const long (& t)[N]){
79 save_binary(t, sizeof(t));
80 }
81 template<int N>
save_override(const unsigned long (& t)[N])82 void save_override(const unsigned long (& t)[N]){
83 save_binary(t, sizeof(t));
84 }
85 public:
fast_binary_oarchive(std::ostream & os,unsigned flags=0)86 fast_binary_oarchive(std::ostream & os, unsigned flags = 0) :
87 base_t(os, flags)
88 {}
fast_binary_oarchive(std::streambuf & bsb,unsigned int flags=0)89 fast_binary_oarchive(std::streambuf & bsb, unsigned int flags = 0) :
90 base_t(bsb, flags)
91 {}
92 };
93
94 // required by export
95 BOOST_SERIALIZATION_REGISTER_ARCHIVE(fast_binary_oarchive)
96
97 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
98 // "Fast" input binary archive. This is a variation of the native binary
99 class fast_binary_iarchive :
100 // don't derive from binary_oarchive !!!
101 public binary_iarchive_impl<
102 fast_binary_iarchive,
103 std::istream::char_type,
104 std::istream::traits_type
105 >
106 {
107 typedef fast_binary_iarchive derived_t;
108 typedef binary_iarchive_impl<
109 fast_binary_iarchive,
110 std::istream::char_type,
111 std::istream::traits_type
112 > base_t;
113 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
114 public:
115 #else
116 friend class boost::archive::detail::interface_iarchive<derived_t>;
117 friend class basic_binary_iarchive<derived_t>;
118 friend class basic_binary_iprimitive<
119 derived_t,
120 std::ostream::char_type,
121 std::ostream::traits_type
122 >;
123 friend class boost::archive::load_access;
124 #endif
125 // add base class to the places considered when matching
126 // save function to a specific set of arguments. Note, this didn't
127 // work on my MSVC 7.0 system using
128 // binary_oarchive_impl<derived_t>::load_override;
129 // so we use the sure-fire method below. This failed to work as well
130 template<class T>
load_override(T & t)131 void load_override(T & t){
132 base_t::load_override(t);
133 BOOST_STATIC_ASSERT(! (boost::is_array<T>::value) );
134 }
135 template<int N>
load_override(int (& t)[N])136 void load_override(int (& t)[N]){
137 load_binary(t, sizeof(t));
138 }
139 template<int N>
load_override(unsigned int (& t)[N])140 void load_override(unsigned int (& t)[N]){
141 load_binary(t, sizeof(t));
142 }
143 template<int N>
load_override(long (& t)[N])144 void load_override(long (& t)[N]){
145 load_binary(t, sizeof(t));
146 }
147 template<int N>
load_override(unsigned long (& t)[N])148 void load_override(unsigned long (& t)[N]){
149 load_binary(t, sizeof(t));
150 }
151 public:
fast_binary_iarchive(std::istream & is,unsigned int flags=0)152 fast_binary_iarchive(std::istream & is, unsigned int flags = 0) :
153 base_t(is, flags)
154 {}
fast_binary_iarchive(std::streambuf & bsb,unsigned int flags=0)155 fast_binary_iarchive(std::streambuf & bsb, unsigned int flags = 0) :
156 base_t(bsb, flags)
157 {}
158 };
159
160 // required by export
BOOST_SERIALIZATION_REGISTER_ARCHIVE(fast_binary_iarchive)161 BOOST_SERIALIZATION_REGISTER_ARCHIVE(fast_binary_iarchive)
162
163 int main( int argc, char* argv[] )
164 {
165 const int a[3] = {1, 2, 3};
166 int a1[3] = {4, 5, 6};
167
168 std::stringstream ss;
169 {
170 fast_binary_oarchive pboa(ss);
171 pboa << a;
172 }
173 {
174 fast_binary_iarchive pbia(ss);
175 pbia >> a1;
176 }
177 return (a[0] != a1[0]) || (a[1] != a1[1]) || (a[2] != a1[2]);
178 }
179
180
181