1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_helper_support.cpp
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)
9 // should pass compilation and execution
15 #include <cstdio> // remove
16 #include <boost/config.hpp>
17 #if defined(BOOST_NO_STDC_NAMESPACE)
23 #include "test_tools.hpp"
24 #include <boost/lexical_cast.hpp>
25 #include <boost/serialization/split_free.hpp>
26 #include <boost/serialization/vector.hpp>
30 class my_string:public std::string
32 typedef std::string super;
36 my_string(const super & str): super(str){}
37 my_string & operator=(const super& rhs) {
38 super::operator=(rhs);
43 struct my_string_helper
45 typedef std::vector<my_string> table;
49 BOOST_SERIALIZATION_SPLIT_FREE(my_string)
52 namespace serialization {
54 template<class Archive>
55 void save(Archive & ar, const my_string & str, const unsigned int /* version */)
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);
61 my_string_helper::table t = msh.m_t;
62 my_string_helper::table::iterator it = std::find(t.begin(), t.end(), str);
64 my_string_helper::table::size_type s = t.size();
65 ar << make_nvp("index", s);
67 ar << make_nvp("string", static_cast<const std::string &>(str));
70 my_string_helper::table::size_type s = it - t.begin();
71 ar << make_nvp("index", s);
75 template<class Archive>
76 void load(Archive & ar, my_string & str, const unsigned int /* version */)
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);
82 my_string_helper::table t = msh.m_t;
84 my_string_helper::table::size_type s;
85 ar >> make_nvp("index", s);
89 ar >> make_nvp("string", tmp);
98 } // namespace serialization
101 int test_main( int /* argc */, char* /* argv */[] ){
102 const char * testfile = boost::archive::tmpnam(NULL);
103 BOOST_REQUIRE(NULL != testfile);
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)));
110 test_ostream os(testfile, TEST_STREAM_FLAGS);
111 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
112 oa << boost::serialization::make_nvp("vector", v1);
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);
121 std::remove(testfile);