slist_hook.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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();
  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& );
  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& );
  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);
  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>Complexity</b>: Constant
  139. bool is_linked() const;
  140. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  141. //! This function is only allowed if link_mode is \c auto_unlink.
  142. //!
  143. //! <b>Throws</b>: Nothing.
  144. void unlink();
  145. #endif
  146. };
  147. //! Helper metafunction to define a \c slist_member_hook that yields to the same
  148. //! type when the same options (either explicitly or implicitly) are used.
  149. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  150. template<class ...Options>
  151. #else
  152. template<class O1 = void, class O2 = void, class O3 = void>
  153. #endif
  154. struct make_slist_member_hook
  155. {
  156. /// @cond
  157. typedef typename pack_options
  158. < hook_defaults,
  159. #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
  160. O1, O2, O3
  161. #else
  162. Options...
  163. #endif
  164. >::type packed_options;
  165. typedef generic_hook
  166. < CircularSListAlgorithms
  167. , slist_node_traits<typename packed_options::void_pointer>
  168. , member_tag
  169. , packed_options::link_mode
  170. , NoBaseHookId
  171. > implementation_defined;
  172. /// @endcond
  173. typedef implementation_defined type;
  174. };
  175. //! Put a public data member slist_member_hook in order to store objects of this class in
  176. //! an list. slist_member_hook holds the data necessary for maintaining the list and
  177. //! provides an appropriate value_traits class for 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 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 slist_member_hook
  193. : public make_slist_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. slist_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. slist_member_hook(const slist_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. slist_member_hook& operator=(const slist_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 slist 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. ~slist_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(slist_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 slist::iterator_to
  251. //! will return a valid iterator.
  252. //!
  253. //! <b>Note</b>: If this member is called when the value is inserted in a
  254. //! slist with the option linear<true>, this function will return "false"
  255. //! for the last element, as it is not linked to anything (the next element is null),
  256. //! so use with care.
  257. //!
  258. //! <b>Complexity</b>: Constant
  259. bool is_linked() const;
  260. //! <b>Effects</b>: Removes the node if it's inserted in a container.
  261. //! This function is only allowed if link_mode is \c auto_unlink.
  262. //!
  263. //! <b>Throws</b>: Nothing.
  264. void unlink();
  265. #endif
  266. };
  267. } //namespace intrusive
  268. } //namespace boost
  269. #include <boost/intrusive/detail/config_end.hpp>
  270. #endif //BOOST_INTRUSIVE_SLIST_HOOK_HPP