Refactor svr4_create_solib_event_breakpoints
[external/binutils.git] / gdb / inferior-iter.h
1 /* Inferior iterators and ranges for GDB, the GNU debugger.
2
3    Copyright (C) 2018-2019 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #ifndef INFERIOR_ITER_H
21 #define INFERIOR_ITER_H
22
23 #include "gdbsupport/filtered-iterator.h"
24 #include "gdbsupport/safe-iterator.h"
25
26 /* A forward iterator that iterates over all inferiors.  */
27
28 class all_inferiors_iterator
29 {
30 public:
31   typedef all_inferiors_iterator self_type;
32   typedef struct inferior *value_type;
33   typedef struct inferior *&reference;
34   typedef struct inferior **pointer;
35   typedef std::forward_iterator_tag iterator_category;
36   typedef int difference_type;
37
38   /* Create an iterator pointing at HEAD.  */
39   explicit all_inferiors_iterator (inferior *head)
40     : m_inf (head)
41   {}
42
43   /* Create a one-past-end iterator.  */
44   all_inferiors_iterator ()
45     : m_inf (nullptr)
46   {}
47
48   all_inferiors_iterator &operator++ ()
49   {
50     m_inf = m_inf->next;
51     return *this;
52   }
53
54   inferior *operator* () const
55   { return m_inf; }
56
57   bool operator!= (const all_inferiors_iterator &other) const
58   { return m_inf != other.m_inf; }
59
60 private:
61   inferior *m_inf;
62 };
63
64 /* Filter for filtered_iterator.  Filters out exited inferiors.  */
65
66 struct exited_inferior_filter
67 {
68   bool operator() (inferior *inf)
69   {
70     return inf->pid != 0;
71   }
72 };
73
74 /* Iterate over all non-exited inferiors.  */
75
76 using all_non_exited_inferiors_iterator
77   = filtered_iterator<all_inferiors_iterator, exited_inferior_filter>;
78
79 /* A range adapter that makes it possible to iterate over all
80    inferiors with range-for.  */
81 struct all_inferiors_range
82 {
83   all_inferiors_iterator begin () const
84   { return all_inferiors_iterator (inferior_list); }
85   all_inferiors_iterator end () const
86   { return all_inferiors_iterator (); }
87 };
88
89 /* Iterate over all inferiors, safely.  */
90
91 using all_inferiors_safe_iterator
92   = basic_safe_iterator<all_inferiors_iterator>;
93
94 /* A range adapter that makes it possible to iterate over all
95    inferiors with range-for "safely".  I.e., it is safe to delete the
96    currently-iterated inferior.  */
97
98 struct all_inferiors_safe_range
99 {
100   all_inferiors_safe_iterator begin () const
101   { return all_inferiors_safe_iterator (inferior_list); }
102   all_inferiors_safe_iterator end () const
103   { return all_inferiors_safe_iterator (); }
104 };
105
106 /* A range adapter that makes it possible to iterate over all
107    non-exited inferiors with range-for.  */
108
109 struct all_non_exited_inferiors_range
110 {
111   all_non_exited_inferiors_iterator begin () const
112   { return all_non_exited_inferiors_iterator (inferior_list); }
113   all_non_exited_inferiors_iterator end () const
114   { return all_non_exited_inferiors_iterator (); }
115 };
116
117 #endif /* !defined (INFERIOR_ITER_H) */