1 /* Copyright 2003-2015 Joaquin M Lopez Munoz.
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_MEMBER_HPP
10 #define BOOST_MULTI_INDEX_MEMBER_HPP
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/is_const.hpp>
19 #include <boost/utility/enable_if.hpp>
22 #if !defined(BOOST_NO_SFINAE)
23 #include <boost/type_traits/is_convertible.hpp>
28 template<class T> class reference_wrapper; /* fwd decl. */
30 namespace multi_index{
34 /* member is a read/write key extractor for accessing a given
36 * Additionally, member is overloaded to support referece_wrappers
37 * of T and "chained pointers" to T's. By chained pointer to T we mean
38 * a type P such that, given a p of Type P
39 * *...n...*x is convertible to T&, for some n>=1.
40 * Examples of chained pointers are raw and smart pointers, iterators and
41 * arbitrary combinations of these (vg. T** or unique_ptr<T*>.)
44 template<class Class,typename Type,Type Class::*PtrToMember>
45 struct const_member_base
47 typedef Type result_type;
49 template<typename ChainedPtr>
51 #if !defined(BOOST_NO_SFINAE)
53 is_convertible<const ChainedPtr&,const Class&>,Type&>::type
58 operator()(const ChainedPtr& x)const
60 return operator()(*x);
63 Type& operator()(const Class& x)const
65 return x.*PtrToMember;
68 Type& operator()(const reference_wrapper<const Class>& x)const
70 return operator()(x.get());
73 Type& operator()(const reference_wrapper<Class>& x)const
75 return operator()(x.get());
79 template<class Class,typename Type,Type Class::*PtrToMember>
80 struct non_const_member_base
82 typedef Type result_type;
84 template<typename ChainedPtr>
86 #if !defined(BOOST_NO_SFINAE)
88 is_convertible<const ChainedPtr&,const Class&>,Type&>::type
93 operator()(const ChainedPtr& x)const
95 return operator()(*x);
98 const Type& operator()(const Class& x)const
100 return x.*PtrToMember;
103 Type& operator()(Class& x)const
105 return x.*PtrToMember;
108 const Type& operator()(const reference_wrapper<const Class>& x)const
110 return operator()(x.get());
113 Type& operator()(const reference_wrapper<Class>& x)const
115 return operator()(x.get());
119 } /* namespace multi_index::detail */
121 template<class Class,typename Type,Type Class::*PtrToMember>
124 is_const<Type>::value,
125 detail::const_member_base<Class,Type,PtrToMember>,
126 detail::non_const_member_base<Class,Type,PtrToMember>
133 /* MSVC++ 6.0 does not support properly pointers to members as
134 * non-type template arguments, as reported in
135 * http://support.microsoft.com/default.aspx?scid=kb;EN-US;249045
136 * A similar problem (though not identical) is shown by MSVC++ 7.0.
137 * We provide an alternative to member<> accepting offsets instead
138 * of pointers to members. This happens to work even for non-POD
139 * types (although the standard forbids use of offsetof on these),
140 * so it serves as a workaround in this compiler for all practical
142 * Surprisingly enough, other compilers, like Intel C++ 7.0/7.1 and
143 * Visual Age 6.0, have similar bugs. This replacement of member<>
144 * can be used for them too.
146 * Support for such old compilers is dropped and
147 * [non_]const_member_offset_base is deprecated.
150 template<class Class,typename Type,std::size_t OffsetOfMember>
151 struct const_member_offset_base
153 typedef Type result_type;
155 template<typename ChainedPtr>
157 #if !defined(BOOST_NO_SFINAE)
159 is_convertible<const ChainedPtr&,const Class&>,Type&>::type
164 operator()(const ChainedPtr& x)const
166 return operator()(*x);
169 Type& operator()(const Class& x)const
171 return *static_cast<const Type*>(
172 static_cast<const void*>(
173 static_cast<const char*>(
174 static_cast<const void *>(&x))+OffsetOfMember));
177 Type& operator()(const reference_wrapper<const Class>& x)const
179 return operator()(x.get());
182 Type& operator()(const reference_wrapper<Class>& x)const
184 return operator()(x.get());
188 template<class Class,typename Type,std::size_t OffsetOfMember>
189 struct non_const_member_offset_base
191 typedef Type result_type;
193 template<typename ChainedPtr>
195 #if !defined(BOOST_NO_SFINAE)
197 is_convertible<const ChainedPtr&,const Class&>,Type&>::type
202 operator()(const ChainedPtr& x)const
204 return operator()(*x);
207 const Type& operator()(const Class& x)const
209 return *static_cast<const Type*>(
210 static_cast<const void*>(
211 static_cast<const char*>(
212 static_cast<const void *>(&x))+OffsetOfMember));
215 Type& operator()(Class& x)const
217 return *static_cast<Type*>(
219 static_cast<char*>(static_cast<void *>(&x))+OffsetOfMember));
222 const Type& operator()(const reference_wrapper<const Class>& x)const
224 return operator()(x.get());
227 Type& operator()(const reference_wrapper<Class>& x)const
229 return operator()(x.get());
233 } /* namespace multi_index::detail */
235 template<class Class,typename Type,std::size_t OffsetOfMember>
236 struct member_offset:
238 is_const<Type>::value,
239 detail::const_member_offset_base<Class,Type,OffsetOfMember>,
240 detail::non_const_member_offset_base<Class,Type,OffsetOfMember>
245 /* BOOST_MULTI_INDEX_MEMBER resolves to member in the normal cases,
246 * and to member_offset as a workaround in those defective compilers for
247 * which BOOST_NO_POINTER_TO_MEMBER_TEMPLATE_PARAMETERS is defined.
250 #if defined(BOOST_NO_POINTER_TO_MEMBER_TEMPLATE_PARAMETERS)
251 #define BOOST_MULTI_INDEX_MEMBER(Class,Type,MemberName) \
252 ::boost::multi_index::member_offset< Class,Type,offsetof(Class,MemberName) >
254 #define BOOST_MULTI_INDEX_MEMBER(Class,Type,MemberName) \
255 ::boost::multi_index::member< Class,Type,&Class::MemberName >
258 } /* namespace multi_index */
260 } /* namespace boost */