Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/serialization/example/demo_fast_archive.cpp @ 20

Last change on this file since 20 was 12, checked in by landauf, 18 years ago

added boost

File size: 4.9 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.hpp>
18#include <boost/archive/binary_iarchive.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<fast_binary_oarchive>
37{
38    typedef fast_binary_oarchive derived_t;
39#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
40public:
41#else
42    friend class boost::archive::detail::interface_oarchive<derived_t>;
43    friend class basic_binary_oarchive<derived_t>;
44    friend class basic_binary_oprimitive<derived_t, std::ostream>;
45    friend class boost::archive::save_access;
46#endif
47    // add base class to the places considered when matching
48    // save function to a specific set of arguments.  Note, this didn't
49    // work on my MSVC 7.0 system
50//        using binary_oarchive_impl<derived_t>::load_override;
51    // so we use the sure-fire method below.  This failed to work as well
52    template<class T>
53    void save_override(T & t, BOOST_PFTO int){
54        binary_oarchive_impl<fast_binary_oarchive>::save_override(t, 0);
55        // verify that this program is in fact working by making sure
56        // that arrays are getting passed here
57        BOOST_STATIC_ASSERT(! (boost::is_array<T>::value) );
58    }
59    template<int N>
60    void save_override(const int (& t)[N] , int){
61        save_binary(t, sizeof(t));
62    }
63    template<int N>
64    void save_override(const unsigned int (& t)[N], int){
65        save_binary(t, sizeof(t));
66    }
67    template<int N>
68    void save_override(const long (& t)[N], int){
69        save_binary(t, sizeof(t));
70    }
71    template<int N>
72    void save_override(const unsigned long (& t)[N], int){
73        save_binary(t, sizeof(t));
74    }
75public:
76    fast_binary_oarchive(std::ostream & os, unsigned flags = 0) :
77        binary_oarchive_impl<derived_t>(os, flags)
78    {}
79};
80
81/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
82// "Fast" input binary archive.  This is a variation of the native binary
83class fast_binary_iarchive :
84    // don't derive from binary_oarchive !!!
85    public binary_iarchive_impl<fast_binary_iarchive>
86{
87    typedef fast_binary_iarchive derived_t;
88#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
89public:
90#else
91    friend class boost::archive::detail::interface_iarchive<derived_t>;
92    friend class basic_binary_iarchive<derived_t>;
93    friend class basic_binary_iprimitive<derived_t, std::istream>;
94    friend class boost::archive::load_access;
95#endif
96    // add base class to the places considered when matching
97    // save function to a specific set of arguments.  Note, this didn't
98    // work on my MSVC 7.0 system
99//        using binary_oarchive_impl<derived_t>::load_override;
100    // so we use the sure-fire method below.  This failed to work as well
101    template<class T>
102    void load_override(T & t, BOOST_PFTO int){
103        binary_iarchive_impl<derived_t>::load_override(t, 0);
104        BOOST_STATIC_ASSERT(! (boost::is_array<T>::value) );
105    }
106    template<int N>
107    void load_override(int (& t)[N], int){
108        load_binary(t, sizeof(t));
109    }
110    template<int N>
111    void load_override(unsigned int (& t)[N], int){
112        load_binary(t, sizeof(t));
113    }
114    template<int N>
115    void load_override(long (& t)[N], int){
116        load_binary(t, sizeof(t));
117    }
118    template<int N>
119    void load_override(unsigned long (& t)[N], int){
120        load_binary(t, sizeof(t));
121    }
122public:
123    fast_binary_iarchive(std::istream & is, unsigned flags = 0) :
124        binary_iarchive_impl<derived_t>(is,flags)
125    {}
126};
127
128int main( int argc, char* argv[] )
129{
130    const int a[3] = {1, 2, 3};
131    int a1[3] = {4, 5, 6};
132
133    std::stringstream ss;
134    {   
135        fast_binary_oarchive pboa(ss);
136        pboa << a;
137    }
138    {
139        fast_binary_iarchive pbia(ss);
140        pbia >> a1;
141    }
142    return (a[0] != a1[0]) || (a[1] != a1[1]) || (a[2] != a1[2]);
143}
144
145
Note: See TracBrowser for help on using the repository browser.