fix doc example typo
[boost.git] / boost / multi_index / global_fun.hpp
blob8520e4d0c7a41fd5a83fafc415d1fa7b5b2ef4ca
1 /* Copyright 2003-2008 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/multi_index for library home page.
7 */
9 #ifndef BOOST_MULTI_INDEX_GLOBAL_FUN_HPP
10 #define BOOST_MULTI_INDEX_GLOBAL_FUN_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/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>
24 #if !defined(BOOST_NO_SFINAE)
25 #include <boost/type_traits/is_convertible.hpp>
26 #endif
28 namespace boost{
30 template<class T> class reference_wrapper; /* fwd decl. */
32 namespace multi_index{
34 namespace detail{
36 /* global_fun is a read-only key extractor from Value based on a given global
37 * (or static member) function with signature:
39 * Type f([const] Value [&]);
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*>.)
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.
59 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
60 struct const_ref_global_fun_base
62 typedef typename remove_reference<Type>::type result_type;
64 template<typename ChainedPtr>
66 #if !defined(BOOST_NO_SFINAE)
67 typename disable_if<
68 is_convertible<const ChainedPtr&,Value>,Type>::type
69 #else
70 Type
71 #endif
73 operator()(const ChainedPtr& x)const
75 return operator()(*x);
78 Type operator()(Value x)const
80 return PtrToFunction(x);
83 Type operator()(
84 const reference_wrapper<
85 typename remove_reference<Value>::type>& x)const
87 return operator()(x.get());
90 Type operator()(
91 const reference_wrapper<
92 typename remove_const<
93 typename remove_reference<Value>::type>::type>& x,int=0)const
95 return operator()(x.get());
99 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
100 struct non_const_ref_global_fun_base
102 typedef typename remove_reference<Type>::type result_type;
104 template<typename ChainedPtr>
106 #if !defined(BOOST_NO_SFINAE)
107 typename disable_if<
108 is_convertible<ChainedPtr&,Value>,Type>::type
109 #else
110 Type
111 #endif
113 operator()(const ChainedPtr& x)const
115 return operator()(*x);
118 Type operator()(Value x)const
120 return PtrToFunction(x);
123 Type operator()(
124 const reference_wrapper<
125 typename remove_reference<Value>::type>& x)const
127 return operator()(x.get());
131 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
132 struct non_ref_global_fun_base
134 typedef typename remove_reference<Type>::type result_type;
136 template<typename ChainedPtr>
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
145 operator()(const ChainedPtr& x)const
147 return operator()(*x);
150 Type operator()(const Value& x)const
152 return PtrToFunction(x);
155 Type operator()(const reference_wrapper<const Value>& x)const
157 return operator()(x.get());
160 Type operator()(
161 const reference_wrapper<
162 typename remove_const<Value>::type>& x,int=0)const
164 return operator()(x.get());
168 } /* namespace multi_index::detail */
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
184 } /* namespace multi_index */
186 } /* namespace boost */
188 #endif