reference-to-pointer-iterator.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* An iterator wrapper that yields pointers instead of references.
  2. Copyright (C) 2021-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 GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H
  15. #define GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H
  16. /* Wrap an iterator that yields references to objects so that it yields
  17. pointers to objects instead.
  18. This is useful for example to bridge the gap between iterators on intrusive
  19. lists, which yield references, and the rest of GDB, which for legacy reasons
  20. expects to iterate on pointers. */
  21. template <typename IteratorType>
  22. struct reference_to_pointer_iterator
  23. {
  24. using self_type = reference_to_pointer_iterator;
  25. using value_type = typename IteratorType::value_type *;
  26. using reference = typename IteratorType::value_type *&;
  27. using pointer = typename IteratorType::value_type **;
  28. using iterator_category = typename IteratorType::iterator_category;
  29. using difference_type = typename IteratorType::difference_type;
  30. /* Construct a reference_to_pointer_iterator, passing args to the underyling
  31. iterator. */
  32. template <typename... Args>
  33. reference_to_pointer_iterator (Args &&...args)
  34. : m_it (std::forward<Args> (args)...)
  35. {}
  36. /* Create a past-the-end iterator.
  37. Assumes that default-constructing an underlying iterator creates a
  38. past-the-end iterator. */
  39. reference_to_pointer_iterator ()
  40. {}
  41. /* Need these as the variadic constructor would be a better match
  42. otherwise. */
  43. reference_to_pointer_iterator (reference_to_pointer_iterator &) = default;
  44. reference_to_pointer_iterator (const reference_to_pointer_iterator &) = default;
  45. reference_to_pointer_iterator (reference_to_pointer_iterator &&) = default;
  46. reference_to_pointer_iterator &operator= (const reference_to_pointer_iterator &) = default;
  47. reference_to_pointer_iterator &operator= (reference_to_pointer_iterator &&) = default;
  48. value_type operator* () const
  49. { return &*m_it; }
  50. self_type &operator++ ()
  51. {
  52. ++m_it;
  53. return *this;
  54. }
  55. bool operator== (const self_type &other) const
  56. { return m_it == other.m_it; }
  57. bool operator!= (const self_type &other) const
  58. { return m_it != other.m_it; }
  59. private:
  60. /* The underlying iterator. */
  61. IteratorType m_it;
  62. };
  63. #endif /* GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H */