hook_traits.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2014
  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_HOOK_TRAITS_HPP
  13. #define BOOST_INTRUSIVE_DETAIL_HOOK_TRAITS_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/workaround.hpp>
  21. #include <boost/intrusive/pointer_traits.hpp>
  22. #include <boost/intrusive/detail/parent_from_member.hpp>
  23. #include <boost/intrusive/link_mode.hpp>
  24. #include <boost/intrusive/detail/mpl.hpp>
  25. #include <boost/move/detail/to_raw_pointer.hpp>
  26. #include <boost/intrusive/detail/node_holder.hpp>
  27. namespace boost {
  28. namespace intrusive {
  29. template<class T, class NodePtr, class Tag, unsigned int Type>
  30. struct bhtraits_base
  31. {
  32. public:
  33. typedef NodePtr node_ptr;
  34. typedef typename pointer_traits<node_ptr>::element_type node;
  35. typedef node_holder<node, Tag, Type> node_holder_type;
  36. typedef T value_type;
  37. typedef typename pointer_traits<node_ptr>::
  38. template rebind_pointer<const node>::type const_node_ptr;
  39. typedef typename pointer_traits<node_ptr>::
  40. template rebind_pointer<T>::type pointer;
  41. typedef typename pointer_traits<node_ptr>::
  42. template rebind_pointer<const T>::type const_pointer;
  43. typedef typename pointer_traits<node_ptr>::
  44. template rebind_pointer<node_holder_type>::type node_holder_ptr;
  45. typedef typename pointer_traits<node_ptr>::
  46. template rebind_pointer<const node_holder_type>::type const_node_holder_ptr;
  47. typedef T & reference;
  48. typedef const T & const_reference;
  49. typedef node_holder_type & node_holder_reference;
  50. typedef const node_holder_type & const_node_holder_reference;
  51. typedef node& node_reference;
  52. typedef const node & const_node_reference;
  53. inline static pointer to_value_ptr(node_ptr n)
  54. {
  55. return pointer_traits<pointer>::
  56. static_cast_from(pointer_traits<node_holder_ptr>::static_cast_from(n));
  57. }
  58. inline static const_pointer to_value_ptr(const_node_ptr n)
  59. {
  60. return pointer_traits<const_pointer>::
  61. static_cast_from(pointer_traits<const_node_holder_ptr>::static_cast_from(n));
  62. }
  63. inline static node_ptr to_node_ptr(reference value)
  64. {
  65. node_ptr p = pointer_traits<node_ptr>::pointer_to
  66. (static_cast<node_reference>(static_cast<node_holder_reference>(value)));
  67. return p;
  68. }
  69. inline static const_node_ptr to_node_ptr(const_reference value)
  70. {
  71. const_node_ptr p = pointer_traits<const_node_ptr>::pointer_to
  72. (static_cast<const_node_reference>(static_cast<const_node_holder_reference>(value)));
  73. return p;
  74. }
  75. };
  76. template<class T, class NodeTraits, link_mode_type LinkMode, class Tag, unsigned int Type>
  77. struct bhtraits
  78. : public bhtraits_base<T, typename NodeTraits::node_ptr, Tag, Type>
  79. {
  80. static const link_mode_type link_mode = LinkMode;
  81. typedef NodeTraits node_traits;
  82. };
  83. template<class T, class Hook, Hook T::* P>
  84. struct mhtraits
  85. {
  86. public:
  87. typedef Hook hook_type;
  88. typedef typename hook_type::hooktags::node_traits node_traits;
  89. typedef typename node_traits::node node;
  90. typedef T value_type;
  91. typedef typename node_traits::node_ptr node_ptr;
  92. typedef typename node_traits::const_node_ptr const_node_ptr;
  93. typedef typename pointer_traits<node_ptr>::
  94. template rebind_pointer<T>::type pointer;
  95. typedef typename pointer_traits<node_ptr>::
  96. template rebind_pointer<const T>::type const_pointer;
  97. typedef T & reference;
  98. typedef const T & const_reference;
  99. typedef node& node_reference;
  100. typedef const node & const_node_reference;
  101. typedef hook_type& hook_reference;
  102. typedef const hook_type & const_hook_reference;
  103. static const link_mode_type link_mode = Hook::hooktags::link_mode;
  104. inline static node_ptr to_node_ptr(reference value)
  105. {
  106. return pointer_traits<node_ptr>::pointer_to
  107. (static_cast<node_reference>(static_cast<hook_reference>(value.*P)));
  108. }
  109. inline static const_node_ptr to_node_ptr(const_reference value)
  110. {
  111. return pointer_traits<const_node_ptr>::pointer_to
  112. (static_cast<const_node_reference>(static_cast<const_hook_reference>(value.*P)));
  113. }
  114. inline static pointer to_value_ptr(node_ptr n)
  115. {
  116. return pointer_traits<pointer>::pointer_to
  117. (*detail::parent_from_member<T, Hook>
  118. (static_cast<Hook*>(boost::movelib::to_raw_pointer(n)), P));
  119. }
  120. inline static const_pointer to_value_ptr(const_node_ptr n)
  121. {
  122. return pointer_traits<const_pointer>::pointer_to
  123. (*detail::parent_from_member<T, Hook>
  124. (static_cast<const Hook*>(boost::movelib::to_raw_pointer(n)), P));
  125. }
  126. };
  127. template<class Functor>
  128. struct fhtraits
  129. {
  130. public:
  131. typedef typename Functor::hook_type hook_type;
  132. typedef typename Functor::hook_ptr hook_ptr;
  133. typedef typename Functor::const_hook_ptr const_hook_ptr;
  134. typedef typename hook_type::hooktags::node_traits node_traits;
  135. typedef typename node_traits::node node;
  136. typedef typename Functor::value_type value_type;
  137. typedef typename node_traits::node_ptr node_ptr;
  138. typedef typename node_traits::const_node_ptr const_node_ptr;
  139. typedef typename pointer_traits<node_ptr>::
  140. template rebind_pointer<value_type>::type pointer;
  141. typedef typename pointer_traits<node_ptr>::
  142. template rebind_pointer<const value_type>::type const_pointer;
  143. typedef value_type & reference;
  144. typedef const value_type & const_reference;
  145. static const link_mode_type link_mode = hook_type::hooktags::link_mode;
  146. inline static node_ptr to_node_ptr(reference value)
  147. { return static_cast<node*>(boost::movelib::to_raw_pointer(Functor::to_hook_ptr(value))); }
  148. inline static const_node_ptr to_node_ptr(const_reference value)
  149. { return static_cast<const node*>(boost::movelib::to_raw_pointer(Functor::to_hook_ptr(value))); }
  150. inline static pointer to_value_ptr(node_ptr n)
  151. { return Functor::to_value_ptr(to_hook_ptr(n)); }
  152. inline static const_pointer to_value_ptr(const_node_ptr n)
  153. { return Functor::to_value_ptr(to_hook_ptr(n)); }
  154. private:
  155. inline static hook_ptr to_hook_ptr(node_ptr n)
  156. { return hook_ptr(&*static_cast<hook_type*>(&*n)); }
  157. inline static const_hook_ptr to_hook_ptr(const_node_ptr n)
  158. { return const_hook_ptr(&*static_cast<const hook_type*>(&*n)); }
  159. };
  160. } //namespace intrusive
  161. } //namespace boost
  162. #endif //BOOST_INTRUSIVE_DETAIL_HOOK_TRAITS_HPP