1 /////////////////////////////////////////////////////////////////////////////
\r
3 // (C) Copyright Ion GaztaƱaga 2006-2007
\r
5 // Distributed under the Boost Software License, Version 1.0.
\r
6 // (See accompanying file LICENSE_1_0.txt or copy at
\r
7 // http://www.boost.org/LICENSE_1_0.txt)
\r
9 // See http://www.boost.org/libs/intrusive for documentation.
\r
11 /////////////////////////////////////////////////////////////////////////////
\r
13 #ifndef BOOST_INTRUSIVE_DETAIL_UTILITIES_HPP
\r
14 #define BOOST_INTRUSIVE_DETAIL_UTILITIES_HPP
\r
16 #include "config_begin.hpp"
\r
17 #include "pointer_type.hpp"
\r
18 #include "pointer_to_other.hpp"
\r
19 #include <boost/get_pointer.hpp>
\r
23 namespace intrusive {
\r
26 class null_destroyer
\r
29 template <class Pointer>
\r
30 void operator()(Pointer)
\r
34 template<bool ConstantSize, class SizeType>
\r
37 enum { constant_time_size = ConstantSize };
\r
38 typedef SizeType size_type;
\r
40 SizeType get_size() const
\r
43 void set_size(SizeType size)
\r
55 template<class SizeType>
\r
56 struct size_holder<false, SizeType>
\r
58 enum { constant_time_size = false };
\r
59 typedef SizeType size_type;
\r
61 size_type get_size() const
\r
64 void set_size(size_type)
\r
74 template<class T, class DerivationHookType, typename Tag>
\r
75 struct derivation_value_traits
\r
78 typedef typename DerivationHookType::node_traits node_traits;
\r
79 typedef T value_type;
\r
80 typedef typename node_traits::node_ptr node_ptr;
\r
81 typedef typename node_traits::const_node_ptr const_node_ptr;
\r
82 typedef typename boost::pointer_to_other
\r
83 <node_ptr, T>::type pointer;
\r
84 typedef typename boost::pointer_to_other
\r
85 <node_ptr, const T>::type const_pointer;
\r
86 enum { linking_policy = DerivationHookType::linking_policy };
\r
88 static node_ptr to_node_ptr(value_type &value)
\r
89 { return static_cast<DerivationHookType &>(value).to_node_ptr(); }
\r
91 static const_node_ptr to_node_ptr(const value_type &value)
\r
92 { return static_cast<const DerivationHookType &>(value).to_node_ptr(); }
\r
94 static pointer to_value_ptr(node_ptr n)
\r
96 using boost::get_pointer;
\r
97 return static_cast<T*>(get_pointer(DerivationHookType::to_hook_ptr(n)));
\r
100 static const_pointer to_value_ptr(const_node_ptr n)
\r
102 using boost::get_pointer;
\r
103 return static_cast<const T*>(get_pointer(DerivationHookType::to_hook_ptr(n)));
\r
108 template<class T, class MemberHookType, MemberHookType T::* P>
\r
109 struct member_value_traits
\r
112 typedef typename MemberHookType::node_traits node_traits;
\r
113 typedef T value_type;
\r
114 typedef typename node_traits::node_ptr node_ptr;
\r
115 typedef typename node_traits::const_node_ptr const_node_ptr;
\r
116 typedef typename boost::pointer_to_other
\r
117 <node_ptr, T>::type pointer;
\r
118 typedef typename boost::pointer_to_other
\r
119 <node_ptr, const T>::type const_pointer;
\r
120 enum { linking_policy = MemberHookType::linking_policy };
\r
123 static node_ptr to_node_ptr(value_type &value)
\r
125 MemberHookType* result = &(value.*P);
\r
126 return result->to_node_ptr();
\r
129 static const_node_ptr to_node_ptr(const value_type &value)
\r
131 const MemberHookType* result = &(value.*P);
\r
132 return result->to_node_ptr();
\r
135 //Now let's be nasty. The distance between the
\r
136 //start of the value_type and the internal node
\r
137 //is constant. That's why a pointer to member
\r
138 //is a compile-time value. Now apply it with
\r
139 //a dummy value, get the offset in bytes and go
\r
140 //backwards from n to the value subtracting
\r
141 //the needed bytes.
\r
142 static pointer to_value_ptr(node_ptr n)
\r
144 using boost::get_pointer;
\r
145 return pointer(reinterpret_cast<value_type*>
\r
146 ((char*)get_pointer(n) - value_to_node_offset()));
\r
149 static const_pointer to_value_ptr(const_node_ptr n)
\r
151 using boost::get_pointer;
\r
152 return const_pointer(reinterpret_cast<const value_type*>
\r
153 ((const char*)get_pointer(n) - value_to_node_offset()));
\r
156 static std::size_t value_to_node_offset()
\r
158 using boost::get_pointer;
\r
159 const typename node_traits::node *np =
\r
160 get_pointer((((const value_type *)0)->*P).to_node_ptr());
\r
161 return ((const char*)np - (const char*)(const value_type *)0);
\r
165 } //namespace detail
\r
166 } //namespace intrusive
\r
167 } //namespace boost
\r
169 #include "config_end.hpp"
\r
171 #endif //BOOST_INTRUSIVE_DETAIL_UTILITIES_HPP
\r