]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/flyweight/assoc_container_factory.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / flyweight / assoc_container_factory.hpp
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)
5  *
6  * See http://www.boost.org/libs/flyweight for library home page.
7  */
8
9 #ifndef BOOST_FLYWEIGHT_ASSOC_CONTAINER_FACTORY_HPP
10 #define BOOST_FLYWEIGHT_ASSOC_CONTAINER_FACTORY_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/flyweight/assoc_container_factory_fwd.hpp>
18 #include <boost/flyweight/detail/is_placeholder_expr.hpp>
19 #include <boost/flyweight/detail/nested_xxx_if_not_ph.hpp>
20 #include <boost/flyweight/factory_tag.hpp>
21 #include <boost/mpl/apply.hpp>
22 #include <boost/mpl/aux_/lambda_support.hpp>
23 #include <boost/mpl/if.hpp>
24
25 namespace boost{namespace flyweights{namespace detail{
26 BOOST_FLYWEIGHT_NESTED_XXX_IF_NOT_PLACEHOLDER_EXPRESSION_DEF(iterator);
27 BOOST_FLYWEIGHT_NESTED_XXX_IF_NOT_PLACEHOLDER_EXPRESSION_DEF(value_type);
28 }}} /* namespace boost::flyweights::detail */
29
30 /* Factory class using a given associative container.
31  */
32
33 namespace boost{
34
35 namespace flyweights{
36
37 template<typename Container>
38 class assoc_container_factory_class:public factory_marker
39 {
40 public:
41   /* When assoc_container_factory_class<Container> is an MPL placeholder
42    * expression, referring to Container::iterator and Container::value_type
43    * force the MPL placeholder expression Container to be instantiated, which
44    * is wasteful and can fail in concept-checked STL implementations.
45    * We protect ourselves against this circumstance.
46    */
47
48   typedef typename detail::nested_iterator_if_not_placeholder_expression<
49     Container
50   >::type                                handle_type;
51   typedef typename detail::nested_value_type_if_not_placeholder_expression<
52     Container
53   >::type                                entry_type;
54   
55   handle_type insert(const entry_type& x)
56   {
57     return cont.insert(x).first;
58   }
59
60   void erase(handle_type h)
61   {
62     cont.erase(h);
63   }
64
65   static const entry_type& entry(handle_type h){return *h;}
66
67 private:
68   /* As above, avoid instantiating Container if it is an
69    * MPL placeholder expression.
70    */
71
72   typedef typename mpl::if_<
73     detail::is_placeholder_expression<Container>,
74     int,
75     Container
76   >::type container_type;
77   container_type cont;
78
79 public:
80   typedef assoc_container_factory_class type;
81   BOOST_MPL_AUX_LAMBDA_SUPPORT(1,assoc_container_factory_class,(Container))
82 };
83
84 /* assoc_container_factory_class specifier */
85
86 template<
87   typename ContainerSpecifier
88   BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF
89 >
90 struct assoc_container_factory:factory_marker
91 {
92   template<typename Entry,typename Key>
93   struct apply
94   {
95     typedef assoc_container_factory_class<
96       typename mpl::apply2<ContainerSpecifier,Entry,Key>::type
97     > type;
98   };
99 };
100
101 }  /* namespace flyweights */
102
103 } /* namespace boost */
104
105 #endif