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 #ifndef BOOST_CONFIG_HPP
16 # include <boost/config.hpp>
19 #if defined(BOOST_HAS_PRAGMA_ONCE)
23 #include <boost/intrusive/detail/config_begin.hpp>
24 #include <boost/intrusive/detail/workaround.hpp>
28 #define BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER
29 #include <boost/static_assert.hpp>
36 template<class Parent, class Member>
37 BOOST_INTRUSIVE_FORCEINLINE std::ptrdiff_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member)
39 //The implementation of a pointer to member is compiler dependent.
40 #if defined(BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER)
42 //MSVC compliant compilers use their the first 32 bits as offset (even in 64 bit mode)
45 const Member Parent::* ptr_to_member;
49 //MSVC ABI can use up to 3 int32 to represent pointer to member data
50 //with virtual base classes, in those cases there is no simple to
51 //obtain the address of the parent. So static assert to avoid runtime errors
52 BOOST_STATIC_ASSERT( sizeof(caster) == sizeof(int) );
54 caster.ptr_to_member = ptr_to_member;
55 return std::ptrdiff_t(caster.offset);
56 //Additional info on MSVC behaviour for the future. For 2/3 int ptr-to-member
57 //types dereference seems to be:
59 // vboffset = [compile_time_offset if 2-int ptr2memb] /
60 // [ptr2memb.i32[2] if 3-int ptr2memb].
61 // vbtable = *(this + vboffset);
62 // adj = vbtable[ptr2memb.i32[1]];
63 // var = adj + (this + vboffset) + ptr2memb.i32[0];
65 //To reverse the operation we need to
66 // - obtain vboffset (in 2-int ptr2memb implementation only)
67 // - Go to Parent's vbtable and obtain adjustment at index ptr2memb.i32[1]
68 // - parent = member - adj - vboffset - ptr2memb.i32[0]
70 //Even accessing to RTTI we might not be able to obtain this information
71 //so anyone who thinks it's possible, please send a patch.
73 //This works with gcc, msvc, ac++, ibmcpp
74 #elif defined(__GNUC__) || defined(__HP_aCC) || defined(BOOST_INTEL) || \
75 defined(__IBMCPP__) || defined(__DECCXX)
76 const Parent * const parent = 0;
77 const char *const member = static_cast<const char*>(static_cast<const void*>(&(parent->*ptr_to_member)));
78 return std::ptrdiff_t(member - static_cast<const char*>(static_cast<const void*>(parent)));
80 //This is the traditional C-front approach: __MWERKS__, __DMC__, __SUNPRO_CC
83 const Member Parent::* ptr_to_member;
84 std::ptrdiff_t offset;
86 caster.ptr_to_member = ptr_to_member;
87 return caster.offset - 1;
91 template<class Parent, class Member>
92 BOOST_INTRUSIVE_FORCEINLINE Parent *parent_from_member(Member *member, const Member Parent::* ptr_to_member)
94 return static_cast<Parent*>
98 static_cast<char*>(static_cast<void*>(member)) - offset_from_pointer_to_member(ptr_to_member)
103 template<class Parent, class Member>
104 BOOST_INTRUSIVE_FORCEINLINE const Parent *parent_from_member(const Member *member, const Member Parent::* ptr_to_member)
106 return static_cast<const Parent*>
108 static_cast<const void*>
110 static_cast<const char*>(static_cast<const void*>(member)) - offset_from_pointer_to_member(ptr_to_member)
115 } //namespace detail {
116 } //namespace intrusive {
117 } //namespace boost {
119 #include <boost/intrusive/detail/config_end.hpp>
121 #endif //#ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP