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)
6 * See http://www.boost.org/libs/flyweight for library home page.
9 #ifndef BOOST_FLYWEIGHT_DETAIL_SERIALIZATION_HELPER_HPP
10 #define BOOST_FLYWEIGHT_DETAIL_SERIALIZATION_HELPER_HPP
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
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>
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.
37 template<typename Flyweight>
38 struct flyweight_value_address
40 typedef const typename Flyweight::value_type* result_type;
42 result_type operator()(const Flyweight& x)const{return &x.get();}
45 template<typename Flyweight>
46 class save_helper:private noncopyable
48 typedef multi_index::multi_index_container<
50 multi_index::indexed_by<
51 multi_index::random_access<>,
52 multi_index::hashed_unique<flyweight_value_address<Flyweight> >
58 typedef typename table::size_type size_type;
60 size_type size()const{return t.size();}
62 size_type find(const Flyweight& x)const
64 return multi_index::project<0>(t,multi_index::get<1>(t).find(&x.get()))
68 void push_back(const Flyweight& x){t.push_back(x);}
74 template<typename Flyweight>
75 class load_helper:private noncopyable
77 typedef std::vector<Flyweight> table;
81 typedef typename table::size_type size_type;
83 size_type size()const{return t.size();}
85 Flyweight operator[](size_type n)const{return t[n];}
87 void push_back(const Flyweight& x){t.push_back(x);}
93 } /* namespace flyweights::detail */
95 } /* namespace flyweights */
97 } /* namespace boost */