bs_set_hook.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_BS_SET_HOOK_HPP
  13. #define BOOST_INTRUSIVE_BS_SET_HOOK_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <boost/intrusive/intrusive_fwd.hpp>
  16. #include <boost/intrusive/detail/tree_node.hpp>
  17. #include <boost/intrusive/bstree_algorithms.hpp>
  18. #include <boost/intrusive/options.hpp>
  19. #include <boost/intrusive/detail/generic_hook.hpp>
  20. #if defined(BOOST_HAS_PRAGMA_ONCE)
  21. # pragma once
  22. #endif
  23. namespace boost {
  24. namespace intrusive {
  25. //! Helper metafunction to define a \c bs_set_base_hook that yields to the same
  26. //! type when the same options (either explicitly or implicitly) are used.
  27. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  28. template<class ...Options>
  29. #else
  30. template<class O1 = void, class O2 = void, class O3 = void>
  31. #endif
  32. struct make_bs_set_base_hook
  33. {
  34. /// @cond
  35. typedef typename pack_options
  36. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  37. < hook_defaults, O1, O2, O3>
  38. #else
  39. < hook_defaults, Options...>
  40. #endif
  41. ::type packed_options;
  42. typedef generic_hook
  43. < BsTreeAlgorithms
  44. , tree_node_traits<typename packed_options::void_pointer>
  45. , typename packed_options::tag
  46. , packed_options::link_mode
  47. , BsTreeBaseHookId
  48. > implementation_defined;
  49. /// @endcond
  50. typedef implementation_defined type;
  51. };
  52. //! Derive a class from bs_set_base_hook in order to store objects in
  53. //! in a bs_set/bs_multiset. bs_set_base_hook holds the data necessary to maintain
  54. //! the bs_set/bs_multiset and provides an appropriate value_traits class for bs_set/bs_multiset.
  55. //!
  56. //! The hook admits the following options: \c tag<>, \c void_pointer<>,
  57. //! \c link_mode<>.
  58. //!
  59. //! \c tag<> defines a tag to identify the node.
  60. //! The same tag value can be used in different classes, but if a class is
  61. //! derived from more than one \c list_base_hook, then each \c list_base_hook needs its
  62. //! unique tag.
  63. //!
  64. //! \c void_pointer<> is the pointer type that will be used internally in the hook
  65. //! and the container configured to use this hook.
  66. //!
  67. //! \c link_mode<> will specify the linking mode of the hook (\c normal_link,
  68. //! \c auto_unlink or \c safe_link).
  69. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  70. template<class ...Options>
  71. #else
  72. template<class O1, class O2, class O3>
  73. #endif
  74. class bs_set_base_hook
  75. : public make_bs_set_base_hook
  76. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  77. <O1, O2, O3>
  78. #else
  79. <Options...>
  80. #endif
  81. ::type
  82. {
  83. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  84. public:
  85. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  86. //! initializes the node to an unlinked state.
  87. //!
  88. //! <b>Throws</b>: Nothing.
  89. bs_set_base_hook();
  90. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  91. //! initializes the node to an unlinked state. The argument is ignored.
  92. //!
  93. //! <b>Throws</b>: Nothing.
  94. //!
  95. //! <b>Rationale</b>: Providing a copy-constructor
  96. //! makes classes using the hook STL-compliant without forcing the
  97. //! user to do some additional work. \c swap can be used to emulate
  98. //! move-semantics.
  99. bs_set_base_hook(const bs_set_base_hook& );
  100. //! <b>Effects</b>: Empty function. The argument is ignored.
  101. //!
  102. //! <b>Throws</b>: Nothing.
  103. //!
  104. //! <b>Rationale</b>: Providing an assignment operator
  105. //! makes classes using the hook STL-compliant without forcing the
  106. //! user to do some additional work. \c swap can be used to emulate
  107. //! move-semantics.
  108. bs_set_base_hook& operator=(const bs_set_base_hook& );
  109. //! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
  110. //! nothing (ie. no code is generated). If link_mode is \c safe_link and the
  111. //! object is stored in a set an assertion is raised. If link_mode is
  112. //! \c auto_unlink and \c is_linked() is true, the node is unlinked.
  113. //!
  114. //! <b>Throws</b>: Nothing.
  115. ~bs_set_base_hook();
  116. //! <b>Effects</b>: Swapping two nodes swaps the position of the elements
  117. //! related to those nodes in one or two containers. That is, if the node
  118. //! this is part of the element e1, the node x is part of the element e2
  119. //! and both elements are included in the containers s1 and s2, then after
  120. //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
  121. //! at the position of e1. If one element is not in a container, then
  122. //! after the swap-operation the other element is not in a container.
  123. //! Iterators to e1 and e2 related to those nodes are invalidated.
  124. //!
  125. //! <b>Complexity</b>: Constant
  126. //!
  127. //! <b>Throws</b>: Nothing.
  128. void swap_nodes(bs_set_base_hook &other);
  129. //! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
  130. //!
  131. //! <b>Returns</b>: true, if the node belongs to a container, false
  132. //! otherwise. This function can be used to test whether \c set::iterator_to
  133. //! will return a valid iterator.
  134. //!
  135. //! <b>Complexity</b>: Constant
  136. bool is_linked() const;
  137. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  138. //! This function is only allowed if link_mode is \c auto_unlink.
  139. //!
  140. //! <b>Throws</b>: Nothing.
  141. void unlink();
  142. #endif
  143. };
  144. //! Helper metafunction to define a \c bs_set_member_hook that yields to the same
  145. //! type when the same options (either explicitly or implicitly) are used.
  146. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  147. template<class ...Options>
  148. #else
  149. template<class O1 = void, class O2 = void, class O3 = void>
  150. #endif
  151. struct make_bs_set_member_hook
  152. {
  153. /// @cond
  154. typedef typename pack_options
  155. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  156. < hook_defaults, O1, O2, O3>
  157. #else
  158. < hook_defaults, Options...>
  159. #endif
  160. ::type packed_options;
  161. typedef generic_hook
  162. < BsTreeAlgorithms
  163. , tree_node_traits<typename packed_options::void_pointer>
  164. , member_tag
  165. , packed_options::link_mode
  166. , NoBaseHookId
  167. > implementation_defined;
  168. /// @endcond
  169. typedef implementation_defined type;
  170. };
  171. //! Put a public data member bs_set_member_hook in order to store objects of this class in
  172. //! a bs_set/bs_multiset. bs_set_member_hook holds the data necessary for maintaining the
  173. //! bs_set/bs_multiset and provides an appropriate value_traits class for bs_set/bs_multiset.
  174. //!
  175. //! The hook admits the following options: \c void_pointer<>, \c link_mode<>.
  176. //!
  177. //! \c void_pointer<> is the pointer type that will be used internally in the hook
  178. //! and the container configured to use this hook.
  179. //!
  180. //! \c link_mode<> will specify the linking mode of the hook (\c normal_link,
  181. //! \c auto_unlink or \c safe_link).
  182. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  183. template<class ...Options>
  184. #else
  185. template<class O1, class O2, class O3>
  186. #endif
  187. class bs_set_member_hook
  188. : public make_bs_set_member_hook
  189. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  190. <O1, O2, O3>
  191. #else
  192. <Options...>
  193. #endif
  194. ::type
  195. {
  196. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  197. public:
  198. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  199. //! initializes the node to an unlinked state.
  200. //!
  201. //! <b>Throws</b>: Nothing.
  202. bs_set_member_hook();
  203. //! <b>Effects</b>: If link_mode is \c auto_unlink or \c safe_link
  204. //! initializes the node to an unlinked state. The argument is ignored.
  205. //!
  206. //! <b>Throws</b>: Nothing.
  207. //!
  208. //! <b>Rationale</b>: Providing a copy-constructor
  209. //! makes classes using the hook STL-compliant without forcing the
  210. //! user to do some additional work. \c swap can be used to emulate
  211. //! move-semantics.
  212. bs_set_member_hook(const bs_set_member_hook& );
  213. //! <b>Effects</b>: Empty function. The argument is ignored.
  214. //!
  215. //! <b>Throws</b>: Nothing.
  216. //!
  217. //! <b>Rationale</b>: Providing an assignment operator
  218. //! makes classes using the hook STL-compliant without forcing the
  219. //! user to do some additional work. \c swap can be used to emulate
  220. //! move-semantics.
  221. bs_set_member_hook& operator=(const bs_set_member_hook& );
  222. //! <b>Effects</b>: If link_mode is \c normal_link, the destructor does
  223. //! nothing (ie. no code is generated). If link_mode is \c safe_link and the
  224. //! object is stored in a set an assertion is raised. If link_mode is
  225. //! \c auto_unlink and \c is_linked() is true, the node is unlinked.
  226. //!
  227. //! <b>Throws</b>: Nothing.
  228. ~bs_set_member_hook();
  229. //! <b>Effects</b>: Swapping two nodes swaps the position of the elements
  230. //! related to those nodes in one or two containers. That is, if the node
  231. //! this is part of the element e1, the node x is part of the element e2
  232. //! and both elements are included in the containers s1 and s2, then after
  233. //! the swap-operation e1 is in s2 at the position of e2 and e2 is in s1
  234. //! at the position of e1. If one element is not in a container, then
  235. //! after the swap-operation the other element is not in a container.
  236. //! Iterators to e1 and e2 related to those nodes are invalidated.
  237. //!
  238. //! <b>Complexity</b>: Constant
  239. //!
  240. //! <b>Throws</b>: Nothing.
  241. void swap_nodes(bs_set_member_hook &other);
  242. //! <b>Precondition</b>: link_mode must be \c safe_link or \c auto_unlink.
  243. //!
  244. //! <b>Returns</b>: true, if the node belongs to a container, false
  245. //! otherwise. This function can be used to test whether \c set::iterator_to
  246. //! will return a valid iterator.
  247. //!
  248. //! <b>Complexity</b>: Constant
  249. bool is_linked() const;
  250. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  251. //! This function is only allowed if link_mode is \c auto_unlink.
  252. //!
  253. //! <b>Throws</b>: Nothing.
  254. void unlink();
  255. #endif
  256. };
  257. } //namespace intrusive
  258. } //namespace boost
  259. #include <boost/intrusive/detail/config_end.hpp>
  260. #endif //BOOST_INTRUSIVE_BS_SET_HOOK_HPP