1 /* Copyright 2003-2006 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 /* NB. Some overloads of operator() have an extra dummy parameter int=0.
43 * This disambiguator serves several purposes:
44 * - Without it, MSVC++ 6.0 incorrectly regards some overloads as
45 * specializations of a previous member function template.
46 * - MSVC++ 6.0/7.0 seem to incorrectly treat some different memfuns
47 * as if they have the same signature.
48 * - If remove_const is broken due to lack of PTS, int=0 avoids the
49 * declaration of memfuns with identical signature.
52 template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const>
55 typedef typename remove_reference<Type>::type result_type;
57 template<typename ChainedPtr>
59 #if !defined(BOOST_NO_SFINAE)
61 is_convertible<const ChainedPtr&,const Class&>,Type>::type
66 operator()(const ChainedPtr& x)const
68 return operator()(*x);
71 Type operator()(const Class& x)const
73 return (x.*PtrToMemberFunction)();
76 Type operator()(const reference_wrapper<const Class>& x)const
78 return operator()(x.get());
81 Type operator()(const reference_wrapper<Class>& x,int=0)const
83 return operator()(x.get());
87 template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()>
90 typedef typename remove_reference<Type>::type result_type;
92 template<typename ChainedPtr>
94 #if !defined(BOOST_NO_SFINAE)
96 is_convertible<ChainedPtr&,Class&>,Type>::type
101 operator()(const ChainedPtr& x)const
103 return operator()(*x);
106 Type operator()(Class& x)const
108 return (x.*PtrToMemberFunction)();
111 Type operator()(const reference_wrapper<Class>& x)const
113 return operator()(x.get());
117 /* MSVC++ 6.0 has problems with const member functions as non-type template
118 * parameters, somehow it takes them as non-const. mem_fun_explicit workarounds
119 * this defficiency by accepting an extra type parameter that specifies the
120 * signature of he member function. The workaround was found at:
121 * Daniel, C.:"Re: weird typedef problem in VC",
122 * news:microsoft.public.vc.language, 21st nov 2002,
123 * http://groups.google.com/groups?
124 * hl=en&lr=&ie=UTF-8&selm=ukwvg3O0BHA.1512%40tkmsftngp05
128 class Class,typename Type,
129 typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
130 struct const_mem_fun_explicit
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 Class&>,Type>::type
143 operator()(const ChainedPtr& x)const
145 return operator()(*x);
148 Type operator()(const Class& x)const
150 return (x.*PtrToMemberFunction)();
153 Type operator()(const reference_wrapper<const Class>& x)const
155 return operator()(x.get());
158 Type operator()(const reference_wrapper<Class>& x,int=0)const
160 return operator()(x.get());
165 class Class,typename Type,
166 typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
167 struct mem_fun_explicit
169 typedef typename remove_reference<Type>::type result_type;
171 template<typename ChainedPtr>
173 #if !defined(BOOST_NO_SFINAE)
175 is_convertible<ChainedPtr&,Class&>,Type>::type
180 operator()(const ChainedPtr& x)const
182 return operator()(*x);
185 Type operator()(Class& x)const
187 return (x.*PtrToMemberFunction)();
190 Type operator()(const reference_wrapper<Class>& x)const
192 return operator()(x.get());
196 /* BOOST_MULTI_INDEX_CONST_MEM_FUN and BOOST_MULTI_INDEX_MEM_FUN resolve to
197 * mem_fun_explicit for MSVC++ 6.0 and to [const_]mem_fun otherwise.
200 #if defined(BOOST_MSVC)&&(BOOST_MSVC<1300)
202 #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
203 ::boost::multi_index::const_mem_fun_explicit<\
204 Class,Type,Type (Class::*)()const,&Class::MemberFunName >
205 #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
206 ::boost::multi_index::mem_fun_explicit<\
207 Class,Type,Type (Class::*)(),&Class::MemberFunName >
211 #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
212 ::boost::multi_index::const_mem_fun< Class,Type,&Class::MemberFunName >
213 #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
214 ::boost::multi_index::mem_fun< Class,Type,&Class::MemberFunName >
218 } /* namespace multi_index */
220 } /* namespace boost */