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_HASHED_FACTORY_HPP
10 #define BOOST_FLYWEIGHT_HASHED_FACTORY_HPP
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17 #include <boost/flyweight/factory_tag.hpp>
18 #include <boost/flyweight/hashed_factory_fwd.hpp>
19 #include <boost/multi_index_container.hpp>
20 #include <boost/multi_index/identity.hpp>
21 #include <boost/multi_index/hashed_index.hpp>
22 #include <boost/mpl/aux_/lambda_support.hpp>
23 #include <boost/mpl/if.hpp>
25 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
29 /* Flyweight factory based on a hashed container implemented
30 * with Boost.MultiIndex.
38 typename Entry,typename Key,
39 typename Hash,typename Pred,typename Allocator
41 class hashed_factory_class:public factory_marker
45 multi_index::hashed_unique<
46 multi_index::identity<Entry>,
47 typename boost::mpl::if_<
52 typename boost::mpl::if_<
61 typedef multi_index::multi_index_container<
64 typename boost::mpl::if_<
65 mpl::is_na<Allocator>,
66 std::allocator<Entry>,
72 typedef const Entry* handle_type;
74 handle_type insert(const Entry& x)
76 return &*cont.insert(x).first;
79 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
80 handle_type insert(Entry&& x)
82 return &*cont.insert(std::move(x)).first;
86 void erase(handle_type h)
88 cont.erase(cont.iterator_to(*h));
91 static const Entry& entry(handle_type h){return *h;}
97 typedef hashed_factory_class type;
98 BOOST_MPL_AUX_LAMBDA_SUPPORT(
99 5,hashed_factory_class,(Entry,Key,Hash,Pred,Allocator))
102 /* hashed_factory_class specifier */
105 typename Hash,typename Pred,typename Allocator
106 BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF
108 struct hashed_factory:factory_marker
110 template<typename Entry,typename Key>
113 hashed_factory_class<
114 boost::mpl::_1,boost::mpl::_2,Hash,Pred,Allocator
121 } /* namespace flyweights */
123 } /* namespace boost */