Chris@102
|
1 /* Copyright 2006-2014 Joaquin M Lopez Munoz.
|
Chris@102
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@102
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@102
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
5 *
|
Chris@102
|
6 * See http://www.boost.org/libs/flyweight for library home page.
|
Chris@102
|
7 */
|
Chris@102
|
8
|
Chris@102
|
9 #ifndef BOOST_FLYWEIGHT_DETAIL_SERIALIZATION_HELPER_HPP
|
Chris@102
|
10 #define BOOST_FLYWEIGHT_DETAIL_SERIALIZATION_HELPER_HPP
|
Chris@102
|
11
|
Chris@102
|
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
|
Chris@102
|
13 #pragma once
|
Chris@102
|
14 #endif
|
Chris@102
|
15
|
Chris@102
|
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
|
Chris@102
|
17 #include <boost/multi_index_container.hpp>
|
Chris@102
|
18 #include <boost/multi_index/hashed_index.hpp>
|
Chris@102
|
19 #include <boost/multi_index/random_access_index.hpp>
|
Chris@102
|
20 #include <boost/noncopyable.hpp>
|
Chris@102
|
21 #include <boost/serialization/extended_type_info.hpp>
|
Chris@102
|
22 #include <vector>
|
Chris@102
|
23
|
Chris@102
|
24 namespace boost{
|
Chris@102
|
25
|
Chris@102
|
26 namespace flyweights{
|
Chris@102
|
27
|
Chris@102
|
28 namespace detail{
|
Chris@102
|
29
|
Chris@102
|
30 /* The serialization helpers for flyweight<T> map numerical IDs to
|
Chris@102
|
31 * flyweight exemplars --an exemplar is the flyweight object
|
Chris@102
|
32 * associated to a given value that appears first on the serialization
|
Chris@102
|
33 * stream, so that subsequent equivalent flyweight objects will be made
|
Chris@102
|
34 * to refer to it during the serialization process.
|
Chris@102
|
35 */
|
Chris@102
|
36
|
Chris@102
|
37 template<typename Flyweight>
|
Chris@102
|
38 struct flyweight_value_address
|
Chris@102
|
39 {
|
Chris@102
|
40 typedef const typename Flyweight::value_type* result_type;
|
Chris@102
|
41
|
Chris@102
|
42 result_type operator()(const Flyweight& x)const{return &x.get();}
|
Chris@102
|
43 };
|
Chris@102
|
44
|
Chris@102
|
45 template<typename Flyweight>
|
Chris@102
|
46 class save_helper:private noncopyable
|
Chris@102
|
47 {
|
Chris@102
|
48 typedef multi_index::multi_index_container<
|
Chris@102
|
49 Flyweight,
|
Chris@102
|
50 multi_index::indexed_by<
|
Chris@102
|
51 multi_index::random_access<>,
|
Chris@102
|
52 multi_index::hashed_unique<flyweight_value_address<Flyweight> >
|
Chris@102
|
53 >
|
Chris@102
|
54 > table;
|
Chris@102
|
55
|
Chris@102
|
56 public:
|
Chris@102
|
57
|
Chris@102
|
58 typedef typename table::size_type size_type;
|
Chris@102
|
59
|
Chris@102
|
60 size_type size()const{return t.size();}
|
Chris@102
|
61
|
Chris@102
|
62 size_type find(const Flyweight& x)const
|
Chris@102
|
63 {
|
Chris@102
|
64 return multi_index::project<0>(t,multi_index::get<1>(t).find(&x.get()))
|
Chris@102
|
65 -t.begin();
|
Chris@102
|
66 }
|
Chris@102
|
67
|
Chris@102
|
68 void push_back(const Flyweight& x){t.push_back(x);}
|
Chris@102
|
69
|
Chris@102
|
70 private:
|
Chris@102
|
71 table t;
|
Chris@102
|
72 };
|
Chris@102
|
73
|
Chris@102
|
74 template<typename Flyweight>
|
Chris@102
|
75 class load_helper:private noncopyable
|
Chris@102
|
76 {
|
Chris@102
|
77 typedef std::vector<Flyweight> table;
|
Chris@102
|
78
|
Chris@102
|
79 public:
|
Chris@102
|
80
|
Chris@102
|
81 typedef typename table::size_type size_type;
|
Chris@102
|
82
|
Chris@102
|
83 size_type size()const{return t.size();}
|
Chris@102
|
84
|
Chris@102
|
85 Flyweight operator[](size_type n)const{return t[n];}
|
Chris@102
|
86
|
Chris@102
|
87 void push_back(const Flyweight& x){t.push_back(x);}
|
Chris@102
|
88
|
Chris@102
|
89 private:
|
Chris@102
|
90 table t;
|
Chris@102
|
91 };
|
Chris@102
|
92
|
Chris@102
|
93 } /* namespace flyweights::detail */
|
Chris@102
|
94
|
Chris@102
|
95 } /* namespace flyweights */
|
Chris@102
|
96
|
Chris@102
|
97 } /* namespace boost */
|
Chris@102
|
98
|
Chris@102
|
99 #endif
|