slist_hook.hpp 11 KB

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