Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2007-2013 Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/intrusive for documentation. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTRUSIVE_GENERIC_HOOK_HPP Chris@16: #define BOOST_INTRUSIVE_GENERIC_HOOK_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace intrusive { Chris@16: Chris@16: /// @cond Chris@16: Chris@101: namespace detail { Chris@101: Chris@101: template Chris@101: struct link_dispatch Chris@101: {}; Chris@101: Chris@101: template Chris@101: void destructor_impl(Hook &hook, detail::link_dispatch) Chris@101: { //If this assertion raises, you might have destroyed an object Chris@101: //while it was still inserted in a container that is alive. Chris@101: //If so, remove the object from the container before destroying it. Chris@101: (void)hook; BOOST_INTRUSIVE_SAFE_HOOK_DESTRUCTOR_ASSERT(!hook.is_linked()); Chris@101: } Chris@101: Chris@101: template Chris@101: void destructor_impl(Hook &hook, detail::link_dispatch) Chris@101: { hook.unlink(); } Chris@101: Chris@101: template Chris@101: void destructor_impl(Hook &, detail::link_dispatch) Chris@101: {} Chris@101: Chris@101: } //namespace detail { Chris@101: Chris@16: enum base_hook_type Chris@16: { NoBaseHookId Chris@16: , ListBaseHookId Chris@16: , SlistBaseHookId Chris@16: , RbTreeBaseHookId Chris@16: , HashBaseHookId Chris@16: , AvlTreeBaseHookId Chris@16: , BsTreeBaseHookId Chris@101: , TreapTreeBaseHookId Chris@16: , AnyBaseHookId Chris@16: }; Chris@16: Chris@16: Chris@16: template Chris@16: struct hook_tags_definer{}; Chris@16: Chris@16: template Chris@16: struct hook_tags_definer Chris@16: { typedef HookTags default_list_hook; }; Chris@16: Chris@16: template Chris@16: struct hook_tags_definer Chris@16: { typedef HookTags default_slist_hook; }; Chris@16: Chris@16: template Chris@16: struct hook_tags_definer Chris@16: { typedef HookTags default_rbtree_hook; }; Chris@16: Chris@16: template Chris@16: struct hook_tags_definer Chris@16: { typedef HookTags default_hashtable_hook; }; Chris@16: Chris@16: template Chris@16: struct hook_tags_definer Chris@16: { typedef HookTags default_avltree_hook; }; Chris@16: Chris@16: template Chris@16: struct hook_tags_definer Chris@16: { typedef HookTags default_bstree_hook; }; Chris@16: Chris@16: template Chris@16: struct hook_tags_definer Chris@16: { typedef HookTags default_any_hook; }; Chris@16: Chris@16: template Chris@16: < class NodeTraits Chris@16: , class Tag Chris@16: , link_mode_type LinkMode Chris@16: , base_hook_type BaseHookType Chris@16: > Chris@16: struct hooktags_impl Chris@16: { Chris@16: static const link_mode_type link_mode = LinkMode; Chris@16: typedef Tag tag; Chris@16: typedef NodeTraits node_traits; Chris@16: static const bool is_base_hook = !detail::is_same::value; Chris@16: static const bool safemode_or_autounlink = is_safe_autounlink::value; Chris@16: static const unsigned int type = BaseHookType; Chris@16: }; Chris@16: Chris@16: /// @endcond Chris@16: Chris@16: template Chris@101: < class NodeAlgorithms Chris@16: , class Tag Chris@16: , link_mode_type LinkMode Chris@16: , base_hook_type BaseHookType Chris@16: > Chris@16: class generic_hook Chris@16: /// @cond Chris@16: //If the hook is a base hook, derive generic hook from node_holder Chris@16: //so that a unique base class is created to convert from the node Chris@16: //to the type. This mechanism will be used by bhtraits. Chris@16: // Chris@16: //If the hook is a member hook, generic hook will directly derive Chris@16: //from the hook. Chris@16: : public detail::if_c Chris@16: < detail::is_same::value Chris@101: , typename NodeAlgorithms::node Chris@101: , node_holder Chris@16: >::type Chris@101: //If this is the a default-tagged base hook derive from a class that Chris@16: //will define an special internal typedef. Containers will be able to detect this Chris@16: //special typedef and obtain generic_hook's internal types in order to deduce Chris@16: //value_traits for this hook. Chris@16: , public hook_tags_definer Chris@101: < generic_hook Chris@101: , detail::is_same::value*BaseHookType> Chris@16: /// @endcond Chris@16: { Chris@16: /// @cond Chris@101: typedef NodeAlgorithms node_algorithms; Chris@16: typedef typename node_algorithms::node node; Chris@16: typedef typename node_algorithms::node_ptr node_ptr; Chris@16: typedef typename node_algorithms::const_node_ptr const_node_ptr; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef hooktags_impl Chris@101: < typename NodeAlgorithms::node_traits Chris@16: , Tag, LinkMode, BaseHookType> hooktags; Chris@16: Chris@16: node_ptr this_ptr() Chris@16: { return pointer_traits::pointer_to(static_cast(*this)); } Chris@16: Chris@16: const_node_ptr this_ptr() const Chris@16: { return pointer_traits::pointer_to(static_cast(*this)); } Chris@16: Chris@16: public: Chris@16: /// @endcond Chris@16: Chris@16: generic_hook() Chris@16: { Chris@16: if(hooktags::safemode_or_autounlink){ Chris@16: node_algorithms::init(this->this_ptr()); Chris@16: } Chris@16: } Chris@16: Chris@16: generic_hook(const generic_hook& ) Chris@16: { Chris@16: if(hooktags::safemode_or_autounlink){ Chris@16: node_algorithms::init(this->this_ptr()); Chris@16: } Chris@16: } Chris@16: Chris@16: generic_hook& operator=(const generic_hook& ) Chris@16: { return *this; } Chris@16: Chris@16: ~generic_hook() Chris@16: { Chris@16: destructor_impl Chris@16: (*this, detail::link_dispatch()); Chris@16: } Chris@16: Chris@16: void swap_nodes(generic_hook &other) Chris@16: { Chris@16: node_algorithms::swap_nodes Chris@16: (this->this_ptr(), other.this_ptr()); Chris@16: } Chris@16: Chris@16: bool is_linked() const Chris@16: { Chris@16: //is_linked() can be only used in safe-mode or auto-unlink Chris@16: BOOST_STATIC_ASSERT(( hooktags::safemode_or_autounlink )); Chris@16: return !node_algorithms::unique(this->this_ptr()); Chris@16: } Chris@16: Chris@16: void unlink() Chris@16: { Chris@16: BOOST_STATIC_ASSERT(( (int)hooktags::link_mode == (int)auto_unlink )); Chris@101: node_ptr n(this->this_ptr()); Chris@101: if(!node_algorithms::inited(n)){ Chris@101: node_algorithms::unlink(n); Chris@101: node_algorithms::init(n); Chris@101: } Chris@16: } Chris@16: }; Chris@16: Chris@16: } //namespace intrusive Chris@16: } //namespace boost Chris@16: Chris@16: #endif //BOOST_INTRUSIVE_GENERIC_HOOK_HPP