]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/intrusive/detail/generic_hook.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / intrusive / detail / generic_hook.hpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007-2009
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 #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>
23
24 namespace boost {
25 namespace intrusive {
26 namespace detail {
27
28 /// @cond
29
30 enum
31 {  NoBaseHook
32 ,  ListBaseHook
33 ,  SlistBaseHook
34 ,  SetBaseHook
35 ,  UsetBaseHook
36 ,  SplaySetBaseHook
37 ,  AvlSetBaseHook
38 ,  BsSetBaseHook
39 ,  AnyBaseHook
40 };
41
42 struct no_default_definer{};
43
44 template <class Hook, unsigned int>
45 struct default_definer;
46
47 template <class Hook>
48 struct default_definer<Hook, ListBaseHook>
49 {  typedef Hook default_list_hook;  };
50
51 template <class Hook>
52 struct default_definer<Hook, SlistBaseHook>
53 {  typedef Hook default_slist_hook;  };
54
55 template <class Hook>
56 struct default_definer<Hook, SetBaseHook>
57 {  typedef Hook default_set_hook;  };
58
59 template <class Hook>
60 struct default_definer<Hook, UsetBaseHook>
61 {  typedef Hook default_uset_hook;  };
62
63 template <class Hook>
64 struct default_definer<Hook, SplaySetBaseHook>
65 {  typedef Hook default_splay_set_hook;  };
66
67 template <class Hook>
68 struct default_definer<Hook, AvlSetBaseHook>
69 {  typedef Hook default_avl_set_hook;  };
70
71 template <class Hook>
72 struct default_definer<Hook, BsSetBaseHook>
73 {  typedef Hook default_bs_set_hook;  };
74
75 template <class Hook>
76 struct default_definer<Hook, AnyBaseHook>
77 {  typedef Hook default_any_hook;  };
78
79 template <class Hook, unsigned int BaseHookType>
80 struct make_default_definer
81 {
82    typedef typename detail::if_c
83       < BaseHookType != 0
84       , default_definer<Hook, BaseHookType>
85       , no_default_definer>::type type;
86 };
87
88 template
89    < class GetNodeAlgorithms
90    , class Tag
91    , link_mode_type LinkMode
92    , int HookType
93    >
94 struct make_node_holder
95 {
96    typedef typename detail::if_c
97       <!detail::is_same<Tag, member_tag>::value
98       , detail::node_holder
99          < typename GetNodeAlgorithms::type::node
100          , Tag
101          , LinkMode
102          , HookType>
103       , typename GetNodeAlgorithms::type::node
104       >::type type;
105 };
106
107 /// @endcond
108
109 template
110    < class GetNodeAlgorithms
111    , class Tag
112    , link_mode_type LinkMode
113    , int HookType
114    >
115 class generic_hook
116    /// @cond
117
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.
121    //
122    //If the hook is a member hook, generic hook will directly derive
123    //from the hook.
124    : public make_default_definer
125       < generic_hook<GetNodeAlgorithms, Tag, LinkMode, HookType>
126       , detail::is_same<Tag, default_tag>::value*HookType
127       >::type
128    , public make_node_holder<GetNodeAlgorithms, Tag, LinkMode, HookType>::type
129    /// @endcond
130 {
131    /// @cond
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;
136
137    public:
138    struct boost_intrusive_tags
139    {
140       static const int hook_type = HookType;
141       static const link_mode_type link_mode = LinkMode;
142       typedef Tag                                           tag;
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;
147    };
148
149    public:
150    /// @endcond
151
152    generic_hook()
153    {
154       if(boost_intrusive_tags::safemode_or_autounlink){
155          node_algorithms::init(static_cast<node*>(this));
156       }
157    }
158
159    generic_hook(const generic_hook& ) 
160    {
161       if(boost_intrusive_tags::safemode_or_autounlink){
162          node_algorithms::init(static_cast<node*>(this));
163       }
164    }
165
166    generic_hook& operator=(const generic_hook& ) 
167    {  return *this;  }
168
169    ~generic_hook()
170    {
171       destructor_impl
172          (*this, detail::link_dispatch<boost_intrusive_tags::link_mode>());
173    }
174
175    void swap_nodes(generic_hook &other) 
176    {
177       node_algorithms::swap_nodes
178          ( static_cast<node*>(this), static_cast<node*>(&other));
179    }
180
181    bool is_linked() const 
182    {
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));
187    }
188
189    void unlink()
190    {
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));
194    }
195 };
196
197 } //namespace detail
198 } //namespace intrusive 
199 } //namespace boost 
200
201 #include <boost/intrusive/detail/config_end.hpp>
202
203 #endif //BOOST_INTRUSIVE_GENERIC_HOOK_HPP