node_cloner_disposer.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_CLONER_DISPOSER_HPP
  13. #define BOOST_INTRUSIVE_DETAIL_NODE_CLONER_DISPOSER_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/link_mode.hpp>
  21. #include <boost/intrusive/detail/mpl.hpp>
  22. #include <boost/intrusive/detail/ebo_functor_holder.hpp>
  23. #include <boost/intrusive/detail/algo_type.hpp>
  24. #include <boost/intrusive/detail/assert.hpp>
  25. namespace boost {
  26. namespace intrusive {
  27. namespace detail {
  28. template<class F, class ValueTraits, algo_types AlgoType, bool IsConst = true>
  29. struct node_cloner
  30. //Use public inheritance to avoid MSVC bugs with closures
  31. : public ebo_functor_holder<F>
  32. {
  33. typedef ValueTraits value_traits;
  34. typedef typename value_traits::node_traits node_traits;
  35. typedef typename node_traits::node_ptr node_ptr;
  36. typedef ebo_functor_holder<F> base_t;
  37. typedef typename get_algo< AlgoType
  38. , node_traits>::type node_algorithms;
  39. static const bool safemode_or_autounlink =
  40. is_safe_autounlink<value_traits::link_mode>::value;
  41. typedef typename value_traits::value_type value_type;
  42. typedef typename value_traits::pointer pointer;
  43. typedef typename value_traits::const_pointer const_pointer;
  44. typedef typename node_traits::node node;
  45. typedef typename value_traits::const_node_ptr const_node_ptr;
  46. typedef typename pointer_traits<pointer>::reference reference;
  47. typedef typename pointer_traits
  48. <const_pointer>::reference const_reference;
  49. typedef typename if_c<IsConst, const_reference, reference>::type reference_type;
  50. node_cloner(F f, const ValueTraits *traits)
  51. : base_t(f), traits_(traits)
  52. {}
  53. // tree-based containers use this method, which is proxy-reference friendly
  54. BOOST_INTRUSIVE_FORCEINLINE node_ptr operator()(const node_ptr & p)
  55. {
  56. reference_type v = *traits_->to_value_ptr(p);
  57. node_ptr n = traits_->to_node_ptr(*base_t::get()(v));
  58. //Cloned node must be in default mode if the linking mode requires it
  59. BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(!safemode_or_autounlink || node_algorithms::unique(n));
  60. return n;
  61. }
  62. const ValueTraits * const traits_;
  63. };
  64. template<class F, class ValueTraits, algo_types AlgoType>
  65. struct node_disposer
  66. //Use public inheritance to avoid MSVC bugs with closures
  67. : public ebo_functor_holder<F>
  68. {
  69. typedef ValueTraits value_traits;
  70. typedef typename value_traits::node_traits node_traits;
  71. typedef typename node_traits::node_ptr node_ptr;
  72. typedef ebo_functor_holder<F> base_t;
  73. typedef typename get_algo< AlgoType
  74. , node_traits>::type node_algorithms;
  75. static const bool safemode_or_autounlink =
  76. is_safe_autounlink<value_traits::link_mode>::value;
  77. node_disposer(F f, const ValueTraits *cont)
  78. : base_t(f), traits_(cont)
  79. {}
  80. BOOST_INTRUSIVE_FORCEINLINE void operator()(const node_ptr & p)
  81. {
  82. if(safemode_or_autounlink)
  83. node_algorithms::init(p);
  84. base_t::get()(traits_->to_value_ptr(p));
  85. }
  86. const ValueTraits * const traits_;
  87. };
  88. } //namespace detail{
  89. } //namespace intrusive{
  90. } //namespace boost{
  91. #endif //BOOST_INTRUSIVE_DETAIL_NODE_CLONER_DISPOSER_HPP