parent_from_member.hpp 4.1 KB

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