Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / flyweight / assoc_container_factory.hpp
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_ASSOC_CONTAINER_FACTORY_HPP
10 #define BOOST_FLYWEIGHT_ASSOC_CONTAINER_FACTORY_HPP
11
12 #if defined(_MSC_VER)
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 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
26 #include <utility>
27 #endif
28
29 namespace boost{namespace flyweights{namespace detail{
30 BOOST_FLYWEIGHT_NESTED_XXX_IF_NOT_PLACEHOLDER_EXPRESSION_DEF(iterator);
31 BOOST_FLYWEIGHT_NESTED_XXX_IF_NOT_PLACEHOLDER_EXPRESSION_DEF(value_type);
32 }}} /* namespace boost::flyweights::detail */
33
34 /* Factory class using a given associative container.
35  */
36
37 namespace boost{
38
39 namespace flyweights{
40
41 template<typename Container>
42 class assoc_container_factory_class:public factory_marker
43 {
44 public:
45   /* When assoc_container_factory_class<Container> is an MPL placeholder
46    * expression, referring to Container::iterator and Container::value_type
47    * force the MPL placeholder expression Container to be instantiated, which
48    * is wasteful and can fail in concept-checked STL implementations.
49    * We protect ourselves against this circumstance.
50    */
51
52   typedef typename detail::nested_iterator_if_not_placeholder_expression<
53     Container
54   >::type                                handle_type;
55   typedef typename detail::nested_value_type_if_not_placeholder_expression<
56     Container
57   >::type                                entry_type;
58   
59   handle_type insert(const entry_type& x)
60   {
61     return cont.insert(x).first;
62   }
63
64 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
65   handle_type insert(entry_type&& x)
66   {
67     return cont.insert(std::move(x)).first;
68   }
69 #endif
70
71   void erase(handle_type h)
72   {
73     cont.erase(h);
74   }
75
76   static const entry_type& entry(handle_type h){return *h;}
77
78 private:
79   /* As above, avoid instantiating Container if it is an
80    * MPL placeholder expression.
81    */
82
83   typedef typename mpl::if_<
84     detail::is_placeholder_expression<Container>,
85     int,
86     Container
87   >::type container_type;
88   container_type cont;
89
90 public:
91   typedef assoc_container_factory_class type;
92   BOOST_MPL_AUX_LAMBDA_SUPPORT(1,assoc_container_factory_class,(Container))
93 };
94
95 /* assoc_container_factory_class specifier */
96
97 template<
98   typename ContainerSpecifier
99   BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION_DEF
100 >
101 struct assoc_container_factory:factory_marker
102 {
103   template<typename Entry,typename Key>
104   struct apply
105   {
106     typedef assoc_container_factory_class<
107       typename mpl::apply2<ContainerSpecifier,Entry,Key>::type
108     > type;
109   };
110 };
111
112 }  /* namespace flyweights */
113
114 } /* namespace boost */
115
116 #endif