gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdbsupport / observable.h
1 /* Observers
2
3 Copyright (C) 2016-2020 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 namespace gdb
28 {
29
30 namespace observers
31 {
32
33 extern unsigned int observer_debug;
34
35 /* An observer is an entity which is interested in being notified
36 when GDB reaches certain states, or certain events occur in GDB.
37 The entity being observed is called the observable. To receive
38 notifications, the observer attaches a callback to the observable.
39 One observable can have several observers.
40
41 The observer implementation is also currently not reentrant. In
42 particular, it is therefore not possible to call the attach or
43 detach routines during a notification. */
44
45 /* The type of a key that can be passed to attach, which can be passed
46 to detach to remove associated observers. Tokens have address
47 identity, and are thus usually const globals. */
48 struct token
49 {
50 token () = default;
51
52 DISABLE_COPY_AND_ASSIGN (token);
53 };
54
55 template<typename... T>
56 class observable
57 {
58 public:
59
60 typedef std::function<void (T...)> func_type;
61
62 explicit observable (const char *name)
63 : m_name (name)
64 {
65 }
66
67 DISABLE_COPY_AND_ASSIGN (observable);
68
69 /* Attach F as an observer to this observable. F cannot be
70 detached. */
71 void attach (const func_type &f)
72 {
73 m_observers.emplace_back (nullptr, f);
74 }
75
76 /* Attach F as an observer to this observable. T is a reference to
77 a token that can be used to later remove F. */
78 void attach (const func_type &f, const token &t)
79 {
80 m_observers.emplace_back (&t, f);
81 }
82
83 /* Remove observers associated with T from this observable. T is
84 the token that was previously passed to any number of "attach"
85 calls. */
86 void detach (const token &t)
87 {
88 auto iter = std::remove_if (m_observers.begin (),
89 m_observers.end (),
90 [&] (const std::pair<const token *,
91 func_type> &e)
92 {
93 return e.first == &t;
94 });
95
96 m_observers.erase (iter, m_observers.end ());
97 }
98
99 /* Notify all observers that are attached to this observable. */
100 void notify (T... args) const
101 {
102 if (observer_debug)
103 fprintf_unfiltered (gdb_stdlog, "observable %s notify() called\n",
104 m_name);
105 for (auto &&e : m_observers)
106 e.second (args...);
107 }
108
109 private:
110
111 std::vector<std::pair<const token *, func_type>> m_observers;
112 const char *m_name;
113 };
114
115 } /* namespace observers */
116
117 } /* namespace gdb */
118
119 #endif /* COMMON_OBSERVABLE_H */
This page took 0.032033 seconds and 4 git commands to generate.