inferior-iter.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Inferior iterators and ranges for GDB, the GNU debugger.
  2. Copyright (C) 2018-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 INFERIOR_ITER_H
  15. #define INFERIOR_ITER_H
  16. #include "gdbsupport/filtered-iterator.h"
  17. #include "gdbsupport/safe-iterator.h"
  18. /* A forward iterator that iterates over all inferiors. */
  19. class all_inferiors_iterator
  20. {
  21. public:
  22. typedef all_inferiors_iterator self_type;
  23. typedef struct inferior *value_type;
  24. typedef struct inferior *&reference;
  25. typedef struct inferior **pointer;
  26. typedef std::forward_iterator_tag iterator_category;
  27. typedef int difference_type;
  28. /* Create an iterator pointing at HEAD. */
  29. all_inferiors_iterator (process_stratum_target *proc_target,
  30. const intrusive_list<inferior> &list)
  31. : m_proc_target (proc_target), m_inf_iter (list.begin ())
  32. {
  33. intrusive_list<inferior>::iterator end;
  34. /* Advance M_INF to the first inferior's position. */
  35. for (; m_inf_iter != end; ++m_inf_iter)
  36. if (m_inf_matches ())
  37. return;
  38. }
  39. /* Create a one-past-end iterator. */
  40. all_inferiors_iterator ()
  41. : m_proc_target (nullptr)
  42. {}
  43. all_inferiors_iterator &operator++ ()
  44. {
  45. advance ();
  46. return *this;
  47. }
  48. inferior *operator* () const
  49. { return &*m_inf_iter; }
  50. bool operator!= (const all_inferiors_iterator &other) const
  51. { return m_inf_iter != other.m_inf_iter; }
  52. private:
  53. /* Advance to next inferior, skipping filtered inferiors. */
  54. void advance ()
  55. {
  56. intrusive_list<inferior>::iterator end;
  57. /* The loop below is written in the natural way as-if we'd always
  58. start at the beginning of the inferior list. This
  59. fast-forwards the algorithm to the actual current position. */
  60. goto start;
  61. while (m_inf_iter != end)
  62. {
  63. if (m_inf_matches ())
  64. return;
  65. start:
  66. ++m_inf_iter;
  67. }
  68. }
  69. bool m_inf_matches ()
  70. {
  71. return (m_proc_target == nullptr
  72. || m_proc_target == m_inf_iter->process_target ());
  73. }
  74. process_stratum_target *m_proc_target;
  75. intrusive_list<inferior>::iterator m_inf_iter;
  76. };
  77. /* A range adapter that makes it possible to iterate over all
  78. inferiors with range-for. */
  79. using all_inferiors_range = iterator_range<all_inferiors_iterator>;
  80. /* Filter for filtered_iterator. Filters out exited inferiors. */
  81. struct exited_inferior_filter
  82. {
  83. bool operator() (inferior *inf)
  84. {
  85. return inf->pid != 0;
  86. }
  87. };
  88. /* Iterate over all non-exited inferiors. */
  89. using all_non_exited_inferiors_iterator
  90. = filtered_iterator<all_inferiors_iterator, exited_inferior_filter>;
  91. /* A range adapter that makes it possible to iterate over all
  92. non-exited inferiors with range-for. */
  93. using all_non_exited_inferiors_range
  94. = iterator_range<all_non_exited_inferiors_iterator>;
  95. /* Iterate over all inferiors, safely. */
  96. using all_inferiors_safe_iterator
  97. = basic_safe_iterator<all_inferiors_iterator>;
  98. /* A range adapter that makes it possible to iterate over all
  99. inferiors with range-for "safely". I.e., it is safe to delete the
  100. currently-iterated inferior. */
  101. using all_inferiors_safe_range = iterator_range<all_inferiors_safe_iterator>;
  102. #endif /* !defined (INFERIOR_ITER_H) */