Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / intrusive / detail / node_to_value.hpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga  2014-2014
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_DETAIL_NODE_TO_VALUE_HPP
14 #define BOOST_INTRUSIVE_DETAIL_NODE_TO_VALUE_HPP
15
16 #if defined(_MSC_VER)
17 #  pragma once
18 #endif
19
20 #include <boost/intrusive/pointer_traits.hpp>
21 #include <boost/intrusive/detail/mpl.hpp>
22 #include <boost/intrusive/detail/is_stateful_value_traits.hpp>
23
24 namespace boost {
25 namespace intrusive {
26 namespace detail {
27
28 template<class VoidPointer>
29 struct dummy_constptr
30 {
31    typedef typename boost::intrusive::pointer_traits<VoidPointer>::
32       template rebind_pointer<const void>::type ConstVoidPtr;
33
34    explicit dummy_constptr(ConstVoidPtr)
35    {}
36
37    dummy_constptr()
38    {}
39
40    ConstVoidPtr get_ptr() const
41    {  return ConstVoidPtr();  }
42 };
43
44 template<class VoidPointer>
45 struct constptr
46 {
47    typedef typename boost::intrusive::pointer_traits<VoidPointer>::
48       template rebind_pointer<const void>::type ConstVoidPtr;
49
50    constptr()
51    {}
52
53    explicit constptr(const ConstVoidPtr &ptr)
54       :  const_void_ptr_(ptr)
55    {}
56
57    const void *get_ptr() const
58    {  return boost::intrusive::detail::to_raw_pointer(const_void_ptr_);  }
59
60    ConstVoidPtr const_void_ptr_;
61 };
62
63 template <class VoidPointer, bool store_ptr>
64 struct select_constptr
65 {
66    typedef typename if_c
67       < store_ptr
68       , constptr<VoidPointer>
69       , dummy_constptr<VoidPointer>
70       >::type type;
71 };
72
73
74 template<class ValueTraits, bool IsConst>
75 struct node_to_value
76    :  public select_constptr
77       < typename pointer_traits
78             <typename ValueTraits::pointer>::template rebind_pointer<void>::type
79       , is_stateful_value_traits<ValueTraits>::value
80       >::type
81 {
82    static const bool stateful_value_traits = is_stateful_value_traits<ValueTraits>::value;
83    typedef typename select_constptr
84       < typename pointer_traits
85             <typename ValueTraits::pointer>::
86                template rebind_pointer<void>::type
87       , stateful_value_traits >::type                 Base;
88
89    typedef ValueTraits                                 value_traits;
90    typedef typename value_traits::value_type           value_type;
91    typedef typename value_traits::node_traits::node    node;
92    typedef typename add_const_if_c
93          <value_type, IsConst>::type                   vtype;
94    typedef typename add_const_if_c
95          <node, IsConst>::type                         ntype;
96    typedef typename pointer_traits
97       <typename ValueTraits::pointer>::
98          template rebind_pointer<ntype>::type          npointer;
99    typedef typename pointer_traits<npointer>::
100       template rebind_pointer<const ValueTraits>::type const_value_traits_ptr;
101
102    node_to_value(const const_value_traits_ptr &ptr)
103       :  Base(ptr)
104    {}
105
106    typedef vtype &                                 result_type;
107    typedef ntype &                                 first_argument_type;
108
109    const_value_traits_ptr get_value_traits() const
110    {  return pointer_traits<const_value_traits_ptr>::static_cast_from(Base::get_ptr());  }
111
112    result_type to_value(first_argument_type arg, false_) const
113    {  return *(value_traits::to_value_ptr(pointer_traits<npointer>::pointer_to(arg)));  }
114
115    result_type to_value(first_argument_type arg, true_) const
116    {  return *(this->get_value_traits()->to_value_ptr(pointer_traits<npointer>::pointer_to(arg))); }
117
118    result_type operator()(first_argument_type arg) const
119    {  return this->to_value(arg, bool_<stateful_value_traits>()); }
120 };
121
122 }  //namespace detail{
123 }  //namespace intrusive{
124 }  //namespace boost{
125
126 #endif //BOOST_INTRUSIVE_DETAIL_NODE_TO_VALUE_HPP