1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2009
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)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_INTRUSIVE_GENERIC_HOOK_HPP
14 #define BOOST_INTRUSIVE_GENERIC_HOOK_HPP
16 #include <boost/intrusive/detail/config_begin.hpp>
17 #include <boost/intrusive/intrusive_fwd.hpp>
18 #include <boost/intrusive/detail/pointer_to_other.hpp>
19 #include <boost/intrusive/link_mode.hpp>
20 #include <boost/intrusive/detail/utilities.hpp>
21 #include <boost/intrusive/detail/mpl.hpp>
22 #include <boost/static_assert.hpp>
42 struct no_default_definer{};
44 template <class Hook, unsigned int>
45 struct default_definer;
48 struct default_definer<Hook, ListBaseHook>
49 { typedef Hook default_list_hook; };
52 struct default_definer<Hook, SlistBaseHook>
53 { typedef Hook default_slist_hook; };
56 struct default_definer<Hook, SetBaseHook>
57 { typedef Hook default_set_hook; };
60 struct default_definer<Hook, UsetBaseHook>
61 { typedef Hook default_uset_hook; };
64 struct default_definer<Hook, SplaySetBaseHook>
65 { typedef Hook default_splay_set_hook; };
68 struct default_definer<Hook, AvlSetBaseHook>
69 { typedef Hook default_avl_set_hook; };
72 struct default_definer<Hook, BsSetBaseHook>
73 { typedef Hook default_bs_set_hook; };
76 struct default_definer<Hook, AnyBaseHook>
77 { typedef Hook default_any_hook; };
79 template <class Hook, unsigned int BaseHookType>
80 struct make_default_definer
82 typedef typename detail::if_c
84 , default_definer<Hook, BaseHookType>
85 , no_default_definer>::type type;
89 < class GetNodeAlgorithms
91 , link_mode_type LinkMode
94 struct make_node_holder
96 typedef typename detail::if_c
97 <!detail::is_same<Tag, member_tag>::value
99 < typename GetNodeAlgorithms::type::node
103 , typename GetNodeAlgorithms::type::node
110 < class GetNodeAlgorithms
112 , link_mode_type LinkMode
118 //If the hook is a base hook, derive generic hook from detail::node_holder
119 //so that a unique base class is created to convert from the node
120 //to the type. This mechanism will be used by base_hook_traits.
122 //If the hook is a member hook, generic hook will directly derive
124 : public make_default_definer
125 < generic_hook<GetNodeAlgorithms, Tag, LinkMode, HookType>
126 , detail::is_same<Tag, default_tag>::value*HookType
128 , public make_node_holder<GetNodeAlgorithms, Tag, LinkMode, HookType>::type
132 typedef typename GetNodeAlgorithms::type node_algorithms;
133 typedef typename node_algorithms::node node;
134 typedef typename node_algorithms::node_ptr node_ptr;
135 typedef typename node_algorithms::const_node_ptr const_node_ptr;
138 struct boost_intrusive_tags
140 static const int hook_type = HookType;
141 static const link_mode_type link_mode = LinkMode;
143 typedef typename GetNodeAlgorithms::type::node_traits node_traits;
144 static const bool is_base_hook = !detail::is_same<Tag, member_tag>::value;
145 static const bool safemode_or_autounlink =
146 (int)link_mode == (int)auto_unlink || (int)link_mode == (int)safe_link;
154 if(boost_intrusive_tags::safemode_or_autounlink){
155 node_algorithms::init(static_cast<node*>(this));
159 generic_hook(const generic_hook& )
161 if(boost_intrusive_tags::safemode_or_autounlink){
162 node_algorithms::init(static_cast<node*>(this));
166 generic_hook& operator=(const generic_hook& )
172 (*this, detail::link_dispatch<boost_intrusive_tags::link_mode>());
175 void swap_nodes(generic_hook &other)
177 node_algorithms::swap_nodes
178 ( static_cast<node*>(this), static_cast<node*>(&other));
181 bool is_linked() const
183 //is_linked() can be only used in safe-mode or auto-unlink
184 BOOST_STATIC_ASSERT(( boost_intrusive_tags::safemode_or_autounlink ));
185 return !node_algorithms::unique
186 (static_cast<const node*>(this));
191 BOOST_STATIC_ASSERT(( (int)boost_intrusive_tags::link_mode == (int)auto_unlink ));
192 node_algorithms::unlink(static_cast<node*>(this));
193 node_algorithms::init(static_cast<node*>(this));
198 } //namespace intrusive
201 #include <boost/intrusive/detail/config_end.hpp>
203 #endif //BOOST_INTRUSIVE_GENERIC_HOOK_HPP