Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/serialization/example/demo_fast_archive.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 5.6 KB
Line 
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#include <boost/pfto.hpp>
15
16#define BOOST_ARCHIVE_SOURCE
17#include <boost/archive/binary_oarchive_impl.hpp>
18#include <boost/archive/binary_iarchive_impl.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#include <boost/archive/impl/archive_pointer_iserializer.ipp>
28#include <boost/archive/impl/archive_pointer_oserializer.ipp>
29
30using namespace boost::archive;
31
32/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
33// "Fast" output binary archive.  This is a variation of the native binary
34class fast_binary_oarchive :
35    // don't derive from binary_oarchive !!!
36    public binary_oarchive_impl<
37        fast_binary_oarchive, 
38        std::ostream::char_type, 
39        std::ostream::traits_type
40    >
41{
42    typedef fast_binary_oarchive derived_t;
43    typedef binary_oarchive_impl<
44        fast_binary_oarchive, 
45        std::ostream::char_type, 
46        std::ostream::traits_type
47    > base_t;
48#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
49public:
50#else
51    friend class boost::archive::detail::interface_oarchive<derived_t>;
52    friend class basic_binary_oarchive<derived_t>;
53    friend class basic_binary_oprimitive<
54        derived_t, 
55        std::ostream::char_type, 
56        std::ostream::traits_type
57    >;
58    friend class boost::archive::save_access;
59#endif
60    // add base class to the places considered when matching
61    // save function to a specific set of arguments.  Note, this didn't
62    // work on my MSVC 7.0 system using
63    // binary_oarchive_impl<derived_t>::load_override;
64    // so we use the sure-fire method below.  This failed to work as well
65    template<class T>
66    void save_override(T & t, BOOST_PFTO int){
67        base_t::save_override(t, 0);
68        // verify that this program is in fact working by making sure
69        // that arrays are getting passed here
70        BOOST_STATIC_ASSERT(! (boost::is_array<T>::value) );
71    }
72    template<int N>
73    void save_override(const int (& t)[N] , int){
74        save_binary(t, sizeof(t));
75    }
76    template<int N>
77    void save_override(const unsigned int (& t)[N], int){
78        save_binary(t, sizeof(t));
79    }
80    template<int N>
81    void save_override(const long (& t)[N], int){
82        save_binary(t, sizeof(t));
83    }
84    template<int N>
85    void save_override(const unsigned long (& t)[N], int){
86        save_binary(t, sizeof(t));
87    }
88public:
89    fast_binary_oarchive(std::ostream & os, unsigned flags = 0) :
90       base_t(os, flags)
91    {}
92    fast_binary_oarchive(std::streambuf & bsb, unsigned int flags = 0) :
93        base_t(bsb, flags)
94    {}
95};
96
97/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
98// "Fast" input binary archive.  This is a variation of the native binary
99class 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
114public:
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>
131    void load_override(T & t, BOOST_PFTO int){
132        base_t::load_override(t, 0);
133        BOOST_STATIC_ASSERT(! (boost::is_array<T>::value) );
134    }
135    template<int N>
136    void load_override(int (& t)[N], int){
137        load_binary(t, sizeof(t));
138    }
139    template<int N>
140    void load_override(unsigned int (& t)[N], int){
141        load_binary(t, sizeof(t));
142    }
143    template<int N>
144    void load_override(long (& t)[N], int){
145        load_binary(t, sizeof(t));
146    }
147    template<int N>
148    void load_override(unsigned long (& t)[N], int){
149        load_binary(t, sizeof(t));
150    }
151public:
152    fast_binary_iarchive(std::istream & is, unsigned int flags = 0) :
153        base_t(is, flags)
154    {}
155    fast_binary_iarchive(std::streambuf & bsb, unsigned int flags = 0) :
156        base_t(bsb, flags)
157    {}
158};
159
160int main( int argc, char* argv[] )
161{
162    const int a[3] = {1, 2, 3};
163    int a1[3] = {4, 5, 6};
164
165    std::stringstream ss;
166    {   
167        fast_binary_oarchive pboa(ss);
168        pboa << a;
169    }
170    {
171        fast_binary_iarchive pbia(ss);
172        pbia >> a1;
173    }
174    return (a[0] != a1[0]) || (a[1] != a1[1]) || (a[2] != a1[2]);
175}
176
177
Note: See TracBrowser for help on using the repository browser.