generic_hook.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_GENERIC_HOOK_HPP
  13. #define BOOST_INTRUSIVE_GENERIC_HOOK_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <boost/intrusive/intrusive_fwd.hpp>
  16. #include <boost/intrusive/pointer_traits.hpp>
  17. #include <boost/intrusive/link_mode.hpp>
  18. #include <boost/intrusive/detail/utilities.hpp>
  19. #include <boost/intrusive/detail/mpl.hpp>
  20. #include <boost/intrusive/pointer_traits.hpp>
  21. #include <boost/static_assert.hpp>
  22. namespace boost {
  23. namespace intrusive {
  24. /// @cond
  25. enum base_hook_type
  26. { NoBaseHookId
  27. , ListBaseHookId
  28. , SlistBaseHookId
  29. , RbTreeBaseHookId
  30. , HashBaseHookId
  31. , SplayTreeBaseHookId
  32. , AvlTreeBaseHookId
  33. , BsTreeBaseHookId
  34. , AnyBaseHookId
  35. };
  36. template <class HookTags, unsigned int>
  37. struct hook_tags_definer{};
  38. template <class HookTags>
  39. struct hook_tags_definer<HookTags, ListBaseHookId>
  40. { typedef HookTags default_list_hook; };
  41. template <class HookTags>
  42. struct hook_tags_definer<HookTags, SlistBaseHookId>
  43. { typedef HookTags default_slist_hook; };
  44. template <class HookTags>
  45. struct hook_tags_definer<HookTags, RbTreeBaseHookId>
  46. { typedef HookTags default_rbtree_hook; };
  47. template <class HookTags>
  48. struct hook_tags_definer<HookTags, HashBaseHookId>
  49. { typedef HookTags default_hashtable_hook; };
  50. template <class HookTags>
  51. struct hook_tags_definer<HookTags, SplayTreeBaseHookId>
  52. { typedef HookTags default_splaytree_hook; };
  53. template <class HookTags>
  54. struct hook_tags_definer<HookTags, AvlTreeBaseHookId>
  55. { typedef HookTags default_avltree_hook; };
  56. template <class HookTags>
  57. struct hook_tags_definer<HookTags, BsTreeBaseHookId>
  58. { typedef HookTags default_bstree_hook; };
  59. template <class HookTags>
  60. struct hook_tags_definer<HookTags, AnyBaseHookId>
  61. { typedef HookTags default_any_hook; };
  62. template
  63. < class NodeTraits
  64. , class Tag
  65. , link_mode_type LinkMode
  66. , base_hook_type BaseHookType
  67. >
  68. struct hooktags_impl
  69. {
  70. static const link_mode_type link_mode = LinkMode;
  71. typedef Tag tag;
  72. typedef NodeTraits node_traits;
  73. static const bool is_base_hook = !detail::is_same<Tag, member_tag>::value;
  74. static const bool safemode_or_autounlink = is_safe_autounlink<link_mode>::value;
  75. static const unsigned int type = BaseHookType;
  76. };
  77. /// @endcond
  78. template
  79. < class GetNodeAlgorithms
  80. , class Tag
  81. , link_mode_type LinkMode
  82. , base_hook_type BaseHookType
  83. >
  84. class generic_hook
  85. /// @cond
  86. //If the hook is a base hook, derive generic hook from node_holder
  87. //so that a unique base class is created to convert from the node
  88. //to the type. This mechanism will be used by bhtraits.
  89. //
  90. //If the hook is a member hook, generic hook will directly derive
  91. //from the hook.
  92. : public detail::if_c
  93. < detail::is_same<Tag, member_tag>::value
  94. , typename GetNodeAlgorithms::type::node
  95. , node_holder<typename GetNodeAlgorithms::type::node, Tag, BaseHookType>
  96. >::type
  97. //If this is the a default-tagged base hook derive from a class that
  98. //will define an special internal typedef. Containers will be able to detect this
  99. //special typedef and obtain generic_hook's internal types in order to deduce
  100. //value_traits for this hook.
  101. , public hook_tags_definer
  102. < generic_hook<GetNodeAlgorithms, Tag, LinkMode, BaseHookType>
  103. , detail::is_same<Tag, default_tag>::value*BaseHookType>
  104. /// @endcond
  105. {
  106. /// @cond
  107. typedef typename GetNodeAlgorithms::type node_algorithms;
  108. typedef typename node_algorithms::node node;
  109. typedef typename node_algorithms::node_ptr node_ptr;
  110. typedef typename node_algorithms::const_node_ptr const_node_ptr;
  111. public:
  112. typedef hooktags_impl
  113. < typename GetNodeAlgorithms::type::node_traits
  114. , Tag, LinkMode, BaseHookType> hooktags;
  115. node_ptr this_ptr()
  116. { return pointer_traits<node_ptr>::pointer_to(static_cast<node&>(*this)); }
  117. const_node_ptr this_ptr() const
  118. { return pointer_traits<const_node_ptr>::pointer_to(static_cast<const node&>(*this)); }
  119. public:
  120. /// @endcond
  121. generic_hook()
  122. {
  123. if(hooktags::safemode_or_autounlink){
  124. node_algorithms::init(this->this_ptr());
  125. }
  126. }
  127. generic_hook(const generic_hook& )
  128. {
  129. if(hooktags::safemode_or_autounlink){
  130. node_algorithms::init(this->this_ptr());
  131. }
  132. }
  133. generic_hook& operator=(const generic_hook& )
  134. { return *this; }
  135. ~generic_hook()
  136. {
  137. destructor_impl
  138. (*this, detail::link_dispatch<hooktags::link_mode>());
  139. }
  140. void swap_nodes(generic_hook &other)
  141. {
  142. node_algorithms::swap_nodes
  143. (this->this_ptr(), other.this_ptr());
  144. }
  145. bool is_linked() const
  146. {
  147. //is_linked() can be only used in safe-mode or auto-unlink
  148. BOOST_STATIC_ASSERT(( hooktags::safemode_or_autounlink ));
  149. return !node_algorithms::unique(this->this_ptr());
  150. }
  151. void unlink()
  152. {
  153. BOOST_STATIC_ASSERT(( (int)hooktags::link_mode == (int)auto_unlink ));
  154. node_algorithms::unlink(this->this_ptr());
  155. node_algorithms::init(this->this_ptr());
  156. }
  157. };
  158. } //namespace intrusive
  159. } //namespace boost
  160. #include <boost/intrusive/detail/config_end.hpp>
  161. #endif //BOOST_INTRUSIVE_GENERIC_HOOK_HPP