1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga  2007-2013
4 //
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)
8 //
9 // See http://www.boost.org/libs/intrusive for documentation.
10 //
11 /////////////////////////////////////////////////////////////////////////////
12 #ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP
13 #define BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP
14 
15 #ifndef BOOST_CONFIG_HPP
16 #  include <boost/config.hpp>
17 #endif
18 
19 #if defined(BOOST_HAS_PRAGMA_ONCE)
20 #  pragma once
21 #endif
22 
23 #include <boost/intrusive/detail/config_begin.hpp>
24 #include <boost/intrusive/detail/workaround.hpp>
25 #include <cstddef>
26 
27 #if defined(_MSC_VER)
28    #define BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER
29    #include <boost/static_assert.hpp>
30 #endif
31 
32 namespace boost {
33 namespace intrusive {
34 namespace detail {
35 
36 template<class Parent, class Member>
offset_from_pointer_to_member(const Member Parent::* ptr_to_member)37 BOOST_INTRUSIVE_FORCEINLINE std::ptrdiff_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member)
38 {
39    //The implementation of a pointer to member is compiler dependent.
40    #if defined(BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER)
41 
42    //MSVC compliant compilers use their the first 32 bits as offset (even in 64 bit mode)
43    union caster_union
44    {
45       const Member Parent::* ptr_to_member;
46       int offset;
47    } caster;
48 
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) );
53 
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:
58    //
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];
64    //
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]
69    //
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.
72 
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)));
79    #else
80    //This is the traditional C-front approach: __MWERKS__, __DMC__, __SUNPRO_CC
81    union caster_union
82    {
83       const Member Parent::* ptr_to_member;
84       std::ptrdiff_t offset;
85    } caster;
86    caster.ptr_to_member = ptr_to_member;
87    return caster.offset - 1;
88    #endif
89 }
90 
91 template<class Parent, class Member>
parent_from_member(Member * member,const Member Parent::* ptr_to_member)92 BOOST_INTRUSIVE_FORCEINLINE Parent *parent_from_member(Member *member, const Member Parent::* ptr_to_member)
93 {
94    return static_cast<Parent*>
95       (
96          static_cast<void*>
97          (
98             static_cast<char*>(static_cast<void*>(member)) - offset_from_pointer_to_member(ptr_to_member)
99          )
100       );
101 }
102 
103 template<class Parent, class Member>
parent_from_member(const Member * member,const Member Parent::* ptr_to_member)104 BOOST_INTRUSIVE_FORCEINLINE const Parent *parent_from_member(const Member *member, const Member Parent::* ptr_to_member)
105 {
106    return static_cast<const Parent*>
107       (
108          static_cast<const void*>
109          (
110             static_cast<const char*>(static_cast<const void*>(member)) - offset_from_pointer_to_member(ptr_to_member)
111          )
112       );
113 }
114 
115 }  //namespace detail {
116 }  //namespace intrusive {
117 }  //namespace boost {
118 
119 #include <boost/intrusive/detail/config_end.hpp>
120 
121 #endif   //#ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP
122