list_node.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Olaf Krzikalla 2004-2006.
  4. // (C) Copyright Ion Gaztanaga 2006-2013
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/intrusive for documentation.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_INTRUSIVE_LIST_NODE_HPP
  14. #define BOOST_INTRUSIVE_LIST_NODE_HPP
  15. #include <boost/intrusive/detail/config_begin.hpp>
  16. #include <iterator>
  17. #include <boost/intrusive/detail/assert.hpp>
  18. #include <boost/intrusive/pointer_traits.hpp>
  19. namespace boost {
  20. namespace intrusive {
  21. // list_node_traits can be used with circular_list_algorithms and supplies
  22. // a list_node holding the pointers needed for a double-linked list
  23. // it is used by list_derived_node and list_member_node
  24. template<class VoidPointer>
  25. struct list_node
  26. {
  27. typedef typename pointer_traits
  28. <VoidPointer>:: template rebind_pointer<list_node>::type node_ptr;
  29. node_ptr next_;
  30. node_ptr prev_;
  31. };
  32. template<class VoidPointer>
  33. struct list_node_traits
  34. {
  35. typedef list_node<VoidPointer> node;
  36. typedef typename pointer_traits
  37. <VoidPointer>:: template rebind_pointer<node>::type node_ptr;
  38. typedef typename pointer_traits
  39. <VoidPointer>:: template rebind_pointer<const node>::type const_node_ptr;
  40. static node_ptr get_previous(const const_node_ptr & n)
  41. { return n->prev_; }
  42. static node_ptr get_previous(const node_ptr & n)
  43. { return n->prev_; }
  44. static void set_previous(const node_ptr & n, const node_ptr & prev)
  45. { n->prev_ = prev; }
  46. static node_ptr get_next(const const_node_ptr & n)
  47. { return n->next_; }
  48. static node_ptr get_next(const node_ptr & n)
  49. { return n->next_; }
  50. static void set_next(const node_ptr & n, const node_ptr & next)
  51. { n->next_ = next; }
  52. };
  53. // list_iterator provides some basic functions for a
  54. // node oriented bidirectional iterator:
  55. template<class RealValueTraits, bool IsConst>
  56. class list_iterator
  57. : public iiterator<RealValueTraits, IsConst, std::bidirectional_iterator_tag>::iterator_base
  58. {
  59. protected:
  60. typedef iiterator
  61. <RealValueTraits, IsConst, std::bidirectional_iterator_tag> types_t;
  62. static const bool stateful_value_traits = types_t::stateful_value_traits;
  63. typedef RealValueTraits real_value_traits;
  64. typedef typename types_t::node_traits node_traits;
  65. typedef typename types_t::node node;
  66. typedef typename types_t::node_ptr node_ptr;
  67. typedef typename types_t::void_pointer void_pointer;
  68. public:
  69. typedef typename types_t::value_type value_type;
  70. typedef typename types_t::pointer pointer;
  71. typedef typename types_t::reference reference;
  72. typedef typename pointer_traits
  73. <void_pointer>::template rebind_pointer
  74. <const real_value_traits>::type const_real_value_traits_ptr;
  75. list_iterator()
  76. {}
  77. explicit list_iterator(const node_ptr & nodeptr, const const_real_value_traits_ptr &traits_ptr)
  78. : members_(nodeptr, traits_ptr)
  79. {}
  80. list_iterator(list_iterator<RealValueTraits, false> const& other)
  81. : members_(other.pointed_node(), other.get_real_value_traits())
  82. {}
  83. const node_ptr &pointed_node() const
  84. { return members_.nodeptr_; }
  85. list_iterator &operator=(const node_ptr &node)
  86. { members_.nodeptr_ = node; return static_cast<list_iterator&>(*this); }
  87. const_real_value_traits_ptr get_real_value_traits() const
  88. { return pointer_traits<const_real_value_traits_ptr>::static_cast_from(members_.get_ptr()); }
  89. public:
  90. list_iterator& operator++()
  91. {
  92. node_ptr p = node_traits::get_next(members_.nodeptr_);
  93. members_.nodeptr_ = p;
  94. return static_cast<list_iterator&> (*this);
  95. }
  96. list_iterator operator++(int)
  97. {
  98. list_iterator result (*this);
  99. members_.nodeptr_ = node_traits::get_next(members_.nodeptr_);
  100. return result;
  101. }
  102. list_iterator& operator--()
  103. {
  104. members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_);
  105. return static_cast<list_iterator&> (*this);
  106. }
  107. list_iterator operator--(int)
  108. {
  109. list_iterator result (*this);
  110. members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_);
  111. return result;
  112. }
  113. friend bool operator== (const list_iterator& l, const list_iterator& r)
  114. { return l.pointed_node() == r.pointed_node(); }
  115. friend bool operator!= (const list_iterator& l, const list_iterator& r)
  116. { return !(l == r); }
  117. reference operator*() const
  118. { return *operator->(); }
  119. pointer operator->() const
  120. { return this->get_real_value_traits()->to_value_ptr(members_.nodeptr_); }
  121. list_iterator<RealValueTraits, false> unconst() const
  122. { return list_iterator<RealValueTraits, false>(this->pointed_node(), this->get_real_value_traits()); }
  123. private:
  124. iiterator_members<node_ptr, stateful_value_traits> members_;
  125. };
  126. } //namespace intrusive
  127. } //namespace boost
  128. #include <boost/intrusive/detail/config_end.hpp>
  129. #endif //BOOST_INTRUSIVE_LIST_NODE_HPP