Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / serialization / test / test_helper_support.cpp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_helper_support.cpp
3
4 // (C) Copyright 2008 Joaquin M Lopez Munoz. 
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
11 #include <algorithm>
12 #include <cstddef>
13 #include <fstream>
14
15 #include <cstdio> // remove
16 #include <boost/config.hpp>
17 #if defined(BOOST_NO_STDC_NAMESPACE)
18 namespace std{ 
19     using ::remove;
20 }
21 #endif
22
23 #include "test_tools.hpp"
24 #include <boost/lexical_cast.hpp>
25 #include <boost/serialization/split_free.hpp>
26 #include <boost/serialization/vector.hpp>
27 #include <string>
28 #include <vector>
29
30 class my_string:public std::string
31 {
32     typedef std::string super;
33
34 public:
35     my_string(){}
36     my_string(const super & str): super(str){}
37     my_string & operator=(const super& rhs) {
38       super::operator=(rhs);
39       return *this;
40     }
41 };
42
43 struct my_string_helper
44 {
45   typedef std::vector<my_string> table;
46   table m_t;
47 };
48
49 BOOST_SERIALIZATION_SPLIT_FREE(my_string)
50
51 namespace boost {
52 namespace serialization {
53
54 template<class Archive>
55 void save(Archive & ar, const my_string & str, const unsigned int /* version */)
56 {
57     void (* const idx)(Archive &, const my_string &, const unsigned int) = & save;
58     void * const id = reinterpret_cast<void * const>(idx);
59     my_string_helper & msh = ar.template get_helper<my_string_helper>(id);
60
61     my_string_helper::table t = msh.m_t;
62     my_string_helper::table::iterator it = std::find(t.begin(), t.end(), str);
63     if(it == t.end()){
64         my_string_helper::table::size_type s = t.size();
65         ar << make_nvp("index", s);
66         t.push_back(str);
67         ar << make_nvp("string", static_cast<const std::string &>(str));
68     }
69     else{
70         my_string_helper::table::size_type s = it - t.begin();
71         ar << make_nvp("index", s);
72     }
73 }
74
75 template<class Archive>
76 void load(Archive & ar, my_string & str, const unsigned int /* version */)
77 {
78     void (* const idx)(Archive &, my_string &, const unsigned int) = & load;
79     void * const id = reinterpret_cast<void * const>(idx);
80     my_string_helper & msh = ar.template get_helper<my_string_helper>(id);
81
82     my_string_helper::table t = msh.m_t;
83
84     my_string_helper::table::size_type s;
85     ar >> make_nvp("index", s);
86     t.reserve(s);
87     if(s >= t.size()){
88         std::string tmp;
89         ar >> make_nvp("string", tmp);
90         str = tmp;
91         t.push_back(str);
92     }
93     else{
94         str = t[s];
95     }
96 }
97
98 } // namespace serialization
99 } // namespace boost
100
101 int test_main( int /* argc */, char* /* argv */[] ){
102     const char * testfile = boost::archive::tmpnam(NULL);
103     BOOST_REQUIRE(NULL != testfile);
104
105     std::vector<my_string> v1;
106     for(int i=0; i<1000; ++i){
107         v1.push_back(my_string(boost::lexical_cast<std::string>(i % 100)));
108     }
109     {
110         test_ostream os(testfile, TEST_STREAM_FLAGS);
111         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
112         oa << boost::serialization::make_nvp("vector", v1);
113     }
114     {
115         std::vector<my_string> v2;
116         test_istream is(testfile, TEST_STREAM_FLAGS);
117         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
118         ia >> boost::serialization::make_nvp("vector", v2);
119         BOOST_CHECK(v1 == v2);
120     }
121     std::remove(testfile);
122     return EXIT_SUCCESS;
123 }
124
125 // EOF