1 /* Boost.Flyweight test template for serialization capabilities.
3 * Copyright 2006-2014 Joaquin M Lopez Munoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org/libs/flyweight for library home page.
11 #ifndef BOOST_FLYWEIGHT_TEST_SERIALIZATION_TEMPLATE_HPP
12 #define BOOST_FLYWEIGHT_TEST_SERIALIZATION_TEMPLATE_HPP
14 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
18 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
19 #include <boost/archive/text_oarchive.hpp>
20 #include <boost/archive/text_iarchive.hpp>
21 #include <boost/detail/lightweight_test.hpp>
22 #include <boost/flyweight/key_value.hpp>
23 #include <boost/flyweight/serialize.hpp>
24 #include <boost/functional/hash.hpp>
25 #include <boost/mpl/apply.hpp>
26 #include <boost/serialization/vector.hpp>
30 #include "heavy_objects.hpp"
32 #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
36 typedef tracked_string type;
39 tracked_string(const char* str_):str(str_){}
41 const std::string& get()const{return str;}
43 friend bool operator==(const type& x,const type& y){return x.str==y.str;}
44 friend bool operator< (const type& x,const type& y){return x.str< y.str;}
45 friend bool operator!=(const type& x,const type& y){return x.str!=y.str;}
46 friend bool operator> (const type& x,const type& y){return x.str> y.str;}
47 friend bool operator>=(const type& x,const type& y){return x.str>=y.str;}
48 friend bool operator<=(const type& x,const type& y){return x.str<=y.str;}
51 friend class boost::serialization::access;
53 template<class Archive>
54 void serialize(Archive& ar,const unsigned int){ar&str;}
59 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
63 inline std::size_t hash_value(const tracked_string& x)
65 boost::hash<std::string> h;
69 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
70 } /* namespace boost */
73 template<typename Flyweight,typename ForwardIterator>
74 void test_serialization_template(
75 ForwardIterator first,ForwardIterator last
76 BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Flyweight))
78 std::vector<Flyweight> v1;
79 while(first!=last)v1.push_back(Flyweight(*first++));
80 std::ostringstream oss;
82 const std::vector<Flyweight>& crv1=v1;
83 boost::archive::text_oarchive oa(oss);
87 std::vector<Flyweight> v2;
89 std::istringstream iss(oss.str());
90 boost::archive::text_iarchive ia(iss);
97 template<typename FlyweightSpecifier>
98 void test_serialization_template(
99 BOOST_EXPLICIT_TEMPLATE_TYPE(FlyweightSpecifier))
101 typedef typename boost::mpl::apply1<
102 FlyweightSpecifier,std::string
103 >::type string_flyweight;
105 typedef typename boost::mpl::apply1<
106 FlyweightSpecifier,tracked_string
107 >::type tracked_string_flyweight;
109 typedef typename boost::mpl::apply1<
111 boost::flyweights::key_value<std::string,texture,from_texture_to_string>
112 >::type texture_flyweight;
114 const char* words[]={"hello","boost","flyweight","boost","bye","c++","c++"};
115 test_serialization_template<string_flyweight>(
116 &words[0],&words[0]+LENGTHOF(words));
117 test_serialization_template<tracked_string_flyweight>(
118 &words[0],&words[0]+LENGTHOF(words));
120 const char* textures[]={
121 "wood","grass","sand","granite","terracotta","wood","sand","grass"};
122 test_serialization_template<texture_flyweight>(
123 &textures[0],&textures[0]+LENGTHOF(textures));