source: NonGTP/Boost/boost/multi_index/member.hpp @ 857

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