1
/////////////////////////////////////////////////////////////////////////////
3
// (C) Copyright Ion Gaztanaga 2007-2013
5
// Distributed under the Boost Software License, Version 1.0.
6
// (See accompanying file LICENSE_1_0.txt or copy at
7
// http://www.boost.org/LICENSE_1_0.txt)
9
// See http://www.boost.org/libs/intrusive for documentation.
11
/////////////////////////////////////////////////////////////////////////////
12
#ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP
13
#define BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP
15
#include <boost/intrusive/detail/config_begin.hpp>
18
#if defined(BOOST_MSVC) || ((defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) && defined(BOOST_INTEL))
19
#define BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER
20
#include <boost/cstdint.hpp>
21
#include <boost/static_assert.hpp>
28
template<class Parent, class Member>
29
inline std::ptrdiff_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member)
31
//The implementation of a pointer to member is compiler dependent.
32
#if defined(BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER)
34
//MSVC compliant compilers use their the first 32 bits as offset (even in 64 bit mode)
37
const Member Parent::* ptr_to_member;
38
boost::int32_t offset;
41
//MSVC ABI can use up to 3 int32 to represent pointer to member data
42
//with virtual base classes, in those cases there is no simple to
43
//obtain the address of the parent. So static assert to avoid runtime errors
44
BOOST_STATIC_ASSERT( sizeof(caster) == sizeof(boost::int32_t) );
46
caster.ptr_to_member = ptr_to_member;
47
return std::ptrdiff_t(caster.offset);
48
//Additional info on MSVC behaviour for the future. For 2/3 int ptr-to-member
49
//types dereference seems to be:
51
// vboffset = [compile_time_offset if 2-int ptr2memb] /
52
// [ptr2memb.i32[2] if 3-int ptr2memb].
53
// vbtable = *(this + vboffset);
54
// adj = vbtable[ptr2memb.i32[1]];
55
// var = adj + (this + vboffset) + ptr2memb.i32[0];
57
//To reverse the operation we need to
58
// - obtain vboffset (in 2-int ptr2memb implementation only)
59
// - Go to Parent's vbtable and obtain adjustment at index ptr2memb.i32[1]
60
// - parent = member - adj - vboffset - ptr2memb.i32[0]
62
//Even accessing to RTTI we might not be able to obtain this information
63
//so anyone who thinks it's possible, please send a patch.
65
//This works with gcc, msvc, ac++, ibmcpp
66
#elif defined(__GNUC__) || defined(__HP_aCC) || defined(BOOST_INTEL) || \
67
defined(__IBMCPP__) || defined(__DECCXX)
68
const Parent * const parent = 0;
69
const char *const member = static_cast<const char*>(static_cast<const void*>(&(parent->*ptr_to_member)));
70
return std::ptrdiff_t(member - static_cast<const char*>(static_cast<const void*>(parent)));
72
//This is the traditional C-front approach: __MWERKS__, __DMC__, __SUNPRO_CC
75
const Member Parent::* ptr_to_member;
76
std::ptrdiff_t offset;
78
caster.ptr_to_member = ptr_to_member;
79
return caster.offset - 1;
83
template<class Parent, class Member>
84
inline Parent *parent_from_member(Member *member, const Member Parent::* ptr_to_member)
86
return static_cast<Parent*>
90
static_cast<char*>(static_cast<void*>(member)) - offset_from_pointer_to_member(ptr_to_member)
95
template<class Parent, class Member>
96
inline const Parent *parent_from_member(const Member *member, const Member Parent::* ptr_to_member)
98
return static_cast<const Parent*>
100
static_cast<const void*>
102
static_cast<const char*>(static_cast<const void*>(member)) - offset_from_pointer_to_member(ptr_to_member)
107
} //namespace detail {
108
} //namespace intrusive {
109
} //namespace boost {
111
#ifdef BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER
112
#undef BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER
115
#include <boost/intrusive/detail/config_end.hpp>
117
#endif //#ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP