314d5269082413316755fd60129ecd1c929aa718
[senf.git] / boost / multi_index / global_fun.hpp
1 /* Copyright 2003-2007 Joaquín M López Muñoz.
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/multi_index for library home page.
7  */
8
9 #ifndef BOOST_MULTI_INDEX_GLOBAL_FUN_HPP
10 #define BOOST_MULTI_INDEX_GLOBAL_FUN_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/mpl/if.hpp>
18 #include <boost/type_traits/is_const.hpp>
19 #include <boost/type_traits/is_reference.hpp>
20 #include <boost/type_traits/remove_const.hpp>
21 #include <boost/type_traits/remove_reference.hpp>
22 #include <boost/utility/enable_if.hpp>
23
24 #if !defined(BOOST_NO_SFINAE)
25 #include <boost/type_traits/is_convertible.hpp>
26 #endif
27
28 namespace boost{
29
30 template<class T> class reference_wrapper; /* fwd decl. */
31
32 namespace multi_index{
33
34 namespace detail{
35
36 /* global_fun is a read-only key extractor from Value based on a given global
37  * (or static member) function with signature:
38  *
39  *   Type f([const] Value [&]);
40  *
41  * Additionally, global_fun  and const_global_fun are overloaded to support
42  * referece_wrappers of Value and "chained pointers" to Value's. By chained
43  * pointer to T we  mean a type P such that, given a p of Type P
44  *   *...n...*x is convertible to T&, for some n>=1.
45  * Examples of chained pointers are raw and smart pointers, iterators and
46  * arbitrary combinations of these (vg. T** or auto_ptr<T*>.)
47  */
48
49 /* NB. Some overloads of operator() have an extra dummy parameter int=0.
50  * This disambiguator serves several purposes:
51  *  - Without it, MSVC++ 6.0 incorrectly regards some overloads as
52  *    specializations of a previous member function template.
53  *  - MSVC++ 6.0/7.0 seem to incorrectly treat some different memfuns
54  *    as if they have the same signature.
55  *  - If remove_const is broken due to lack of PTS, int=0 avoids the
56  *    declaration of memfuns with identical signature.
57  */
58
59 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
60 struct const_ref_global_fun_base
61 {
62   typedef typename remove_reference<Type>::type result_type;
63
64   template<typename ChainedPtr>
65
66 #if !defined(BOOST_NO_SFINAE)
67   typename disable_if<
68     is_convertible<const ChainedPtr&,Value>,Type>::type
69 #else
70   Type
71 #endif
72
73   operator()(const ChainedPtr& x)const
74   {
75     return operator()(*x);
76   }
77
78   Type operator()(Value x)const
79   {
80     return PtrToFunction(x);
81   }
82
83   Type operator()(
84     const reference_wrapper<
85       typename remove_reference<Value>::type>& x)const
86   { 
87     return operator()(x.get());
88   }
89
90   Type operator()(
91     const reference_wrapper<
92       typename remove_const<
93         typename remove_reference<Value>::type>::type>& x,int=0)const
94   { 
95     return operator()(x.get());
96   }
97 };
98
99 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
100 struct non_const_ref_global_fun_base
101 {
102   typedef typename remove_reference<Type>::type result_type;
103
104   template<typename ChainedPtr>
105
106 #if !defined(BOOST_NO_SFINAE)
107   typename disable_if<
108     is_convertible<ChainedPtr&,Value>,Type>::type
109 #else
110   Type
111 #endif
112
113   operator()(const ChainedPtr& x)const
114   {
115     return operator()(*x);
116   }
117
118   Type operator()(Value x)const
119   {
120     return PtrToFunction(x);
121   }
122
123   Type operator()(
124     const reference_wrapper<
125       typename remove_reference<Value>::type>& x)const
126   { 
127     return operator()(x.get());
128   }
129 };
130
131 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
132 struct non_ref_global_fun_base
133 {
134   typedef typename remove_reference<Type>::type result_type;
135
136   template<typename ChainedPtr>
137
138 #if !defined(BOOST_NO_SFINAE)
139   typename disable_if<
140     is_convertible<const ChainedPtr&,const Value&>,Type>::type
141 #else
142   Type
143 #endif
144
145   operator()(const ChainedPtr& x)const
146   {
147     return operator()(*x);
148   }
149
150   Type operator()(const Value& x)const
151   {
152     return PtrToFunction(x);
153   }
154
155   Type operator()(const reference_wrapper<const Value>& x)const
156   { 
157     return operator()(x.get());
158   }
159
160   Type operator()(
161     const reference_wrapper<
162       typename remove_const<Value>::type>& x,int=0)const
163   { 
164     return operator()(x.get());
165   }
166 };
167
168 } /* namespace multi_index::detail */
169
170 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
171 struct global_fun:
172   mpl::if_c<
173     is_reference<Value>::value,
174     typename mpl::if_c<
175       is_const<typename remove_reference<Value>::type>::value,
176       detail::const_ref_global_fun_base<Value,Type,PtrToFunction>,
177       detail::non_const_ref_global_fun_base<Value,Type,PtrToFunction>
178     >::type,
179     detail::non_ref_global_fun_base<Value,Type,PtrToFunction>
180   >::type
181 {
182 };
183
184 } /* namespace multi_index */
185
186 } /* namespace boost */
187
188 #endif