observable.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Observers
  2. Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_OBSERVABLE_H
  15. #define COMMON_OBSERVABLE_H
  16. #include <algorithm>
  17. #include <functional>
  18. #include <vector>
  19. /* Print an "observer" debug statement. */
  20. #define observer_debug_printf(fmt, ...) \
  21. debug_prefixed_printf_cond (observer_debug, "observer", fmt, ##__VA_ARGS__)
  22. /* Print "observer" start/end debug statements. */
  23. #define OBSERVER_SCOPED_DEBUG_START_END(fmt, ...) \
  24. scoped_debug_start_end (observer_debug, "observer", fmt, ##__VA_ARGS__)
  25. namespace gdb
  26. {
  27. namespace observers
  28. {
  29. extern bool observer_debug;
  30. /* An observer is an entity which is interested in being notified
  31. when GDB reaches certain states, or certain events occur in GDB.
  32. The entity being observed is called the observable. To receive
  33. notifications, the observer attaches a callback to the observable.
  34. One observable can have several observers.
  35. The observer implementation is also currently not reentrant. In
  36. particular, it is therefore not possible to call the attach or
  37. detach routines during a notification. */
  38. /* The type of a key that can be passed to attach, which can be passed
  39. to detach to remove associated observers. Tokens have address
  40. identity, and are thus usually const globals. */
  41. struct token
  42. {
  43. token () = default;
  44. DISABLE_COPY_AND_ASSIGN (token);
  45. };
  46. template<typename... T>
  47. class observable
  48. {
  49. public:
  50. typedef std::function<void (T...)> func_type;
  51. private:
  52. struct observer
  53. {
  54. observer (const struct token *token, func_type func, const char *name,
  55. const std::vector<const struct token *> &dependencies)
  56. : token (token), func (func), name (name), dependencies (dependencies)
  57. {}
  58. const struct token *token;
  59. func_type func;
  60. const char *name;
  61. std::vector<const struct token *> dependencies;
  62. };
  63. public:
  64. explicit observable (const char *name)
  65. : m_name (name)
  66. {
  67. }
  68. DISABLE_COPY_AND_ASSIGN (observable);
  69. /* Attach F as an observer to this observable. F cannot be detached or
  70. specified as a dependency.
  71. DEPENDENCIES is a list of tokens of observers to be notified before this
  72. one.
  73. NAME is the name of the observer, used for debug output purposes. Its
  74. lifetime must be at least as long as the observer is attached. */
  75. void attach (const func_type &f, const char *name,
  76. const std::vector<const struct token *> &dependencies = {})
  77. {
  78. attach (f, nullptr, name, dependencies);
  79. }
  80. /* Attach F as an observer to this observable.
  81. T is a reference to a token that can be used to later remove F or specify F
  82. as a dependency of another observer.
  83. DEPENDENCIES is a list of tokens of observers to be notified before this
  84. one.
  85. NAME is the name of the observer, used for debug output purposes. Its
  86. lifetime must be at least as long as the observer is attached. */
  87. void attach (const func_type &f, const token &t, const char *name,
  88. const std::vector<const struct token *> &dependencies = {})
  89. {
  90. attach (f, &t, name, dependencies);
  91. }
  92. /* Remove observers associated with T from this observable. T is
  93. the token that was previously passed to any number of "attach"
  94. calls. */
  95. void detach (const token &t)
  96. {
  97. auto iter = std::remove_if (m_observers.begin (),
  98. m_observers.end (),
  99. [&] (const observer &o)
  100. {
  101. return o.token == &t;
  102. });
  103. observer_debug_printf ("Detaching observable %s from observer %s",
  104. iter->name, m_name);
  105. m_observers.erase (iter, m_observers.end ());
  106. }
  107. /* Notify all observers that are attached to this observable. */
  108. void notify (T... args) const
  109. {
  110. OBSERVER_SCOPED_DEBUG_START_END ("observable %s notify() called", m_name);
  111. for (auto &&e : m_observers)
  112. {
  113. OBSERVER_SCOPED_DEBUG_START_END ("calling observer %s of observable %s",
  114. e.name, m_name);
  115. e.func (args...);
  116. }
  117. }
  118. private:
  119. std::vector<observer> m_observers;
  120. const char *m_name;
  121. /* Use for sorting algorithm, to indicate which observer we have visited. */
  122. enum class visit_state
  123. {
  124. NOT_VISITED,
  125. VISITING,
  126. VISITED,
  127. };
  128. /* Helper method for topological sort using depth-first search algorithm.
  129. Visit all dependencies of observer at INDEX in M_OBSERVERS (later referred
  130. to as "the observer"). Then append the observer to SORTED_OBSERVERS.
  131. If the observer is already visited, do nothing. */
  132. void visit_for_sorting (std::vector<observer> &sorted_observers,
  133. std::vector<visit_state> &visit_states, int index)
  134. {
  135. if (visit_states[index] == visit_state::VISITED)
  136. return;
  137. /* If we are already visiting this observer, it means there's a cycle. */
  138. gdb_assert (visit_states[index] != visit_state::VISITING);
  139. visit_states[index] = visit_state::VISITING;
  140. /* For each dependency of this observer... */
  141. for (const token *dep : m_observers[index].dependencies)
  142. {
  143. /* ... find the observer that has token DEP. If found, visit it. */
  144. auto it_dep
  145. = std::find_if (m_observers.begin (), m_observers.end (),
  146. [&] (observer o) { return o.token == dep; });
  147. if (it_dep != m_observers.end ())
  148. {
  149. int i = std::distance (m_observers.begin (), it_dep);
  150. visit_for_sorting (sorted_observers, visit_states, i);
  151. }
  152. }
  153. visit_states[index] = visit_state::VISITED;
  154. sorted_observers.push_back (m_observers[index]);
  155. }
  156. /* Sort the observers, so that dependencies come before observers
  157. depending on them.
  158. Uses depth-first search algorithm for topological sorting, see
  159. https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search . */
  160. void sort_observers ()
  161. {
  162. std::vector<observer> sorted_observers;
  163. std::vector<visit_state> visit_states (m_observers.size (),
  164. visit_state::NOT_VISITED);
  165. for (size_t i = 0; i < m_observers.size (); i++)
  166. visit_for_sorting (sorted_observers, visit_states, i);
  167. m_observers = std::move (sorted_observers);
  168. }
  169. void attach (const func_type &f, const token *t, const char *name,
  170. const std::vector<const struct token *> &dependencies)
  171. {
  172. observer_debug_printf ("Attaching observable %s to observer %s",
  173. name, m_name);
  174. m_observers.emplace_back (t, f, name, dependencies);
  175. /* The observer has been inserted at the end of the vector, so it will be
  176. after any of its potential dependencies attached earlier. If the
  177. observer has a token, it means that other observers can specify it as
  178. a dependency, so sorting is necessary to ensure those will be after the
  179. newly inserted observer afterwards. */
  180. if (t != nullptr)
  181. sort_observers ();
  182. };
  183. };
  184. } /* namespace observers */
  185. } /* namespace gdb */
  186. #endif /* COMMON_OBSERVABLE_H */