1 | /* Boost.Flyweight test template for serialization capabilities. |
2 | * |
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) |
7 | * |
8 | * See http://www.boost.org/libs/flyweight for library home page. |
9 | */ |
10 | |
11 | #ifndef BOOST_FLYWEIGHT_TEST_SERIALIZATION_TEMPLATE_HPP |
12 | #define BOOST_FLYWEIGHT_TEST_SERIALIZATION_TEMPLATE_HPP |
13 | |
14 | #if defined(_MSC_VER)&&(_MSC_VER>=1200) |
15 | #pragma once |
16 | #endif |
17 | |
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> |
27 | #include <string> |
28 | #include <sstream> |
29 | #include <vector> |
30 | #include "heavy_objects.hpp" |
31 | |
32 | #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0])) |
33 | |
34 | struct tracked_string |
35 | { |
36 | typedef tracked_string type; |
37 | |
38 | tracked_string(){} |
39 | tracked_string(const char* str_):str(str_){} |
40 | |
41 | const std::string& get()const{return str;} |
42 | |
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;} |
49 | |
50 | private: |
51 | friend class boost::serialization::access; |
52 | |
53 | template<class Archive> |
54 | void serialize(Archive& ar,const unsigned int){ar&str;} |
55 | |
56 | std::string str; |
57 | }; |
58 | |
59 | #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) |
60 | namespace boost{ |
61 | #endif |
62 | |
63 | inline std::size_t hash_value(const tracked_string& x) |
64 | { |
65 | boost::hash<std::string> h; |
66 | return h(x.get()); |
67 | } |
68 | |
69 | #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) |
70 | } /* namespace boost */ |
71 | #endif |
72 | |
73 | template<typename Flyweight,typename ForwardIterator> |
74 | void test_serialization_template( |
75 | ForwardIterator first,ForwardIterator last |
76 | BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(Flyweight)) |
77 | { |
78 | std::vector<Flyweight> v1; |
79 | while(first!=last)v1.push_back(Flyweight(*first++)); |
80 | std::ostringstream oss; |
81 | { |
82 | const std::vector<Flyweight>& crv1=v1; |
83 | boost::archive::text_oarchive oa(oss); |
84 | oa<<crv1; |
85 | } |
86 | |
87 | std::vector<Flyweight> v2; |
88 | { |
89 | std::istringstream iss(oss.str()); |
90 | boost::archive::text_iarchive ia(iss); |
91 | ia>>v2; |
92 | } |
93 | |
94 | BOOST_TEST(v1==v2); |
95 | } |
96 | |
97 | template<typename FlyweightSpecifier> |
98 | void test_serialization_template( |
99 | BOOST_EXPLICIT_TEMPLATE_TYPE(FlyweightSpecifier)) |
100 | { |
101 | typedef typename boost::mpl::apply1< |
102 | FlyweightSpecifier,std::string |
103 | >::type string_flyweight; |
104 | |
105 | typedef typename boost::mpl::apply1< |
106 | FlyweightSpecifier,tracked_string |
107 | >::type tracked_string_flyweight; |
108 | |
109 | typedef typename boost::mpl::apply1< |
110 | FlyweightSpecifier, |
111 | boost::flyweights::key_value<std::string,texture,from_texture_to_string> |
112 | >::type texture_flyweight; |
113 | |
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)); |
119 | |
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)); |
124 | } |
125 | |
126 | #undef LENGTHOF |
127 | |
128 | #endif |
129 | |