parent_from_member.hpp 4.0 KB

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