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)
6 * See http://www.boost.org/libs/multi_index for library home page.
9 #ifndef BOOST_MULTI_INDEX_MEM_FUN_HPP
10 #define BOOST_MULTI_INDEX_MEM_FUN_HPP
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
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/remove_reference.hpp>
19 #include <boost/utility/enable_if.hpp>
21 #if !defined(BOOST_NO_SFINAE)
22 #include <boost/type_traits/is_convertible.hpp>
27 template<class T> class reference_wrapper; /* fwd decl. */
29 namespace multi_index{
31 /* mem_fun implements a read-only key extractor based on a given non-const
32 * member function of a class.
33 * const_mem_fun does the same for const member functions.
34 * Additionally, mem_fun and const_mem_fun are overloaded to support
35 * referece_wrappers of T and "chained pointers" to T's. By chained pointer
36 * to T we mean a type P such that, given a p of Type P
37 * *...n...*x is convertible to T&, for some n>=1.
38 * Examples of chained pointers are raw and smart pointers, iterators and
39 * arbitrary combinations of these (vg. T** or auto_ptr<T*>.)
42 template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const>
45 typedef typename remove_reference<Type>::type result_type;
47 template<typename ChainedPtr>
49 #if !defined(BOOST_NO_SFINAE)
51 is_convertible<const ChainedPtr&,const Class&>,Type>::type
56 operator()(const ChainedPtr& x)const
58 return operator()(*x);
61 Type operator()(const Class& x)const
63 return (x.*PtrToMemberFunction)();
66 Type operator()(const reference_wrapper<const Class>& x)const
68 return operator()(x.get());
71 Type operator()(const reference_wrapper<Class>& x)const
73 return operator()(x.get());
77 template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()>
80 typedef typename remove_reference<Type>::type result_type;
82 template<typename ChainedPtr>
84 #if !defined(BOOST_NO_SFINAE)
86 is_convertible<ChainedPtr&,Class&>,Type>::type
91 operator()(const ChainedPtr& x)const
93 return operator()(*x);
96 Type operator()(Class& x)const
98 return (x.*PtrToMemberFunction)();
101 Type operator()(const reference_wrapper<Class>& x)const
103 return operator()(x.get());
107 /* MSVC++ 6.0 has problems with const member functions as non-type template
108 * parameters, somehow it takes them as non-const. const_mem_fun_explicit
109 * workarounds this deficiency by accepting an extra type parameter that
110 * specifies the signature of the member function. The workaround was found at:
111 * Daniel, C.:"Re: weird typedef problem in VC",
112 * news:microsoft.public.vc.language, 21st nov 2002,
113 * http://groups.google.com/groups?
114 * hl=en&lr=&ie=UTF-8&selm=ukwvg3O0BHA.1512%40tkmsftngp05
118 class Class,typename Type,
119 typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
120 struct const_mem_fun_explicit
122 typedef typename remove_reference<Type>::type result_type;
124 template<typename ChainedPtr>
126 #if !defined(BOOST_NO_SFINAE)
128 is_convertible<const ChainedPtr&,const Class&>,Type>::type
133 operator()(const ChainedPtr& x)const
135 return operator()(*x);
138 Type operator()(const Class& x)const
140 return (x.*PtrToMemberFunction)();
143 Type operator()(const reference_wrapper<const Class>& x)const
145 return operator()(x.get());
148 Type operator()(const reference_wrapper<Class>& x)const
150 return operator()(x.get());
155 class Class,typename Type,
156 typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
157 struct mem_fun_explicit
159 typedef typename remove_reference<Type>::type result_type;
161 template<typename ChainedPtr>
163 #if !defined(BOOST_NO_SFINAE)
165 is_convertible<ChainedPtr&,Class&>,Type>::type
170 operator()(const ChainedPtr& x)const
172 return operator()(*x);
175 Type operator()(Class& x)const
177 return (x.*PtrToMemberFunction)();
180 Type operator()(const reference_wrapper<Class>& x)const
182 return operator()(x.get());
186 /* BOOST_MULTI_INDEX_CONST_MEM_FUN and BOOST_MULTI_INDEX_MEM_FUN resolve to
187 * [const_]mem_fun_explicit for MSVC++ 6.0 and to [const_]mem_fun otherwise.
190 #if defined(BOOST_MSVC)&&(BOOST_MSVC<1300)
192 #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
193 ::boost::multi_index::const_mem_fun_explicit<\
194 Class,Type,Type (Class::*)()const,&Class::MemberFunName >
195 #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
196 ::boost::multi_index::mem_fun_explicit<\
197 Class,Type,Type (Class::*)(),&Class::MemberFunName >
201 #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
202 ::boost::multi_index::const_mem_fun< Class,Type,&Class::MemberFunName >
203 #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
204 ::boost::multi_index::mem_fun< Class,Type,&Class::MemberFunName >
208 } /* namespace multi_index */
210 } /* namespace boost */