1 /* Copyright 2003-2015 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/detail/workaround.hpp>
18 #include <boost/mpl/if.hpp>
19 #include <boost/type_traits/is_const.hpp>
20 #include <boost/type_traits/is_reference.hpp>
21 #include <boost/type_traits/remove_const.hpp>
22 #include <boost/type_traits/remove_reference.hpp>
23 #include <boost/utility/enable_if.hpp>
25 #if !defined(BOOST_NO_SFINAE)
26 #include <boost/type_traits/is_convertible.hpp>
31 template<class T> class reference_wrapper; /* fwd decl. */
33 namespace multi_index{
37 /* global_fun is a read-only key extractor from Value based on a given global
38 * (or static member) function with signature:
40 * Type f([const] Value [&]);
42 * Additionally, global_fun and const_global_fun are overloaded to support
43 * referece_wrappers of Value and "chained pointers" to Value's. By chained
44 * pointer to T we mean a type P such that, given a p of Type P
45 * *...n...*x is convertible to T&, for some n>=1.
46 * Examples of chained pointers are raw and smart pointers, iterators and
47 * arbitrary combinations of these (vg. T** or unique_ptr<T*>.)
50 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
51 struct const_ref_global_fun_base
53 typedef typename remove_reference<Type>::type result_type;
55 template<typename ChainedPtr>
57 #if !defined(BOOST_NO_SFINAE)
59 is_convertible<const ChainedPtr&,Value>,Type>::type
64 operator()(const ChainedPtr& x)const
66 return operator()(*x);
69 Type operator()(Value x)const
71 return PtrToFunction(x);
75 const reference_wrapper<
76 typename remove_reference<Value>::type>& x)const
78 return operator()(x.get());
82 const reference_wrapper<
83 typename remove_const<
84 typename remove_reference<Value>::type>::type>& x
86 #if BOOST_WORKAROUND(BOOST_MSVC,==1310)
87 /* http://lists.boost.org/Archives/boost/2015/10/226135.php */
93 return operator()(x.get());
97 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
98 struct non_const_ref_global_fun_base
100 typedef typename remove_reference<Type>::type result_type;
102 template<typename ChainedPtr>
104 #if !defined(BOOST_NO_SFINAE)
106 is_convertible<ChainedPtr&,Value>,Type>::type
111 operator()(const ChainedPtr& x)const
113 return operator()(*x);
116 Type operator()(Value x)const
118 return PtrToFunction(x);
122 const reference_wrapper<
123 typename remove_reference<Value>::type>& x)const
125 return operator()(x.get());
129 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
130 struct non_ref_global_fun_base
132 typedef typename remove_reference<Type>::type result_type;
134 template<typename ChainedPtr>
136 #if !defined(BOOST_NO_SFINAE)
138 is_convertible<const ChainedPtr&,const Value&>,Type>::type
143 operator()(const ChainedPtr& x)const
145 return operator()(*x);
148 Type operator()(const Value& x)const
150 return PtrToFunction(x);
153 Type operator()(const reference_wrapper<const Value>& x)const
155 return operator()(x.get());
159 const reference_wrapper<typename remove_const<Value>::type>& x)const
161 return operator()(x.get());
165 } /* namespace multi_index::detail */
167 template<class Value,typename Type,Type (*PtrToFunction)(Value)>
170 is_reference<Value>::value,
172 is_const<typename remove_reference<Value>::type>::value,
173 detail::const_ref_global_fun_base<Value,Type,PtrToFunction>,
174 detail::non_const_ref_global_fun_base<Value,Type,PtrToFunction>
176 detail::non_ref_global_fun_base<Value,Type,PtrToFunction>
181 } /* namespace multi_index */
183 } /* namespace boost */