1d19429ac4f5a8d89e6021a4a63a56adc4fcc04e
3 Copyright (C) 2016-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef COMMON_OBSERVABLE_H
21 #define COMMON_OBSERVABLE_H
33 extern bool observer_debug
;
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.
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. */
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. */
52 DISABLE_COPY_AND_ASSIGN (token
);
55 template<typename
... T
>
59 typedef std::function
<void (T
...)> func_type
;
64 observer (const struct token
*token
, func_type func
)
65 : token (token
), func (func
)
68 const struct token
*token
;
73 explicit observable (const char *name
)
78 DISABLE_COPY_AND_ASSIGN (observable
);
80 /* Attach F as an observer to this observable. F cannot be
82 void attach (const func_type
&f
)
84 m_observers
.emplace_back (nullptr, f
);
87 /* Attach F as an observer to this observable. T is a reference to
88 a token that can be used to later remove F. */
89 void attach (const func_type
&f
, const token
&t
)
91 m_observers
.emplace_back (&t
, f
);
94 /* Remove observers associated with T from this observable. T is
95 the token that was previously passed to any number of "attach"
97 void detach (const token
&t
)
99 auto iter
= std::remove_if (m_observers
.begin (),
101 [&] (const observer
&o
)
103 return o
.token
== &t
;
106 m_observers
.erase (iter
, m_observers
.end ());
109 /* Notify all observers that are attached to this observable. */
110 void notify (T
... args
) const
113 fprintf_unfiltered (gdb_stdlog
, "observable %s notify() called\n",
115 for (auto &&e
: m_observers
)
121 std::vector
<observer
> m_observers
;
125 } /* namespace observers */
127 } /* namespace gdb */
129 #endif /* COMMON_OBSERVABLE_H */