parent_from_member.hpp 4.1 KB

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