File indexing completed on 2025-07-09 08:13:48
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_INTRUSIVE_GENERIC_HOOK_HPP
0014 #define BOOST_INTRUSIVE_GENERIC_HOOK_HPP
0015
0016 #ifndef BOOST_CONFIG_HPP
0017 # include <boost/config.hpp>
0018 #endif
0019
0020 #if defined(BOOST_HAS_PRAGMA_ONCE)
0021 # pragma once
0022 #endif
0023
0024 #include <boost/intrusive/pointer_traits.hpp>
0025 #include <boost/intrusive/link_mode.hpp>
0026 #include <boost/intrusive/detail/mpl.hpp>
0027 #include <boost/intrusive/detail/assert.hpp>
0028 #include <boost/intrusive/detail/node_holder.hpp>
0029 #include <boost/intrusive/detail/algo_type.hpp>
0030
0031 namespace boost {
0032 namespace intrusive {
0033
0034
0035
0036 namespace detail {
0037
0038 template <link_mode_type LinkMode>
0039 struct link_dispatch
0040 {};
0041
0042 template<class Hook>
0043 inline void destructor_impl(Hook &hook, detail::link_dispatch<safe_link>)
0044 {
0045
0046
0047 (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked());
0048 }
0049
0050 template<class Hook>
0051 inline void destructor_impl(Hook &hook, detail::link_dispatch<auto_unlink>)
0052 { hook.unlink(); }
0053
0054 template<class Hook>
0055 inline void destructor_impl(Hook &, detail::link_dispatch<normal_link>)
0056 {}
0057
0058 }
0059
0060 enum base_hook_type
0061 { NoBaseHookId
0062 , ListBaseHookId
0063 , SlistBaseHookId
0064 , RbTreeBaseHookId
0065 , HashBaseHookId
0066 , AvlTreeBaseHookId
0067 , BsTreeBaseHookId
0068 , TreapTreeBaseHookId
0069 , AnyBaseHookId
0070 };
0071
0072
0073 template <class HookTags, unsigned int>
0074 struct hook_tags_definer{};
0075
0076 template <class HookTags>
0077 struct hook_tags_definer<HookTags, ListBaseHookId>
0078 { typedef HookTags default_list_hook; };
0079
0080 template <class HookTags>
0081 struct hook_tags_definer<HookTags, SlistBaseHookId>
0082 { typedef HookTags default_slist_hook; };
0083
0084 template <class HookTags>
0085 struct hook_tags_definer<HookTags, RbTreeBaseHookId>
0086 { typedef HookTags default_rbtree_hook; };
0087
0088 template <class HookTags>
0089 struct hook_tags_definer<HookTags, HashBaseHookId>
0090 { typedef HookTags default_hashtable_hook; };
0091
0092 template <class HookTags>
0093 struct hook_tags_definer<HookTags, AvlTreeBaseHookId>
0094 { typedef HookTags default_avltree_hook; };
0095
0096 template <class HookTags>
0097 struct hook_tags_definer<HookTags, BsTreeBaseHookId>
0098 { typedef HookTags default_bstree_hook; };
0099
0100 template <class HookTags>
0101 struct hook_tags_definer<HookTags, AnyBaseHookId>
0102 { typedef HookTags default_any_hook; };
0103
0104 template
0105 < class NodeTraits
0106 , class Tag
0107 , link_mode_type LinkMode
0108 , base_hook_type BaseHookType
0109 >
0110 struct hooktags_impl
0111 {
0112 static const link_mode_type link_mode = LinkMode;
0113 typedef Tag tag;
0114 typedef NodeTraits node_traits;
0115 static const bool is_base_hook = !detail::is_same<Tag, member_tag>::value;
0116 static const bool safemode_or_autounlink = is_safe_autounlink<link_mode>::value;
0117 static const unsigned int type = BaseHookType;
0118 };
0119
0120
0121
0122 template
0123 < boost::intrusive::algo_types Algo
0124 , class NodeTraits
0125 , class Tag
0126 , link_mode_type LinkMode
0127 , base_hook_type BaseHookType
0128 >
0129 class generic_hook
0130
0131
0132
0133
0134
0135
0136
0137 : public detail::if_c
0138 < detail::is_same<Tag, member_tag>::value
0139 , typename NodeTraits::node
0140 , node_holder<typename NodeTraits::node, Tag, BaseHookType>
0141 >::type
0142
0143
0144
0145
0146 , public hook_tags_definer
0147 < generic_hook<Algo, NodeTraits, Tag, LinkMode, BaseHookType>
0148 , detail::is_same<Tag, dft_tag>::value ? BaseHookType : NoBaseHookId>
0149
0150 {
0151
0152 typedef typename get_algo<Algo, NodeTraits>::type node_algorithms;
0153 typedef typename node_algorithms::node node;
0154 typedef typename node_algorithms::node_ptr node_ptr;
0155 typedef typename node_algorithms::const_node_ptr const_node_ptr;
0156
0157 public:
0158
0159 typedef hooktags_impl
0160 < NodeTraits
0161 , Tag, LinkMode, BaseHookType> hooktags;
0162
0163 inline node_ptr this_ptr() BOOST_NOEXCEPT
0164 { return pointer_traits<node_ptr>::pointer_to(static_cast<node&>(*this)); }
0165
0166 inline const_node_ptr this_ptr() const BOOST_NOEXCEPT
0167 { return pointer_traits<const_node_ptr>::pointer_to(static_cast<const node&>(*this)); }
0168
0169 public:
0170
0171
0172 inline generic_hook() BOOST_NOEXCEPT
0173 {
0174 if(hooktags::safemode_or_autounlink){
0175 node_algorithms::init(this->this_ptr());
0176 }
0177 }
0178
0179 inline generic_hook(const generic_hook& ) BOOST_NOEXCEPT
0180 {
0181 if(hooktags::safemode_or_autounlink){
0182 node_algorithms::init(this->this_ptr());
0183 }
0184 }
0185
0186 inline generic_hook& operator=(const generic_hook& ) BOOST_NOEXCEPT
0187 { return *this; }
0188
0189 inline ~generic_hook()
0190 {
0191 destructor_impl
0192 (*this, detail::link_dispatch<hooktags::link_mode>());
0193 }
0194
0195 inline void swap_nodes(generic_hook &other) BOOST_NOEXCEPT
0196 {
0197 node_algorithms::swap_nodes
0198 (this->this_ptr(), other.this_ptr());
0199 }
0200
0201 inline bool is_linked() const BOOST_NOEXCEPT
0202 {
0203
0204 BOOST_INTRUSIVE_STATIC_ASSERT(( hooktags::safemode_or_autounlink ));
0205 return !node_algorithms::unique(this->this_ptr());
0206 }
0207
0208 inline void unlink() BOOST_NOEXCEPT
0209 {
0210 BOOST_INTRUSIVE_STATIC_ASSERT(( (int)hooktags::link_mode == (int)auto_unlink ));
0211 node_ptr n(this->this_ptr());
0212 if(!node_algorithms::inited(n)){
0213 node_algorithms::unlink(n);
0214 node_algorithms::init(n);
0215 }
0216 }
0217 };
0218
0219 }
0220 }
0221
0222 #endif