Chris@101
|
1 /* Copyright 2003-2013 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@101
|
12 #if defined(_MSC_VER)
|
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@101
|
115 *
|
Chris@101
|
116 * MSVC++ 6.0 support has been dropped and [const_]mem_fun_explicit is
|
Chris@101
|
117 * deprecated.
|
Chris@16
|
118 */
|
Chris@16
|
119
|
Chris@16
|
120 template<
|
Chris@16
|
121 class Class,typename Type,
|
Chris@16
|
122 typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
|
Chris@16
|
123 struct const_mem_fun_explicit
|
Chris@16
|
124 {
|
Chris@16
|
125 typedef typename remove_reference<Type>::type result_type;
|
Chris@16
|
126
|
Chris@16
|
127 template<typename ChainedPtr>
|
Chris@16
|
128
|
Chris@16
|
129 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
130 typename disable_if<
|
Chris@16
|
131 is_convertible<const ChainedPtr&,const Class&>,Type>::type
|
Chris@16
|
132 #else
|
Chris@16
|
133 Type
|
Chris@16
|
134 #endif
|
Chris@16
|
135
|
Chris@16
|
136 operator()(const ChainedPtr& x)const
|
Chris@16
|
137 {
|
Chris@16
|
138 return operator()(*x);
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 Type operator()(const Class& x)const
|
Chris@16
|
142 {
|
Chris@16
|
143 return (x.*PtrToMemberFunction)();
|
Chris@16
|
144 }
|
Chris@16
|
145
|
Chris@16
|
146 Type operator()(const reference_wrapper<const Class>& x)const
|
Chris@16
|
147 {
|
Chris@16
|
148 return operator()(x.get());
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 Type operator()(const reference_wrapper<Class>& x)const
|
Chris@16
|
152 {
|
Chris@16
|
153 return operator()(x.get());
|
Chris@16
|
154 }
|
Chris@16
|
155 };
|
Chris@16
|
156
|
Chris@16
|
157 template<
|
Chris@16
|
158 class Class,typename Type,
|
Chris@16
|
159 typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
|
Chris@16
|
160 struct mem_fun_explicit
|
Chris@16
|
161 {
|
Chris@16
|
162 typedef typename remove_reference<Type>::type result_type;
|
Chris@16
|
163
|
Chris@16
|
164 template<typename ChainedPtr>
|
Chris@16
|
165
|
Chris@16
|
166 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
167 typename disable_if<
|
Chris@16
|
168 is_convertible<ChainedPtr&,Class&>,Type>::type
|
Chris@16
|
169 #else
|
Chris@16
|
170 Type
|
Chris@16
|
171 #endif
|
Chris@16
|
172
|
Chris@16
|
173 operator()(const ChainedPtr& x)const
|
Chris@16
|
174 {
|
Chris@16
|
175 return operator()(*x);
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178 Type operator()(Class& x)const
|
Chris@16
|
179 {
|
Chris@16
|
180 return (x.*PtrToMemberFunction)();
|
Chris@16
|
181 }
|
Chris@16
|
182
|
Chris@16
|
183 Type operator()(const reference_wrapper<Class>& x)const
|
Chris@16
|
184 {
|
Chris@16
|
185 return operator()(x.get());
|
Chris@16
|
186 }
|
Chris@16
|
187 };
|
Chris@16
|
188
|
Chris@101
|
189 /* BOOST_MULTI_INDEX_CONST_MEM_FUN and BOOST_MULTI_INDEX_MEM_FUN used to
|
Chris@101
|
190 * resolve to [const_]mem_fun_explicit for MSVC++ 6.0 and to
|
Chris@101
|
191 * [const_]mem_fun otherwise. Support for this compiler having been dropped,
|
Chris@101
|
192 * they are now just wrappers over [const_]mem_fun kept for backwards-
|
Chris@101
|
193 * compatibility reasons.
|
Chris@16
|
194 */
|
Chris@16
|
195
|
Chris@16
|
196 #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
|
Chris@16
|
197 ::boost::multi_index::const_mem_fun< Class,Type,&Class::MemberFunName >
|
Chris@16
|
198 #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
|
Chris@16
|
199 ::boost::multi_index::mem_fun< Class,Type,&Class::MemberFunName >
|
Chris@16
|
200
|
Chris@16
|
201 } /* namespace multi_index */
|
Chris@16
|
202
|
Chris@16
|
203 } /* namespace boost */
|
Chris@16
|
204
|
Chris@16
|
205 #endif
|