| 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 <vector> |
| 22 | |
| 23 | namespace boost{ |
| 24 | |
| 25 | namespace flyweights{ |
| 26 | |
| 27 | namespace detail{ |
| 28 | |
| 29 | /* The serialization helpers for flyweight<T> map numerical IDs to |
| 30 | * flyweight exemplars --an exemplar is the flyweight object |
| 31 | * associated to a given value that appears first on the serialization |
| 32 | * stream, so that subsequent equivalent flyweight objects will be made |
| 33 | * to refer to it during the serialization process. |
| 34 | */ |
| 35 | |
| 36 | template<typename Flyweight> |
| 37 | struct flyweight_value_address |
| 38 | { |
| 39 | typedef const typename Flyweight::value_type* result_type; |
| 40 | |
| 41 | result_type operator()(const Flyweight& x)const{return &x.get();} |
| 42 | }; |
| 43 | |
| 44 | template<typename Flyweight> |
| 45 | class save_helper:private noncopyable |
| 46 | { |
| 47 | typedef multi_index::multi_index_container< |
| 48 | Flyweight, |
| 49 | multi_index::indexed_by< |
| 50 | multi_index::random_access<>, |
| 51 | multi_index::hashed_unique<flyweight_value_address<Flyweight> > |
| 52 | > |
| 53 | > table; |
| 54 | |
| 55 | public: |
| 56 | |
| 57 | typedef typename table::size_type size_type; |
| 58 | |
| 59 | size_type size()const{return t.size();} |
| 60 | |
| 61 | size_type find(const Flyweight& x)const |
| 62 | { |
| 63 | return multi_index::project<0>(t,multi_index::get<1>(t).find(&x.get())) |
| 64 | -t.begin(); |
| 65 | } |
| 66 | |
| 67 | void push_back(const Flyweight& x){t.push_back(x);} |
| 68 | |
| 69 | private: |
| 70 | table t; |
| 71 | }; |
| 72 | |
| 73 | template<typename Flyweight> |
| 74 | class load_helper:private noncopyable |
| 75 | { |
| 76 | typedef std::vector<Flyweight> table; |
| 77 | |
| 78 | public: |
| 79 | |
| 80 | typedef typename table::size_type size_type; |
| 81 | |
| 82 | size_type size()const{return t.size();} |
| 83 | |
| 84 | Flyweight operator[](size_type n)const{return t[n];} |
| 85 | |
| 86 | void push_back(const Flyweight& x){t.push_back(x);} |
| 87 | |
| 88 | private: |
| 89 | table t; |
| 90 | }; |
| 91 | |
| 92 | } /* namespace flyweights::detail */ |
| 93 | |
| 94 | } /* namespace flyweights */ |
| 95 | |
| 96 | } /* namespace boost */ |
| 97 | |
| 98 | #endif |
| 99 | |