From ec098003e27d67bca9e9880320e26ab8ad30fe31 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Sat, 24 Apr 2021 19:26:04 -0400 Subject: gdbsupport: introduce struct observer Instead of using a pair. This allows keeping more data per observer in a structured way, and using field names is clearer than first/second. gdbsupport/ChangeLog: * observable.h (class observable) : New. : Update. : Change type to vector of observers. Change-Id: Iadf7d1fa25049cfb089e6b1b429ddebc548825ab --- gdbsupport/ChangeLog | 6 ++++++ gdbsupport/observable.h | 22 ++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/gdbsupport/ChangeLog b/gdbsupport/ChangeLog index 3ceeb3dab49..bfbd152ae8c 100644 --- a/gdbsupport/ChangeLog +++ b/gdbsupport/ChangeLog @@ -1,3 +1,9 @@ +2021-04-24 Simon Marchi + + * observable.h (class observable) : New. + : Update. + : Change type to vector of observers. + 2021-04-23 Simon Marchi * observable.h (observer_debug): Change to bool. diff --git a/gdbsupport/observable.h b/gdbsupport/observable.h index 0532500dd5f..1d19429ac4f 100644 --- a/gdbsupport/observable.h +++ b/gdbsupport/observable.h @@ -56,9 +56,20 @@ template class observable { public: - typedef std::function func_type; +private: + struct observer + { + observer (const struct token *token, func_type func) + : token (token), func (func) + {} + + const struct token *token; + func_type func; + }; + +public: explicit observable (const char *name) : m_name (name) { @@ -87,10 +98,9 @@ public: { auto iter = std::remove_if (m_observers.begin (), m_observers.end (), - [&] (const std::pair &e) + [&] (const observer &o) { - return e.first == &t; + return o.token == &t; }); m_observers.erase (iter, m_observers.end ()); @@ -103,12 +113,12 @@ public: fprintf_unfiltered (gdb_stdlog, "observable %s notify() called\n", m_name); for (auto &&e : m_observers) - e.second (args...); + e.func (args...); } private: - std::vector> m_observers; + std::vector m_observers; const char *m_name; }; -- cgit v1.2.1