node_to_value.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef BOOST_INTRUSIVE_DETAIL_NODE_TO_VALUE_HPP
  13. #define BOOST_INTRUSIVE_DETAIL_NODE_TO_VALUE_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  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. namespace boost {
  24. namespace intrusive {
  25. namespace detail {
  26. template<class VoidPointer>
  27. struct dummy_constptr
  28. {
  29. typedef typename boost::intrusive::pointer_traits<VoidPointer>::
  30. template rebind_pointer<const void>::type ConstVoidPtr;
  31. explicit dummy_constptr(ConstVoidPtr)
  32. {}
  33. dummy_constptr()
  34. {}
  35. ConstVoidPtr get_ptr() const
  36. { return ConstVoidPtr(); }
  37. };
  38. template<class VoidPointer>
  39. struct constptr
  40. {
  41. typedef typename boost::intrusive::pointer_traits<VoidPointer>::
  42. template rebind_pointer<const void>::type ConstVoidPtr;
  43. constptr()
  44. {}
  45. explicit constptr(const ConstVoidPtr &ptr)
  46. : const_void_ptr_(ptr)
  47. {}
  48. const void *get_ptr() const
  49. { return boost::movelib::to_raw_pointer(const_void_ptr_); }
  50. ConstVoidPtr const_void_ptr_;
  51. };
  52. template <class VoidPointer, bool store_ptr>
  53. struct select_constptr
  54. {
  55. typedef typename if_c
  56. < store_ptr
  57. , constptr<VoidPointer>
  58. , dummy_constptr<VoidPointer>
  59. >::type type;
  60. };
  61. template<class ValueTraits, bool IsConst>
  62. struct node_to_value
  63. : public select_constptr
  64. < typename pointer_traits
  65. <typename ValueTraits::pointer>::template rebind_pointer<void>::type
  66. , is_stateful_value_traits<ValueTraits>::value
  67. >::type
  68. {
  69. static const bool stateful_value_traits = is_stateful_value_traits<ValueTraits>::value;
  70. typedef typename select_constptr
  71. < typename pointer_traits
  72. <typename ValueTraits::pointer>::
  73. template rebind_pointer<void>::type
  74. , stateful_value_traits >::type Base;
  75. typedef ValueTraits value_traits;
  76. typedef typename value_traits::value_type value_type;
  77. typedef typename value_traits::node_traits::node node;
  78. typedef typename add_const_if_c
  79. <value_type, IsConst>::type vtype;
  80. typedef typename add_const_if_c
  81. <node, IsConst>::type ntype;
  82. typedef typename pointer_traits
  83. <typename ValueTraits::pointer>::
  84. template rebind_pointer<ntype>::type npointer;
  85. typedef typename pointer_traits<npointer>::
  86. template rebind_pointer<const ValueTraits>::type const_value_traits_ptr;
  87. node_to_value(const const_value_traits_ptr &ptr)
  88. : Base(ptr)
  89. {}
  90. typedef vtype & result_type;
  91. typedef ntype & first_argument_type;
  92. const_value_traits_ptr get_value_traits() const
  93. { return pointer_traits<const_value_traits_ptr>::static_cast_from(Base::get_ptr()); }
  94. result_type to_value(first_argument_type arg, false_) const
  95. { return *(value_traits::to_value_ptr(pointer_traits<npointer>::pointer_to(arg))); }
  96. result_type to_value(first_argument_type arg, true_) const
  97. { return *(this->get_value_traits()->to_value_ptr(pointer_traits<npointer>::pointer_to(arg))); }
  98. result_type operator()(first_argument_type arg) const
  99. { return this->to_value(arg, bool_<stateful_value_traits>()); }
  100. };
  101. } //namespace detail{
  102. } //namespace intrusive{
  103. } //namespace boost{
  104. #endif //BOOST_INTRUSIVE_DETAIL_NODE_TO_VALUE_HPP