slist_iterator.hpp 4.7 KB

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