hook_traits.hpp 8.2 KB

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