slist_iterator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. BOOST_INTRUSIVE_FORCEINLINE slist_iterator()
  54. {}
  55. BOOST_INTRUSIVE_FORCEINLINE explicit slist_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr)
  56. : members_(nodeptr, traits_ptr)
  57. {}
  58. BOOST_INTRUSIVE_FORCEINLINE slist_iterator(const slist_iterator &other)
  59. : members_(other.pointed_node(), other.get_value_traits())
  60. {}
  61. BOOST_INTRUSIVE_FORCEINLINE slist_iterator(const nonconst_iterator &other)
  62. : members_(other.pointed_node(), other.get_value_traits())
  63. {}
  64. BOOST_INTRUSIVE_FORCEINLINE slist_iterator &operator=(const slist_iterator &other)
  65. { members_.nodeptr_ = other.members_.nodeptr_; return *this; }
  66. BOOST_INTRUSIVE_FORCEINLINE node_ptr pointed_node() const
  67. { return members_.nodeptr_; }
  68. BOOST_INTRUSIVE_FORCEINLINE slist_iterator &operator=(const node_ptr &node)
  69. { members_.nodeptr_ = node; return static_cast<slist_iterator&>(*this); }
  70. BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr get_value_traits() const
  71. { return members_.get_ptr(); }
  72. public:
  73. BOOST_INTRUSIVE_FORCEINLINE slist_iterator& operator++()
  74. {
  75. members_.nodeptr_ = node_traits::get_next(members_.nodeptr_);
  76. return static_cast<slist_iterator&> (*this);
  77. }
  78. BOOST_INTRUSIVE_FORCEINLINE slist_iterator operator++(int)
  79. {
  80. slist_iterator result (*this);
  81. members_.nodeptr_ = node_traits::get_next(members_.nodeptr_);
  82. return result;
  83. }
  84. BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const slist_iterator& l, const slist_iterator& r)
  85. { return l.pointed_node() == r.pointed_node(); }
  86. BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const slist_iterator& l, const slist_iterator& r)
  87. { return !(l == r); }
  88. BOOST_INTRUSIVE_FORCEINLINE reference operator*() const
  89. { return *operator->(); }
  90. BOOST_INTRUSIVE_FORCEINLINE pointer operator->() const
  91. { return this->operator_arrow(detail::bool_<stateful_value_traits>()); }
  92. BOOST_INTRUSIVE_FORCEINLINE slist_iterator<ValueTraits, false> unconst() const
  93. { return slist_iterator<ValueTraits, false>(this->pointed_node(), this->get_value_traits()); }
  94. private:
  95. BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::false_) const
  96. { return ValueTraits::to_value_ptr(members_.nodeptr_); }
  97. BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::true_) const
  98. { return this->get_value_traits()->to_value_ptr(members_.nodeptr_); }
  99. iiterator_members<node_ptr, const_value_traits_ptr, stateful_value_traits> members_;
  100. };
  101. } //namespace intrusive
  102. } //namespace boost
  103. #include <boost/intrusive/detail/config_end.hpp>
  104. #endif //BOOST_INTRUSIVE_SLIST_ITERATOR_HPP