list_iterator.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_ITERATOR_HPP
  14. #define BOOST_INTRUSIVE_LIST_ITERATOR_HPP
  15. #ifndef BOOST_CONFIG_HPP
  16. # include <boost/config.hpp>
  17. #endif
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. #include <boost/intrusive/detail/workaround.hpp>
  22. #include <boost/intrusive/detail/std_fwd.hpp>
  23. #include <boost/intrusive/detail/iiterator.hpp>
  24. #include <boost/intrusive/detail/mpl.hpp>
  25. namespace boost {
  26. namespace intrusive {
  27. // list_iterator provides some basic functions for a
  28. // node oriented bidirectional iterator:
  29. template<class ValueTraits, bool IsConst>
  30. class list_iterator
  31. {
  32. private:
  33. typedef iiterator
  34. <ValueTraits, IsConst, std::bidirectional_iterator_tag> types_t;
  35. static const bool stateful_value_traits = types_t::stateful_value_traits;
  36. typedef ValueTraits value_traits;
  37. typedef typename types_t::node_traits node_traits;
  38. typedef typename types_t::node node;
  39. typedef typename types_t::node_ptr node_ptr;
  40. typedef typename types_t::const_value_traits_ptr const_value_traits_ptr;
  41. class nat;
  42. typedef typename
  43. detail::if_c< IsConst
  44. , list_iterator<value_traits, false>
  45. , nat>::type nonconst_iterator;
  46. public:
  47. typedef typename types_t::iterator_type::difference_type difference_type;
  48. typedef typename types_t::iterator_type::value_type value_type;
  49. typedef typename types_t::iterator_type::pointer pointer;
  50. typedef typename types_t::iterator_type::reference reference;
  51. typedef typename types_t::iterator_type::iterator_category iterator_category;
  52. BOOST_INTRUSIVE_FORCEINLINE list_iterator()
  53. {}
  54. BOOST_INTRUSIVE_FORCEINLINE explicit list_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr)
  55. : members_(nodeptr, traits_ptr)
  56. {}
  57. BOOST_INTRUSIVE_FORCEINLINE list_iterator(const list_iterator &other)
  58. : members_(other.pointed_node(), other.get_value_traits())
  59. {}
  60. BOOST_INTRUSIVE_FORCEINLINE list_iterator(const nonconst_iterator &other)
  61. : members_(other.pointed_node(), other.get_value_traits())
  62. {}
  63. BOOST_INTRUSIVE_FORCEINLINE list_iterator &operator=(const list_iterator &other)
  64. { members_.nodeptr_ = other.members_.nodeptr_; return *this; }
  65. BOOST_INTRUSIVE_FORCEINLINE node_ptr pointed_node() const
  66. { return members_.nodeptr_; }
  67. BOOST_INTRUSIVE_FORCEINLINE list_iterator &operator=(const node_ptr &node)
  68. { members_.nodeptr_ = node; return *this; }
  69. BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr get_value_traits() const
  70. { return members_.get_ptr(); }
  71. public:
  72. BOOST_INTRUSIVE_FORCEINLINE list_iterator& operator++()
  73. {
  74. node_ptr p = node_traits::get_next(members_.nodeptr_);
  75. members_.nodeptr_ = p;
  76. return static_cast<list_iterator&> (*this);
  77. }
  78. BOOST_INTRUSIVE_FORCEINLINE list_iterator operator++(int)
  79. {
  80. list_iterator result (*this);
  81. members_.nodeptr_ = node_traits::get_next(members_.nodeptr_);
  82. return result;
  83. }
  84. BOOST_INTRUSIVE_FORCEINLINE list_iterator& operator--()
  85. {
  86. members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_);
  87. return static_cast<list_iterator&> (*this);
  88. }
  89. BOOST_INTRUSIVE_FORCEINLINE list_iterator operator--(int)
  90. {
  91. list_iterator result (*this);
  92. members_.nodeptr_ = node_traits::get_previous(members_.nodeptr_);
  93. return result;
  94. }
  95. BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const list_iterator& l, const list_iterator& r)
  96. { return l.pointed_node() == r.pointed_node(); }
  97. BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const list_iterator& l, const list_iterator& r)
  98. { return !(l == r); }
  99. BOOST_INTRUSIVE_FORCEINLINE reference operator*() const
  100. { return *operator->(); }
  101. BOOST_INTRUSIVE_FORCEINLINE pointer operator->() const
  102. { return this->operator_arrow(detail::bool_<stateful_value_traits>()); }
  103. BOOST_INTRUSIVE_FORCEINLINE list_iterator<ValueTraits, false> unconst() const
  104. { return list_iterator<ValueTraits, false>(this->pointed_node(), this->get_value_traits()); }
  105. private:
  106. BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::false_) const
  107. { return ValueTraits::to_value_ptr(members_.nodeptr_); }
  108. BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::true_) const
  109. { return this->get_value_traits()->to_value_ptr(members_.nodeptr_); }
  110. iiterator_members<node_ptr, const_value_traits_ptr, stateful_value_traits> members_;
  111. };
  112. } //namespace intrusive
  113. } //namespace boost
  114. #endif //BOOST_INTRUSIVE_LIST_ITERATOR_HPP