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