Brian Silverman | 8867871 | 2018-08-04 23:56:48 -0700 | [diff] [blame^] | 1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 2 | // |
| 3 | // demo_portable_archive.cpp |
| 4 | // |
| 5 | // (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com . |
| 6 | // Use, modification and distribution is subject to the Boost Software |
| 7 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | // http://www.boost.org/LICENSE_1_0.txt) |
| 9 | |
| 10 | // should pass compilation and execution |
| 11 | |
| 12 | // note:: this example can only be built with the static library |
| 13 | // (at least with MSVC - due to conflicts related to import of library |
| 14 | // code and instantiation of templates. |
| 15 | #include <sstream> |
| 16 | |
| 17 | #include "portable_binary_oarchive.hpp" |
| 18 | #include "portable_binary_iarchive.hpp" |
| 19 | |
| 20 | #include <cstdlib> |
| 21 | #include <boost/config.hpp> |
| 22 | #if defined(BOOST_NO_STDC_NAMESPACE) |
| 23 | namespace std{ using ::rand; } |
| 24 | #endif |
| 25 | |
| 26 | class A |
| 27 | { |
| 28 | friend class boost::serialization::access; |
| 29 | char c; |
| 30 | A *pa; |
| 31 | int i; |
| 32 | int i2; // special tricky case to check sign extension |
| 33 | unsigned int ui; |
| 34 | long l; |
| 35 | unsigned long ul; |
| 36 | template<class Archive> |
| 37 | void serialize(Archive & ar, const unsigned int /* version */){ |
| 38 | ar & c & i & i2 & ui & l & ul ; |
| 39 | } |
| 40 | public: |
| 41 | bool operator==(const A & rhs) const { |
| 42 | return |
| 43 | c == rhs.c |
| 44 | && i == rhs.i |
| 45 | && i2 == rhs.i2 |
| 46 | && ui == rhs.ui |
| 47 | && l == rhs.l |
| 48 | && ul == rhs.ul |
| 49 | ; |
| 50 | } |
| 51 | A() : |
| 52 | c(0xFF & std::rand()), |
| 53 | pa(0), |
| 54 | i(std::rand()), |
| 55 | i2(0x80), |
| 56 | ui(std::rand()), |
| 57 | l(std::rand() * std::rand()), |
| 58 | ul(std::rand()) |
| 59 | {} |
| 60 | }; |
| 61 | |
| 62 | int main( int /* argc */, char* /* argv */[] ) |
| 63 | { |
| 64 | const A a; |
| 65 | A a1; |
| 66 | |
| 67 | std::stringstream ss; |
| 68 | { |
| 69 | portable_binary_oarchive pboa(ss); |
| 70 | pboa << a; |
| 71 | } |
| 72 | { |
| 73 | portable_binary_iarchive pbia(ss); |
| 74 | pbia >> a1; |
| 75 | } |
| 76 | if(! (a == a1)) |
| 77 | return 1; |
| 78 | |
| 79 | ss.clear(); |
| 80 | { |
| 81 | portable_binary_oarchive pboa(ss, endian_big); |
| 82 | pboa << a; |
| 83 | } |
| 84 | { |
| 85 | portable_binary_iarchive pbia(ss, endian_big); |
| 86 | pbia >> a1; |
| 87 | } |
| 88 | if(! (a == a1)) |
| 89 | return 1; |
| 90 | |
| 91 | ss.clear(); |
| 92 | { |
| 93 | portable_binary_oarchive pboa(ss, endian_big); |
| 94 | pboa << a; |
| 95 | } |
| 96 | { |
| 97 | portable_binary_iarchive pbia(ss, endian_big); |
| 98 | pbia >> a1; |
| 99 | } |
| 100 | |
| 101 | return !(a == a1); |
| 102 | } |
| 103 | |
| 104 | |