slist_hook.hpp 10 KB

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