1 /* Copyright 2003-2013 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.
9 #ifndef BOOST_MULTI_INDEX_GLOBAL_FUN_HPP
10 #define BOOST_MULTI_INDEX_GLOBAL_FUN_HPP
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>
30 template<class T> class reference_wrapper; /* fwd decl. */
32 namespace multi_index{
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 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
50 struct const_ref_global_fun_base
52 typedef typename remove_reference<Type>::type result_type;
54 template<typename ChainedPtr>
56 #if !defined(BOOST_NO_SFINAE)
58 is_convertible<const ChainedPtr&,Value>,Type>::type
63 operator()(const ChainedPtr& x)const
65 return operator()(*x);
68 Type operator()(Value x)const
70 return PtrToFunction(x);
74 const reference_wrapper<
75 typename remove_reference<Value>::type>& x)const
77 return operator()(x.get());
81 const reference_wrapper<
82 typename remove_const<
83 typename remove_reference<Value>::type>::type>& x)const
85 return operator()(x.get());
89 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
90 struct non_const_ref_global_fun_base
92 typedef typename remove_reference<Type>::type result_type;
94 template<typename ChainedPtr>
96 #if !defined(BOOST_NO_SFINAE)
98 is_convertible<ChainedPtr&,Value>,Type>::type
103 operator()(const ChainedPtr& x)const
105 return operator()(*x);
108 Type operator()(Value x)const
110 return PtrToFunction(x);
114 const reference_wrapper<
115 typename remove_reference<Value>::type>& x)const
117 return operator()(x.get());
121 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
122 struct non_ref_global_fun_base
124 typedef typename remove_reference<Type>::type result_type;
126 template<typename ChainedPtr>
128 #if !defined(BOOST_NO_SFINAE)
130 is_convertible<const ChainedPtr&,const Value&>,Type>::type
135 operator()(const ChainedPtr& x)const
137 return operator()(*x);
140 Type operator()(const Value& x)const
142 return PtrToFunction(x);
145 Type operator()(const reference_wrapper<const Value>& x)const
147 return operator()(x.get());
151 const reference_wrapper<typename remove_const<Value>::type>& x)const
153 return operator()(x.get());
157 } /* namespace multi_index::detail */
159 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
162 is_reference<Value>::value,
164 is_const<typename remove_reference<Value>::type>::value,
165 detail::const_ref_global_fun_base<Value,Type,PtrToFunction>,
166 detail::non_const_ref_global_fun_base<Value,Type,PtrToFunction>
168 detail::non_ref_global_fun_base<Value,Type,PtrToFunction>
173 } /* namespace multi_index */
175 } /* namespace boost */