[1/4] gdbsupport: introduce struct observer

Message ID 20210424021006.22779-2-simon.marchi@polymtl.ca
State New
Headers show
Series
  • Improvements to observer debug output
Related show

Commit Message

Eli Zaretskii via Gdb-patches April 24, 2021, 2:10 a.m.
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) <struct observer>: New.
	<detach, notify>: Update.
	<m_observers>: Change type to vector of observers.

Change-Id: Iadf7d1fa25049cfb089e6b1b429ddebc548825ab
---
 gdbsupport/observable.h | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

-- 
2.30.1

Patch

diff --git a/gdbsupport/observable.h b/gdbsupport/observable.h
index 0532500dd5fa..1d19429ac4f5 100644
--- a/gdbsupport/observable.h
+++ b/gdbsupport/observable.h
@@ -56,9 +56,20 @@  template<typename... T>
 class observable
 {
 public:
-
   typedef std::function<void (T...)> 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 @@  class observable
   {
     auto iter = std::remove_if (m_observers.begin (),
 				m_observers.end (),
-				[&] (const std::pair<const token *,
-				     func_type> &e)
+				[&] (const observer &o)
 				{
-				  return e.first == &t;
+				  return o.token == &t;
 				});
 
     m_observers.erase (iter, m_observers.end ());
@@ -103,12 +113,12 @@  class observable
       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<std::pair<const token *, func_type>> m_observers;
+  std::vector<observer> m_observers;
   const char *m_name;
 };