list_hook.hpp 9.9 KB

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