Chris@16
|
1 /* Copyright 2003-2008 Joaquin M Lopez Munoz.
|
Chris@16
|
2 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 *
|
Chris@16
|
6 * See http://www.boost.org/libs/multi_index for library home page.
|
Chris@16
|
7 */
|
Chris@16
|
8
|
Chris@16
|
9 #ifndef BOOST_MULTI_INDEX_MEM_FUN_HPP
|
Chris@16
|
10 #define BOOST_MULTI_INDEX_MEM_FUN_HPP
|
Chris@16
|
11
|
Chris@16
|
12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
|
Chris@16
|
13 #pragma once
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
|
Chris@16
|
17 #include <boost/mpl/if.hpp>
|
Chris@16
|
18 #include <boost/type_traits/remove_reference.hpp>
|
Chris@16
|
19 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
22 #include <boost/type_traits/is_convertible.hpp>
|
Chris@16
|
23 #endif
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost{
|
Chris@16
|
26
|
Chris@16
|
27 template<class T> class reference_wrapper; /* fwd decl. */
|
Chris@16
|
28
|
Chris@16
|
29 namespace multi_index{
|
Chris@16
|
30
|
Chris@16
|
31 /* mem_fun implements a read-only key extractor based on a given non-const
|
Chris@16
|
32 * member function of a class.
|
Chris@16
|
33 * const_mem_fun does the same for const member functions.
|
Chris@16
|
34 * Additionally, mem_fun and const_mem_fun are overloaded to support
|
Chris@16
|
35 * referece_wrappers of T and "chained pointers" to T's. By chained pointer
|
Chris@16
|
36 * to T we mean a type P such that, given a p of Type P
|
Chris@16
|
37 * *...n...*x is convertible to T&, for some n>=1.
|
Chris@16
|
38 * Examples of chained pointers are raw and smart pointers, iterators and
|
Chris@16
|
39 * arbitrary combinations of these (vg. T** or auto_ptr<T*>.)
|
Chris@16
|
40 */
|
Chris@16
|
41
|
Chris@16
|
42 template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const>
|
Chris@16
|
43 struct const_mem_fun
|
Chris@16
|
44 {
|
Chris@16
|
45 typedef typename remove_reference<Type>::type result_type;
|
Chris@16
|
46
|
Chris@16
|
47 template<typename ChainedPtr>
|
Chris@16
|
48
|
Chris@16
|
49 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
50 typename disable_if<
|
Chris@16
|
51 is_convertible<const ChainedPtr&,const Class&>,Type>::type
|
Chris@16
|
52 #else
|
Chris@16
|
53 Type
|
Chris@16
|
54 #endif
|
Chris@16
|
55
|
Chris@16
|
56 operator()(const ChainedPtr& x)const
|
Chris@16
|
57 {
|
Chris@16
|
58 return operator()(*x);
|
Chris@16
|
59 }
|
Chris@16
|
60
|
Chris@16
|
61 Type operator()(const Class& x)const
|
Chris@16
|
62 {
|
Chris@16
|
63 return (x.*PtrToMemberFunction)();
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 Type operator()(const reference_wrapper<const Class>& x)const
|
Chris@16
|
67 {
|
Chris@16
|
68 return operator()(x.get());
|
Chris@16
|
69 }
|
Chris@16
|
70
|
Chris@16
|
71 Type operator()(const reference_wrapper<Class>& x)const
|
Chris@16
|
72 {
|
Chris@16
|
73 return operator()(x.get());
|
Chris@16
|
74 }
|
Chris@16
|
75 };
|
Chris@16
|
76
|
Chris@16
|
77 template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()>
|
Chris@16
|
78 struct mem_fun
|
Chris@16
|
79 {
|
Chris@16
|
80 typedef typename remove_reference<Type>::type result_type;
|
Chris@16
|
81
|
Chris@16
|
82 template<typename ChainedPtr>
|
Chris@16
|
83
|
Chris@16
|
84 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
85 typename disable_if<
|
Chris@16
|
86 is_convertible<ChainedPtr&,Class&>,Type>::type
|
Chris@16
|
87 #else
|
Chris@16
|
88 Type
|
Chris@16
|
89 #endif
|
Chris@16
|
90
|
Chris@16
|
91 operator()(const ChainedPtr& x)const
|
Chris@16
|
92 {
|
Chris@16
|
93 return operator()(*x);
|
Chris@16
|
94 }
|
Chris@16
|
95
|
Chris@16
|
96 Type operator()(Class& x)const
|
Chris@16
|
97 {
|
Chris@16
|
98 return (x.*PtrToMemberFunction)();
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 Type operator()(const reference_wrapper<Class>& x)const
|
Chris@16
|
102 {
|
Chris@16
|
103 return operator()(x.get());
|
Chris@16
|
104 }
|
Chris@16
|
105 };
|
Chris@16
|
106
|
Chris@16
|
107 /* MSVC++ 6.0 has problems with const member functions as non-type template
|
Chris@16
|
108 * parameters, somehow it takes them as non-const. const_mem_fun_explicit
|
Chris@16
|
109 * workarounds this deficiency by accepting an extra type parameter that
|
Chris@16
|
110 * specifies the signature of the member function. The workaround was found at:
|
Chris@16
|
111 * Daniel, C.:"Re: weird typedef problem in VC",
|
Chris@16
|
112 * news:microsoft.public.vc.language, 21st nov 2002,
|
Chris@16
|
113 * http://groups.google.com/groups?
|
Chris@16
|
114 * hl=en&lr=&ie=UTF-8&selm=ukwvg3O0BHA.1512%40tkmsftngp05
|
Chris@16
|
115 */
|
Chris@16
|
116
|
Chris@16
|
117 template<
|
Chris@16
|
118 class Class,typename Type,
|
Chris@16
|
119 typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
|
Chris@16
|
120 struct const_mem_fun_explicit
|
Chris@16
|
121 {
|
Chris@16
|
122 typedef typename remove_reference<Type>::type result_type;
|
Chris@16
|
123
|
Chris@16
|
124 template<typename ChainedPtr>
|
Chris@16
|
125
|
Chris@16
|
126 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
127 typename disable_if<
|
Chris@16
|
128 is_convertible<const ChainedPtr&,const Class&>,Type>::type
|
Chris@16
|
129 #else
|
Chris@16
|
130 Type
|
Chris@16
|
131 #endif
|
Chris@16
|
132
|
Chris@16
|
133 operator()(const ChainedPtr& x)const
|
Chris@16
|
134 {
|
Chris@16
|
135 return operator()(*x);
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 Type operator()(const Class& x)const
|
Chris@16
|
139 {
|
Chris@16
|
140 return (x.*PtrToMemberFunction)();
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 Type operator()(const reference_wrapper<const Class>& x)const
|
Chris@16
|
144 {
|
Chris@16
|
145 return operator()(x.get());
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 Type operator()(const reference_wrapper<Class>& x)const
|
Chris@16
|
149 {
|
Chris@16
|
150 return operator()(x.get());
|
Chris@16
|
151 }
|
Chris@16
|
152 };
|
Chris@16
|
153
|
Chris@16
|
154 template<
|
Chris@16
|
155 class Class,typename Type,
|
Chris@16
|
156 typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
|
Chris@16
|
157 struct mem_fun_explicit
|
Chris@16
|
158 {
|
Chris@16
|
159 typedef typename remove_reference<Type>::type result_type;
|
Chris@16
|
160
|
Chris@16
|
161 template<typename ChainedPtr>
|
Chris@16
|
162
|
Chris@16
|
163 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
164 typename disable_if<
|
Chris@16
|
165 is_convertible<ChainedPtr&,Class&>,Type>::type
|
Chris@16
|
166 #else
|
Chris@16
|
167 Type
|
Chris@16
|
168 #endif
|
Chris@16
|
169
|
Chris@16
|
170 operator()(const ChainedPtr& x)const
|
Chris@16
|
171 {
|
Chris@16
|
172 return operator()(*x);
|
Chris@16
|
173 }
|
Chris@16
|
174
|
Chris@16
|
175 Type operator()(Class& x)const
|
Chris@16
|
176 {
|
Chris@16
|
177 return (x.*PtrToMemberFunction)();
|
Chris@16
|
178 }
|
Chris@16
|
179
|
Chris@16
|
180 Type operator()(const reference_wrapper<Class>& x)const
|
Chris@16
|
181 {
|
Chris@16
|
182 return operator()(x.get());
|
Chris@16
|
183 }
|
Chris@16
|
184 };
|
Chris@16
|
185
|
Chris@16
|
186 /* BOOST_MULTI_INDEX_CONST_MEM_FUN and BOOST_MULTI_INDEX_MEM_FUN resolve to
|
Chris@16
|
187 * [const_]mem_fun_explicit for MSVC++ 6.0 and to [const_]mem_fun otherwise.
|
Chris@16
|
188 */
|
Chris@16
|
189
|
Chris@16
|
190 #if defined(BOOST_MSVC)&&(BOOST_MSVC<1300)
|
Chris@16
|
191
|
Chris@16
|
192 #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
|
Chris@16
|
193 ::boost::multi_index::const_mem_fun_explicit<\
|
Chris@16
|
194 Class,Type,Type (Class::*)()const,&Class::MemberFunName >
|
Chris@16
|
195 #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
|
Chris@16
|
196 ::boost::multi_index::mem_fun_explicit<\
|
Chris@16
|
197 Class,Type,Type (Class::*)(),&Class::MemberFunName >
|
Chris@16
|
198
|
Chris@16
|
199 #else
|
Chris@16
|
200
|
Chris@16
|
201 #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
|
Chris@16
|
202 ::boost::multi_index::const_mem_fun< Class,Type,&Class::MemberFunName >
|
Chris@16
|
203 #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
|
Chris@16
|
204 ::boost::multi_index::mem_fun< Class,Type,&Class::MemberFunName >
|
Chris@16
|
205
|
Chris@16
|
206 #endif
|
Chris@16
|
207
|
Chris@16
|
208 } /* namespace multi_index */
|
Chris@16
|
209
|
Chris@16
|
210 } /* namespace boost */
|
Chris@16
|
211
|
Chris@16
|
212 #endif
|