next-iterator.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* A "next" iterator for GDB, the GNU debugger.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_NEXT_ITERATOR_H
  15. #define COMMON_NEXT_ITERATOR_H
  16. #include "gdbsupport/iterator-range.h"
  17. /* An iterator that uses the 'next' field of a type to iterate. This
  18. can be used with various GDB types that are stored as linked
  19. lists. */
  20. template<typename T>
  21. struct next_iterator
  22. {
  23. typedef next_iterator self_type;
  24. typedef T *value_type;
  25. typedef T *&reference;
  26. typedef T **pointer;
  27. typedef std::forward_iterator_tag iterator_category;
  28. typedef int difference_type;
  29. explicit next_iterator (T *item)
  30. : m_item (item)
  31. {
  32. }
  33. /* Create a one-past-the-end iterator. */
  34. next_iterator ()
  35. : m_item (nullptr)
  36. {
  37. }
  38. value_type operator* () const
  39. {
  40. return m_item;
  41. }
  42. bool operator== (const self_type &other) const
  43. {
  44. return m_item == other.m_item;
  45. }
  46. bool operator!= (const self_type &other) const
  47. {
  48. return m_item != other.m_item;
  49. }
  50. self_type &operator++ ()
  51. {
  52. m_item = m_item->next;
  53. return *this;
  54. }
  55. private:
  56. T *m_item;
  57. };
  58. /* A convenience wrapper to make a range type around a next_iterator. */
  59. template <typename T>
  60. using next_range = iterator_range<next_iterator<T>>;
  61. #endif /* COMMON_NEXT_ITERATOR_H */