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_MEMBER_HPP
|
Chris@16
|
10 #define BOOST_MULTI_INDEX_MEMBER_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/is_const.hpp>
|
Chris@16
|
19 #include <boost/utility/enable_if.hpp>
|
Chris@16
|
20 #include <cstddef>
|
Chris@16
|
21
|
Chris@16
|
22 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
23 #include <boost/type_traits/is_convertible.hpp>
|
Chris@16
|
24 #endif
|
Chris@16
|
25
|
Chris@16
|
26 namespace boost{
|
Chris@16
|
27
|
Chris@16
|
28 template<class T> class reference_wrapper; /* fwd decl. */
|
Chris@16
|
29
|
Chris@16
|
30 namespace multi_index{
|
Chris@16
|
31
|
Chris@16
|
32 namespace detail{
|
Chris@16
|
33
|
Chris@16
|
34 /* member is a read/write key extractor for accessing a given
|
Chris@16
|
35 * member of a class.
|
Chris@16
|
36 * Additionally, member is overloaded to support referece_wrappers
|
Chris@16
|
37 * of T and "chained pointers" to T's. By chained pointer to T we mean
|
Chris@16
|
38 * a type P such that, given a p of Type P
|
Chris@16
|
39 * *...n...*x is convertible to T&, for some n>=1.
|
Chris@16
|
40 * Examples of chained pointers are raw and smart pointers, iterators and
|
Chris@16
|
41 * arbitrary combinations of these (vg. T** or auto_ptr<T*>.)
|
Chris@16
|
42 */
|
Chris@16
|
43
|
Chris@16
|
44 /* NB. Some overloads of operator() have an extra dummy parameter int=0.
|
Chris@16
|
45 * This disambiguator serves several purposes:
|
Chris@16
|
46 * - Without it, MSVC++ 6.0 incorrectly regards some overloads as
|
Chris@16
|
47 * specializations of a previous member function template.
|
Chris@16
|
48 * - MSVC++ 6.0/7.0 seem to incorrectly treat some different memfuns
|
Chris@16
|
49 * as if they have the same signature.
|
Chris@16
|
50 * - If remove_const is broken due to lack of PTS, int=0 avoids the
|
Chris@16
|
51 * declaration of memfuns with identical signature.
|
Chris@16
|
52 */
|
Chris@16
|
53
|
Chris@16
|
54 template<class Class,typename Type,Type Class::*PtrToMember>
|
Chris@16
|
55 struct const_member_base
|
Chris@16
|
56 {
|
Chris@16
|
57 typedef Type result_type;
|
Chris@16
|
58
|
Chris@16
|
59 template<typename ChainedPtr>
|
Chris@16
|
60
|
Chris@16
|
61 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
62 typename disable_if<
|
Chris@16
|
63 is_convertible<const ChainedPtr&,const Class&>,Type&>::type
|
Chris@16
|
64 #else
|
Chris@16
|
65 Type&
|
Chris@16
|
66 #endif
|
Chris@16
|
67
|
Chris@16
|
68 operator()(const ChainedPtr& x)const
|
Chris@16
|
69 {
|
Chris@16
|
70 return operator()(*x);
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 Type& operator()(const Class& x)const
|
Chris@16
|
74 {
|
Chris@16
|
75 return x.*PtrToMember;
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78 Type& operator()(const reference_wrapper<const Class>& x)const
|
Chris@16
|
79 {
|
Chris@16
|
80 return operator()(x.get());
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 Type& operator()(const reference_wrapper<Class>& x,int=0)const
|
Chris@16
|
84 {
|
Chris@16
|
85 return operator()(x.get());
|
Chris@16
|
86 }
|
Chris@16
|
87 };
|
Chris@16
|
88
|
Chris@16
|
89 template<class Class,typename Type,Type Class::*PtrToMember>
|
Chris@16
|
90 struct non_const_member_base
|
Chris@16
|
91 {
|
Chris@16
|
92 typedef Type result_type;
|
Chris@16
|
93
|
Chris@16
|
94 template<typename ChainedPtr>
|
Chris@16
|
95
|
Chris@16
|
96 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
97 typename disable_if<
|
Chris@16
|
98 is_convertible<const ChainedPtr&,const Class&>,Type&>::type
|
Chris@16
|
99 #else
|
Chris@16
|
100 Type&
|
Chris@16
|
101 #endif
|
Chris@16
|
102
|
Chris@16
|
103 operator()(const ChainedPtr& x)const
|
Chris@16
|
104 {
|
Chris@16
|
105 return operator()(*x);
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 const Type& operator()(const Class& x,int=0)const
|
Chris@16
|
109 {
|
Chris@16
|
110 return x.*PtrToMember;
|
Chris@16
|
111 }
|
Chris@16
|
112
|
Chris@16
|
113 Type& operator()(Class& x)const
|
Chris@16
|
114 {
|
Chris@16
|
115 return x.*PtrToMember;
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 const Type& operator()(const reference_wrapper<const Class>& x,int=0)const
|
Chris@16
|
119 {
|
Chris@16
|
120 return operator()(x.get());
|
Chris@16
|
121 }
|
Chris@16
|
122
|
Chris@16
|
123 Type& operator()(const reference_wrapper<Class>& x)const
|
Chris@16
|
124 {
|
Chris@16
|
125 return operator()(x.get());
|
Chris@16
|
126 }
|
Chris@16
|
127 };
|
Chris@16
|
128
|
Chris@16
|
129 } /* namespace multi_index::detail */
|
Chris@16
|
130
|
Chris@16
|
131 template<class Class,typename Type,Type Class::*PtrToMember>
|
Chris@16
|
132 struct member:
|
Chris@16
|
133 mpl::if_c<
|
Chris@16
|
134 is_const<Type>::value,
|
Chris@16
|
135 detail::const_member_base<Class,Type,PtrToMember>,
|
Chris@16
|
136 detail::non_const_member_base<Class,Type,PtrToMember>
|
Chris@16
|
137 >::type
|
Chris@16
|
138 {
|
Chris@16
|
139 };
|
Chris@16
|
140
|
Chris@16
|
141 namespace detail{
|
Chris@16
|
142
|
Chris@16
|
143 /* MSVC++ 6.0 does not support properly pointers to members as
|
Chris@16
|
144 * non-type template arguments, as reported in
|
Chris@16
|
145 * http://support.microsoft.com/default.aspx?scid=kb;EN-US;249045
|
Chris@16
|
146 * A similar problem (though not identical) is shown by MSVC++ 7.0.
|
Chris@16
|
147 * We provide an alternative to member<> accepting offsets instead
|
Chris@16
|
148 * of pointers to members. This happens to work even for non-POD
|
Chris@16
|
149 * types (although the standard forbids use of offsetof on these),
|
Chris@16
|
150 * so it serves as a workaround in this compiler for all practical
|
Chris@16
|
151 * purposes.
|
Chris@16
|
152 * Surprisingly enough, other compilers, like Intel C++ 7.0/7.1 and
|
Chris@16
|
153 * Visual Age 6.0, have similar bugs. This replacement of member<>
|
Chris@16
|
154 * can be used for them too.
|
Chris@16
|
155 */
|
Chris@16
|
156
|
Chris@16
|
157 template<class Class,typename Type,std::size_t OffsetOfMember>
|
Chris@16
|
158 struct const_member_offset_base
|
Chris@16
|
159 {
|
Chris@16
|
160 typedef Type result_type;
|
Chris@16
|
161
|
Chris@16
|
162 template<typename ChainedPtr>
|
Chris@16
|
163
|
Chris@16
|
164 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
165 typename disable_if<
|
Chris@16
|
166 is_convertible<const ChainedPtr&,const Class&>,Type&>::type
|
Chris@16
|
167 #else
|
Chris@16
|
168 Type&
|
Chris@16
|
169 #endif
|
Chris@16
|
170
|
Chris@16
|
171 operator()(const ChainedPtr& x)const
|
Chris@16
|
172 {
|
Chris@16
|
173 return operator()(*x);
|
Chris@16
|
174 }
|
Chris@16
|
175
|
Chris@16
|
176 Type& operator()(const Class& x)const
|
Chris@16
|
177 {
|
Chris@16
|
178 return *static_cast<const Type*>(
|
Chris@16
|
179 static_cast<const void*>(
|
Chris@16
|
180 static_cast<const char*>(
|
Chris@16
|
181 static_cast<const void *>(&x))+OffsetOfMember));
|
Chris@16
|
182 }
|
Chris@16
|
183
|
Chris@16
|
184 Type& operator()(const reference_wrapper<const Class>& x)const
|
Chris@16
|
185 {
|
Chris@16
|
186 return operator()(x.get());
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 Type& operator()(const reference_wrapper<Class>& x,int=0)const
|
Chris@16
|
190 {
|
Chris@16
|
191 return operator()(x.get());
|
Chris@16
|
192 }
|
Chris@16
|
193 };
|
Chris@16
|
194
|
Chris@16
|
195 template<class Class,typename Type,std::size_t OffsetOfMember>
|
Chris@16
|
196 struct non_const_member_offset_base
|
Chris@16
|
197 {
|
Chris@16
|
198 typedef Type result_type;
|
Chris@16
|
199
|
Chris@16
|
200 template<typename ChainedPtr>
|
Chris@16
|
201
|
Chris@16
|
202 #if !defined(BOOST_NO_SFINAE)
|
Chris@16
|
203 typename disable_if<
|
Chris@16
|
204 is_convertible<const ChainedPtr&,const Class&>,Type&>::type
|
Chris@16
|
205 #else
|
Chris@16
|
206 Type&
|
Chris@16
|
207 #endif
|
Chris@16
|
208
|
Chris@16
|
209 operator()(const ChainedPtr& x)const
|
Chris@16
|
210 {
|
Chris@16
|
211 return operator()(*x);
|
Chris@16
|
212 }
|
Chris@16
|
213
|
Chris@16
|
214 const Type& operator()(const Class& x,int=0)const
|
Chris@16
|
215 {
|
Chris@16
|
216 return *static_cast<const Type*>(
|
Chris@16
|
217 static_cast<const void*>(
|
Chris@16
|
218 static_cast<const char*>(
|
Chris@16
|
219 static_cast<const void *>(&x))+OffsetOfMember));
|
Chris@16
|
220 }
|
Chris@16
|
221
|
Chris@16
|
222 Type& operator()(Class& x)const
|
Chris@16
|
223 {
|
Chris@16
|
224 return *static_cast<Type*>(
|
Chris@16
|
225 static_cast<void*>(
|
Chris@16
|
226 static_cast<char*>(static_cast<void *>(&x))+OffsetOfMember));
|
Chris@16
|
227 }
|
Chris@16
|
228
|
Chris@16
|
229 const Type& operator()(const reference_wrapper<const Class>& x,int=0)const
|
Chris@16
|
230 {
|
Chris@16
|
231 return operator()(x.get());
|
Chris@16
|
232 }
|
Chris@16
|
233
|
Chris@16
|
234 Type& operator()(const reference_wrapper<Class>& x)const
|
Chris@16
|
235 {
|
Chris@16
|
236 return operator()(x.get());
|
Chris@16
|
237 }
|
Chris@16
|
238 };
|
Chris@16
|
239
|
Chris@16
|
240 } /* namespace multi_index::detail */
|
Chris@16
|
241
|
Chris@16
|
242 template<class Class,typename Type,std::size_t OffsetOfMember>
|
Chris@16
|
243 struct member_offset:
|
Chris@16
|
244 mpl::if_c<
|
Chris@16
|
245 is_const<Type>::value,
|
Chris@16
|
246 detail::const_member_offset_base<Class,Type,OffsetOfMember>,
|
Chris@16
|
247 detail::non_const_member_offset_base<Class,Type,OffsetOfMember>
|
Chris@16
|
248 >::type
|
Chris@16
|
249 {
|
Chris@16
|
250 };
|
Chris@16
|
251
|
Chris@16
|
252 /* BOOST_MULTI_INDEX_MEMBER resolves to member in the normal cases,
|
Chris@16
|
253 * and to member_offset as a workaround in those defective compilers for
|
Chris@16
|
254 * which BOOST_NO_POINTER_TO_MEMBER_TEMPLATE_PARAMETERS is defined.
|
Chris@16
|
255 */
|
Chris@16
|
256
|
Chris@16
|
257 #if defined(BOOST_NO_POINTER_TO_MEMBER_TEMPLATE_PARAMETERS)
|
Chris@16
|
258 #define BOOST_MULTI_INDEX_MEMBER(Class,Type,MemberName) \
|
Chris@16
|
259 ::boost::multi_index::member_offset< Class,Type,offsetof(Class,MemberName) >
|
Chris@16
|
260 #else
|
Chris@16
|
261 #define BOOST_MULTI_INDEX_MEMBER(Class,Type,MemberName) \
|
Chris@16
|
262 ::boost::multi_index::member< Class,Type,&Class::MemberName >
|
Chris@16
|
263 #endif
|
Chris@16
|
264
|
Chris@16
|
265 } /* namespace multi_index */
|
Chris@16
|
266
|
Chris@16
|
267 } /* namespace boost */
|
Chris@16
|
268
|
Chris@16
|
269 #endif
|