hook_traits.hpp 7.8 KB

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