1/////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2007-2013
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// See http://www.boost.org/libs/intrusive for documentation.
10//
11/////////////////////////////////////////////////////////////////////////////
12
13#ifndef BOOST_INTRUSIVE_GENERIC_HOOK_HPP
14#define BOOST_INTRUSIVE_GENERIC_HOOK_HPP
15
16#ifndef BOOST_CONFIG_HPP
17# include <boost/config.hpp>
18#endif
19
20#if defined(BOOST_HAS_PRAGMA_ONCE)
21# pragma once
22#endif
23
24#include <boost/intrusive/pointer_traits.hpp>
25#include <boost/intrusive/link_mode.hpp>
26#include <boost/intrusive/detail/mpl.hpp>
27#include <boost/intrusive/detail/assert.hpp>
28#include <boost/intrusive/detail/node_holder.hpp>
29#include <boost/intrusive/detail/algo_type.hpp>
30
31namespace boost {
32namespace intrusive {
33
34/// @cond
35
36namespace detail {
37
38template <link_mode_type LinkMode>
39struct link_dispatch
40{};
41
42template<class Hook>
43inline void destructor_impl(Hook &hook, detail::link_dispatch<safe_link>)
44{ //If this assertion raises, you might have destroyed an object
45 //while it was still inserted in a container that is alive.
46 //If so, remove the object from the container before destroying it.
47 (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked());
48}
49
50template<class Hook>
51inline void destructor_impl(Hook &hook, detail::link_dispatch<auto_unlink>)
52{ hook.unlink(); }
53
54template<class Hook>
55inline void destructor_impl(Hook &, detail::link_dispatch<normal_link>)
56{}
57
58} //namespace detail {
59
60enum base_hook_type
61{ NoBaseHookId
62, ListBaseHookId
63, SlistBaseHookId
64, RbTreeBaseHookId
65, HashBaseHookId
66, AvlTreeBaseHookId
67, BsTreeBaseHookId
68, TreapTreeBaseHookId
69, AnyBaseHookId
70};
71
72
73template <class HookTags, unsigned int>
74struct hook_tags_definer{};
75
76template <class HookTags>
77struct hook_tags_definer<HookTags, ListBaseHookId>
78{ typedef HookTags default_list_hook; };
79
80template <class HookTags>
81struct hook_tags_definer<HookTags, SlistBaseHookId>
82{ typedef HookTags default_slist_hook; };
83
84template <class HookTags>
85struct hook_tags_definer<HookTags, RbTreeBaseHookId>
86{ typedef HookTags default_rbtree_hook; };
87
88template <class HookTags>
89struct hook_tags_definer<HookTags, HashBaseHookId>
90{ typedef HookTags default_hashtable_hook; };
91
92template <class HookTags>
93struct hook_tags_definer<HookTags, AvlTreeBaseHookId>
94{ typedef HookTags default_avltree_hook; };
95
96template <class HookTags>
97struct hook_tags_definer<HookTags, BsTreeBaseHookId>
98{ typedef HookTags default_bstree_hook; };
99
100template <class HookTags>
101struct hook_tags_definer<HookTags, AnyBaseHookId>
102{ typedef HookTags default_any_hook; };
103
104template
105 < class NodeTraits
106 , class Tag
107 , link_mode_type LinkMode
108 , base_hook_type BaseHookType
109 >
110struct hooktags_impl
111{
112 static const link_mode_type link_mode = LinkMode;
113 typedef Tag tag;
114 typedef NodeTraits node_traits;
115 static const bool is_base_hook = !detail::is_same<Tag, member_tag>::value;
116 static const bool safemode_or_autounlink = is_safe_autounlink<link_mode>::value;
117 static const unsigned int type = BaseHookType;
118};
119
120/// @endcond
121
122template
123 < boost::intrusive::algo_types Algo
124 , class NodeTraits
125 , class Tag
126 , link_mode_type LinkMode
127 , base_hook_type BaseHookType
128 >
129class generic_hook
130 /// @cond
131 //If the hook is a base hook, derive generic hook from node_holder
132 //so that a unique base class is created to convert from the node
133 //to the type. This mechanism will be used by bhtraits.
134 //
135 //If the hook is a member hook, generic hook will directly derive
136 //from the hook.
137 : public detail::if_c
138 < detail::is_same<Tag, member_tag>::value
139 , typename NodeTraits::node
140 , node_holder<typename NodeTraits::node, Tag, BaseHookType>
141 >::type
142 //If this is the a default-tagged base hook derive from a class that
143 //will define an special internal typedef. Containers will be able to detect this
144 //special typedef and obtain generic_hook's internal types in order to deduce
145 //value_traits for this hook.
146 , public hook_tags_definer
147 < generic_hook<Algo, NodeTraits, Tag, LinkMode, BaseHookType>
148 , detail::is_same<Tag, dft_tag>::value ? BaseHookType : NoBaseHookId>
149 /// @endcond
150{
151 /// @cond
152 typedef typename get_algo<Algo, NodeTraits>::type node_algorithms;
153 typedef typename node_algorithms::node node;
154 typedef typename node_algorithms::node_ptr node_ptr;
155 typedef typename node_algorithms::const_node_ptr const_node_ptr;
156
157 public:
158
159 typedef hooktags_impl
160 < NodeTraits
161 , Tag, LinkMode, BaseHookType> hooktags;
162
163 inline node_ptr this_ptr() BOOST_NOEXCEPT
164 { return pointer_traits<node_ptr>::pointer_to(static_cast<node&>(*this)); }
165
166 inline const_node_ptr this_ptr() const BOOST_NOEXCEPT
167 { return pointer_traits<const_node_ptr>::pointer_to(static_cast<const node&>(*this)); }
168
169 public:
170 /// @endcond
171
172 inline generic_hook() BOOST_NOEXCEPT
173 {
174 if(hooktags::safemode_or_autounlink){
175 node_algorithms::init(this->this_ptr());
176 }
177 }
178
179 inline generic_hook(const generic_hook& ) BOOST_NOEXCEPT
180 {
181 if(hooktags::safemode_or_autounlink){
182 node_algorithms::init(this->this_ptr());
183 }
184 }
185
186 inline generic_hook& operator=(const generic_hook& ) BOOST_NOEXCEPT
187 { return *this; }
188
189 inline ~generic_hook()
190 {
191 destructor_impl
192 (*this, detail::link_dispatch<hooktags::link_mode>());
193 }
194
195 inline void swap_nodes(generic_hook &other) BOOST_NOEXCEPT
196 {
197 node_algorithms::swap_nodes
198 (this->this_ptr(), other.this_ptr());
199 }
200
201 inline bool is_linked() const BOOST_NOEXCEPT
202 {
203 //is_linked() can be only used in safe-mode or auto-unlink
204 BOOST_INTRUSIVE_STATIC_ASSERT(( hooktags::safemode_or_autounlink ));
205 return !node_algorithms::unique(this->this_ptr());
206 }
207
208 inline void unlink() BOOST_NOEXCEPT
209 {
210 BOOST_INTRUSIVE_STATIC_ASSERT(( (int)hooktags::link_mode == (int)auto_unlink ));
211 node_ptr n(this->this_ptr());
212 if(!node_algorithms::inited(n)){
213 node_algorithms::unlink(n);
214 node_algorithms::init(n);
215 }
216 }
217};
218
219} //namespace intrusive
220} //namespace boost
221
222#endif //BOOST_INTRUSIVE_GENERIC_HOOK_HPP
223

source code of boost/libs/intrusive/include/boost/intrusive/detail/generic_hook.hpp