Chris@101
|
1 /* Copyright 2006-2014 Joaquin M Lopez Munoz.
|
Chris@16
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 *
|
Chris@16
|
6 * See http://www.boost.org/libs/flyweight for library home page.
|
Chris@16
|
7 */
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_FLYWEIGHT_HASHED_FACTORY_HPP
|
Chris@16
|
10 #define BOOST_FLYWEIGHT_HASHED_FACTORY_HPP
|
Chris@16
|
11
|
Chris@101
|
12 #if defined(_MSC_VER)
|
Chris@16
|
13 #pragma once
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
|
Chris@16
|
17 #include <boost/flyweight/factory_tag.hpp>
|
Chris@16
|
18 #include <boost/flyweight/hashed_factory_fwd.hpp>
|
Chris@16
|
19 #include <boost/multi_index_container.hpp>
|
Chris@16
|
20 #include <boost/multi_index/identity.hpp>
|
Chris@16
|
21 #include <boost/multi_index/hashed_index.hpp>
|
Chris@16
|
22 #include <boost/mpl/aux_/lambda_support.hpp>
|
Chris@16
|
23 #include <boost/mpl/if.hpp>
|
Chris@16
|
24
|
Chris@101
|
25 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
Chris@101
|
26 #include <utility>
|
Chris@101
|
27 #endif
|
Chris@101
|
28
|
Chris@16
|
29 /* Flyweight factory based on a hashed container implemented
|
Chris@16
|
30 * with Boost.MultiIndex.
|
Chris@16
|
31 */
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost{
|
Chris@16
|
34
|
Chris@16
|
35 namespace flyweights{
|
Chris@16
|
36
|
Chris@16
|
37 template<
|
Chris@16
|
38 typename Entry,typename Key,
|
Chris@16
|
39 typename Hash,typename Pred,typename Allocator
|
Chris@16
|
40 >
|
Chris@16
|
41 class hashed_factory_class:public factory_marker
|
Chris@16
|
42 {
|
Chris@16
|
43 struct index_list:
|
Chris@16
|
44 boost::mpl::vector1<
|
Chris@16
|
45 multi_index::hashed_unique<
|
Chris@16
|
46 multi_index::identity<Entry>,
|
Chris@16
|
47 typename boost::mpl::if_<
|
Chris@16
|
48 mpl::is_na<Hash>,
|
Chris@16
|
49 hash<Key>,
|
Chris@16
|
50 Hash
|
Chris@16
|
51 >::type,
|
Chris@16
|
52 typename boost::mpl::if_<
|
Chris@16
|
53 mpl::is_na<Pred>,
|
Chris@16
|
54 std::equal_to<Key>,
|
Chris@16
|
55 Pred
|
Chris@16
|
56 >::type
|
Chris@16
|
57 >
|
Chris@16
|
58 >
|
Chris@16
|
59 {};
|
Chris@16
|
60
|
Chris@16
|
61 typedef multi_index::multi_index_container<
|
Chris@16
|
62 Entry,
|
Chris@16
|
63 index_list,
|
Chris@16
|
64 typename boost::mpl::if_<
|
Chris@16
|
65 mpl::is_na<Allocator>,
|
Chris@16
|
66 std::allocator<Entry>,
|
Chris@16
|
67 Allocator
|
Chris@16
|
68 >::type
|
Chris@16
|
69 > container_type;
|
Chris@16
|
70
|
Chris@16
|
71 public:
|
Chris@16
|
72 typedef const Entry* handle_type;
|
Chris@16
|
73
|
Chris@16
|
74 handle_type insert(const Entry& x)
|
Chris@16
|
75 {
|
Chris@16
|
76 return &*cont.insert(x).first;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@101
|
79 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
Chris@101
|
80 handle_type insert(Entry&& x)
|
Chris@101
|
81 {
|
Chris@101
|
82 return &*cont.insert(std::move(x)).first;
|
Chris@101
|
83 }
|
Chris@101
|
84 #endif
|
Chris@101
|
85
|
Chris@16
|
86 void erase(handle_type h)
|
Chris@16
|
87 {
|
Chris@16
|
88 cont.erase(cont.iterator_to(*h));
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 static const Entry& entry(handle_type h){return *h;}
|
Chris@16
|
92
|
Chris@16
|
93 private:
|
Chris@16
|
94 container_type cont;
|
Chris@16
|
95
|
Chris@16
|
96 public:
|
Chris@16
|
97 typedef hashed_factory_class type;
|
Chris@16
|
98 BOOST_MPL_AUX_LAMBDA_SUPPORT(
|
Chris@16
|
99 5,hashed_factory_class,(Entry,Key,Hash,Pred,Allocator))
|
Chris@16
|
100 };
|
Chris@16
|
101
|
Chris@16
|
102 /* hashed_factory_class specifier */
|
Chris@16
|
103
|
Chris@16
|
104 template<
|
Chris@16
|
105 typename Hash,typename Pred,typename Allocator
|
Chris@16
|
106 BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF
|
Chris@16
|
107 >
|
Chris@16
|
108 struct hashed_factory:factory_marker
|
Chris@16
|
109 {
|
Chris@16
|
110 template<typename Entry,typename Key>
|
Chris@16
|
111 struct apply:
|
Chris@16
|
112 mpl::apply2<
|
Chris@16
|
113 hashed_factory_class<
|
Chris@16
|
114 boost::mpl::_1,boost::mpl::_2,Hash,Pred,Allocator
|
Chris@16
|
115 >,
|
Chris@16
|
116 Entry,Key
|
Chris@16
|
117 >
|
Chris@16
|
118 {};
|
Chris@16
|
119 };
|
Chris@16
|
120
|
Chris@16
|
121 } /* namespace flyweights */
|
Chris@16
|
122
|
Chris@16
|
123 } /* namespace boost */
|
Chris@16
|
124
|
Chris@16
|
125 #endif
|