Move include files in debian packge into 'senf' subdirectory
[senf.git] / boost / intrusive / detail / utilities.hpp
1 /////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 // (C) Copyright Ion GaztaƱaga  2006-2007\r
4 //\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
8 //\r
9 // See http://www.boost.org/libs/intrusive for documentation.\r
10 //\r
11 /////////////////////////////////////////////////////////////////////////////\r
12 \r
13 #ifndef BOOST_INTRUSIVE_DETAIL_UTILITIES_HPP\r
14 #define BOOST_INTRUSIVE_DETAIL_UTILITIES_HPP\r
15 \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
20 #include <cstddef>\r
21 \r
22 namespace boost {\r
23 namespace intrusive {\r
24 namespace detail {\r
25 \r
26 class null_destroyer\r
27 {\r
28    public:\r
29    template <class Pointer>\r
30    void operator()(Pointer)\r
31    {}\r
32 };\r
33 \r
34 template<bool ConstantSize, class SizeType>\r
35 struct size_holder\r
36 {\r
37    enum {   constant_time_size = ConstantSize  };\r
38    typedef SizeType  size_type;\r
39 \r
40    SizeType get_size() const\r
41    {  return size_;  }\r
42 \r
43    void set_size(SizeType size)\r
44    {  size_ = size; }\r
45 \r
46    void decrement()\r
47    {  --size_; }\r
48 \r
49    void increment()\r
50    {  ++size_; }\r
51 \r
52    SizeType size_;\r
53 };\r
54 \r
55 template<class SizeType>\r
56 struct size_holder<false, SizeType>\r
57 {\r
58    enum {   constant_time_size = false  };\r
59    typedef SizeType  size_type;\r
60 \r
61    size_type get_size() const\r
62    {  return 0;  }\r
63 \r
64    void set_size(size_type)\r
65    {}\r
66 \r
67    void decrement()\r
68    {}\r
69 \r
70    void increment()\r
71    {}\r
72 };\r
73 \r
74 template<class T, class DerivationHookType, typename Tag>\r
75 struct derivation_value_traits\r
76 {\r
77    public:\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
87 \r
88    static node_ptr to_node_ptr(value_type &value)\r
89    { return static_cast<DerivationHookType &>(value).to_node_ptr(); }\r
90 \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
93 \r
94    static pointer to_value_ptr(node_ptr n) \r
95    { \r
96       using boost::get_pointer;\r
97       return static_cast<T*>(get_pointer(DerivationHookType::to_hook_ptr(n))); \r
98    }\r
99 \r
100    static const_pointer to_value_ptr(const_node_ptr n)\r
101    { \r
102       using boost::get_pointer;\r
103       return static_cast<const T*>(get_pointer(DerivationHookType::to_hook_ptr(n))); \r
104    }\r
105 };\r
106 \r
107 \r
108 template<class T, class MemberHookType, MemberHookType T::* P>\r
109 struct member_value_traits\r
110 {\r
111    public:\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
121 \r
122    public:\r
123    static node_ptr to_node_ptr(value_type &value)\r
124    {\r
125       MemberHookType* result = &(value.*P);\r
126       return result->to_node_ptr();\r
127    }\r
128 \r
129    static const_node_ptr to_node_ptr(const value_type &value)\r
130    {\r
131       const MemberHookType* result = &(value.*P);\r
132       return result->to_node_ptr();\r
133    }\r
134 \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
143    {\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
147    }\r
148 \r
149    static const_pointer to_value_ptr(const_node_ptr n)\r
150    {\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
154    }\r
155    private:\r
156    static std::size_t value_to_node_offset()\r
157    {\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
162    }\r
163 };\r
164 \r
165 } //namespace detail \r
166 } //namespace intrusive \r
167 } //namespace boost \r
168 \r
169 #include "config_end.hpp"\r
170 \r
171 #endif //BOOST_INTRUSIVE_DETAIL_UTILITIES_HPP\r