list_hook.hpp 10 KB

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