gdbsupport: add observer_debug_printf, OBSERVER_SCOPED_DEBUG_ENTER_EXIT
[binutils-gdb.git] / gdbsupport / observable.h
1 /* Observers
2
3 Copyright (C) 2016-2021 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 COMMON_OBSERVABLE_H
21 #define COMMON_OBSERVABLE_H
22
23 #include <algorithm>
24 #include <functional>
25 #include <vector>
26
27 /* Print an "observer" debug statement. */
28
29 #define observer_debug_printf(fmt, ...) \
30 debug_prefixed_printf_cond (observer_debug, "observer", fmt, ##__VA_ARGS__)
31
32 /* Print "observer" start/end debug statements. */
33
34 #define OBSERVER_SCOPED_DEBUG_START_END(fmt, ...) \
35 scoped_debug_start_end (observer_debug, "observer", fmt, ##__VA_ARGS__)
36
37 namespace gdb
38 {
39
40 namespace observers
41 {
42
43 extern bool observer_debug;
44
45 /* An observer is an entity which is interested in being notified
46 when GDB reaches certain states, or certain events occur in GDB.
47 The entity being observed is called the observable. To receive
48 notifications, the observer attaches a callback to the observable.
49 One observable can have several observers.
50
51 The observer implementation is also currently not reentrant. In
52 particular, it is therefore not possible to call the attach or
53 detach routines during a notification. */
54
55 /* The type of a key that can be passed to attach, which can be passed
56 to detach to remove associated observers. Tokens have address
57 identity, and are thus usually const globals. */
58 struct token
59 {
60 token () = default;
61
62 DISABLE_COPY_AND_ASSIGN (token);
63 };
64
65 template<typename... T>
66 class observable
67 {
68 public:
69 typedef std::function<void (T...)> func_type;
70
71 private:
72 struct observer
73 {
74 observer (const struct token *token, func_type func, const char *name)
75 : token (token), func (func), name (name)
76 {}
77
78 const struct token *token;
79 func_type func;
80 const char *name;
81 };
82
83 public:
84 explicit observable (const char *name)
85 : m_name (name)
86 {
87 }
88
89 DISABLE_COPY_AND_ASSIGN (observable);
90
91 /* Attach F as an observer to this observable. F cannot be
92 detached.
93
94 NAME is the name of the observer, used for debug output purposes. Its
95 lifetime must be at least as long as the observer is attached. */
96 void attach (const func_type &f, const char *name)
97 {
98 observer_debug_printf ("Attaching observable %s to observer %s",
99 name, m_name);
100
101 m_observers.emplace_back (nullptr, f, name);
102 }
103
104 /* Attach F as an observer to this observable. T is a reference to
105 a token that can be used to later remove F.
106
107 NAME is the name of the observer, used for debug output purposes. Its
108 lifetime must be at least as long as the observer is attached. */
109 void attach (const func_type &f, const token &t, const char *name)
110 {
111 observer_debug_printf ("Attaching observable %s to observer %s",
112 name, m_name);
113
114 m_observers.emplace_back (&t, f, name);
115 }
116
117 /* Remove observers associated with T from this observable. T is
118 the token that was previously passed to any number of "attach"
119 calls. */
120 void detach (const token &t)
121 {
122 auto iter = std::remove_if (m_observers.begin (),
123 m_observers.end (),
124 [&] (const observer &o)
125 {
126 return o.token == &t;
127 });
128
129 observer_debug_printf ("Detaching observable %s from observer %s",
130 iter->name, m_name);
131
132 m_observers.erase (iter, m_observers.end ());
133 }
134
135 /* Notify all observers that are attached to this observable. */
136 void notify (T... args) const
137 {
138 OBSERVER_SCOPED_DEBUG_START_END ("observable %s notify() called", m_name);
139
140 for (auto &&e : m_observers)
141 {
142 OBSERVER_SCOPED_DEBUG_START_END ("calling observer %s of observable %s",
143 e.name, m_name);
144 e.func (args...);
145 }
146 }
147
148 private:
149
150 std::vector<observer> m_observers;
151 const char *m_name;
152 };
153
154 } /* namespace observers */
155
156 } /* namespace gdb */
157
158 #endif /* COMMON_OBSERVABLE_H */