| 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) |
|---|
| 5 | * |
|---|
| 6 | * See http://www.boost.org/libs/multi_index for library home page. |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #ifndef BOOST_MULTI_INDEX_MEM_FUN_HPP |
|---|
| 10 | #define BOOST_MULTI_INDEX_MEM_FUN_HPP |
|---|
| 11 | |
|---|
| 12 | #if defined(_MSC_VER)&&(_MSC_VER>=1200) |
|---|
| 13 | #pragma once |
|---|
| 14 | #endif |
|---|
| 15 | |
|---|
| 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> |
|---|
| 20 | |
|---|
| 21 | #if !defined(BOOST_NO_SFINAE) |
|---|
| 22 | #include <boost/type_traits/is_convertible.hpp> |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | namespace boost{ |
|---|
| 26 | |
|---|
| 27 | template<class T> class reference_wrapper; /* fwd decl. */ |
|---|
| 28 | |
|---|
| 29 | namespace multi_index{ |
|---|
| 30 | |
|---|
| 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*>.) |
|---|
| 40 | */ |
|---|
| 41 | |
|---|
| 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. |
|---|
| 50 | */ |
|---|
| 51 | |
|---|
| 52 | template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const> |
|---|
| 53 | struct const_mem_fun |
|---|
| 54 | { |
|---|
| 55 | typedef typename remove_reference<Type>::type result_type; |
|---|
| 56 | |
|---|
| 57 | template<typename ChainedPtr> |
|---|
| 58 | |
|---|
| 59 | #if !defined(BOOST_NO_SFINAE) |
|---|
| 60 | typename disable_if< |
|---|
| 61 | is_convertible<const ChainedPtr&,const Class&>,Type>::type |
|---|
| 62 | #else |
|---|
| 63 | Type |
|---|
| 64 | #endif |
|---|
| 65 | |
|---|
| 66 | operator()(const ChainedPtr& x)const |
|---|
| 67 | { |
|---|
| 68 | return operator()(*x); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | Type operator()(const Class& x)const |
|---|
| 72 | { |
|---|
| 73 | return (x.*PtrToMemberFunction)(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | Type operator()(const reference_wrapper<const Class>& x)const |
|---|
| 77 | { |
|---|
| 78 | return operator()(x.get()); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | Type operator()(const reference_wrapper<Class>& x,int=0)const |
|---|
| 82 | { |
|---|
| 83 | return operator()(x.get()); |
|---|
| 84 | } |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()> |
|---|
| 88 | struct mem_fun |
|---|
| 89 | { |
|---|
| 90 | typedef typename remove_reference<Type>::type result_type; |
|---|
| 91 | |
|---|
| 92 | template<typename ChainedPtr> |
|---|
| 93 | |
|---|
| 94 | #if !defined(BOOST_NO_SFINAE) |
|---|
| 95 | typename disable_if< |
|---|
| 96 | is_convertible<ChainedPtr&,Class&>,Type>::type |
|---|
| 97 | #else |
|---|
| 98 | Type |
|---|
| 99 | #endif |
|---|
| 100 | |
|---|
| 101 | operator()(const ChainedPtr& x)const |
|---|
| 102 | { |
|---|
| 103 | return operator()(*x); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | Type operator()(Class& x)const |
|---|
| 107 | { |
|---|
| 108 | return (x.*PtrToMemberFunction)(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | Type operator()(const reference_wrapper<Class>& x)const |
|---|
| 112 | { |
|---|
| 113 | return operator()(x.get()); |
|---|
| 114 | } |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 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 |
|---|
| 125 | */ |
|---|
| 126 | |
|---|
| 127 | template< |
|---|
| 128 | class Class,typename Type, |
|---|
| 129 | typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction> |
|---|
| 130 | struct const_mem_fun_explicit |
|---|
| 131 | { |
|---|
| 132 | typedef typename remove_reference<Type>::type result_type; |
|---|
| 133 | |
|---|
| 134 | template<typename ChainedPtr> |
|---|
| 135 | |
|---|
| 136 | #if !defined(BOOST_NO_SFINAE) |
|---|
| 137 | typename disable_if< |
|---|
| 138 | is_convertible<const ChainedPtr&,const Class&>,Type>::type |
|---|
| 139 | #else |
|---|
| 140 | Type |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | operator()(const ChainedPtr& x)const |
|---|
| 144 | { |
|---|
| 145 | return operator()(*x); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | Type operator()(const Class& x)const |
|---|
| 149 | { |
|---|
| 150 | return (x.*PtrToMemberFunction)(); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | Type operator()(const reference_wrapper<const Class>& x)const |
|---|
| 154 | { |
|---|
| 155 | return operator()(x.get()); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | Type operator()(const reference_wrapper<Class>& x,int=0)const |
|---|
| 159 | { |
|---|
| 160 | return operator()(x.get()); |
|---|
| 161 | } |
|---|
| 162 | }; |
|---|
| 163 | |
|---|
| 164 | template< |
|---|
| 165 | class Class,typename Type, |
|---|
| 166 | typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction> |
|---|
| 167 | struct mem_fun_explicit |
|---|
| 168 | { |
|---|
| 169 | typedef typename remove_reference<Type>::type result_type; |
|---|
| 170 | |
|---|
| 171 | template<typename ChainedPtr> |
|---|
| 172 | |
|---|
| 173 | #if !defined(BOOST_NO_SFINAE) |
|---|
| 174 | typename disable_if< |
|---|
| 175 | is_convertible<ChainedPtr&,Class&>,Type>::type |
|---|
| 176 | #else |
|---|
| 177 | Type |
|---|
| 178 | #endif |
|---|
| 179 | |
|---|
| 180 | operator()(const ChainedPtr& x)const |
|---|
| 181 | { |
|---|
| 182 | return operator()(*x); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | Type operator()(Class& x)const |
|---|
| 186 | { |
|---|
| 187 | return (x.*PtrToMemberFunction)(); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | Type operator()(const reference_wrapper<Class>& x)const |
|---|
| 191 | { |
|---|
| 192 | return operator()(x.get()); |
|---|
| 193 | } |
|---|
| 194 | }; |
|---|
| 195 | |
|---|
| 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. |
|---|
| 198 | */ |
|---|
| 199 | |
|---|
| 200 | #if defined(BOOST_MSVC)&&(BOOST_MSVC<1300) |
|---|
| 201 | |
|---|
| 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 > |
|---|
| 208 | |
|---|
| 209 | #else |
|---|
| 210 | |
|---|
| 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 > |
|---|
| 215 | |
|---|
| 216 | #endif |
|---|
| 217 | |
|---|
| 218 | } /* namespace multi_index */ |
|---|
| 219 | |
|---|
| 220 | } /* namespace boost */ |
|---|
| 221 | |
|---|
| 222 | #endif |
|---|