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