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