bs_set_hook.hpp 10 KB

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