fix doc example typo
[boost.git] / boost / flyweight / hashed_factory.hpp
blob18062aae8b85539dd322f0cbca636386c5d4aacb
1 /* Copyright 2006-2009 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.
7 */
9 #ifndef BOOST_FLYWEIGHT_HASHED_FACTORY_HPP
10 #define BOOST_FLYWEIGHT_HASHED_FACTORY_HPP
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
13 #pragma once
14 #endif
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 /* Flyweight factory based on a hashed container implemented
26 * with Boost.MultiIndex.
29 namespace boost{
31 namespace flyweights{
33 template<
34 typename Entry,typename Key,
35 typename Hash,typename Pred,typename Allocator
37 class hashed_factory_class:public factory_marker
39 struct index_list:
40 boost::mpl::vector1<
41 multi_index::hashed_unique<
42 multi_index::identity<Entry>,
43 typename boost::mpl::if_<
44 mpl::is_na<Hash>,
45 hash<Key>,
46 Hash
47 >::type,
48 typename boost::mpl::if_<
49 mpl::is_na<Pred>,
50 std::equal_to<Key>,
51 Pred
52 >::type
55 {};
57 typedef multi_index::multi_index_container<
58 Entry,
59 index_list,
60 typename boost::mpl::if_<
61 mpl::is_na<Allocator>,
62 std::allocator<Entry>,
63 Allocator
64 >::type
65 > container_type;
67 public:
68 typedef const Entry* handle_type;
70 handle_type insert(const Entry& x)
72 return &*cont.insert(x).first;
75 void erase(handle_type h)
77 cont.erase(cont.iterator_to(*h));
80 static const Entry& entry(handle_type h){return *h;}
82 private:
83 container_type cont;
85 public:
86 typedef hashed_factory_class type;
87 BOOST_MPL_AUX_LAMBDA_SUPPORT(
88 5,hashed_factory_class,(Entry,Key,Hash,Pred,Allocator))
91 /* hashed_factory_class specifier */
93 template<
94 typename Hash,typename Pred,typename Allocator
95 BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF
97 struct hashed_factory:factory_marker
99 template<typename Entry,typename Key>
100 struct apply:
101 mpl::apply2<
102 hashed_factory_class<
103 boost::mpl::_1,boost::mpl::_2,Hash,Pred,Allocator
105 Entry,Key
110 } /* namespace flyweights */
112 } /* namespace boost */
114 #endif