1 /* GDB Notifications to Observers.
3 Copyright (C) 2003, 2004 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* An observer is an entity who is interested in being notified when GDB
23 reaches certain states, or certain events occur in GDB. The entity being
24 observed is called the Subject. To receive notifications, the observer
25 attaches a callback to the subject. One subject can have several
28 This file implements an internal generic low-level event notification
29 mechanism based on the Observer paradigm described in the book "Design
30 Patterns". This generic event notification mechansim is then re-used
31 to implement the exported high-level notification management routines
32 for all possible notifications.
34 The current implementation of the generic observer provides support
35 for contextual data. This contextual data is given to the subject
36 when attaching the callback. In return, the subject will provide
37 this contextual data back to the observer as a parameter of the
40 FIXME: The current support for the contextual data is only partial,
41 as it lacks a mechanism that would deallocate this data when the
42 callback is detached. This is not a problem so far, as this contextual
43 data is only used internally to hold a function pointer. Later on,
44 if a certain observer needs to provide support for user-level
45 contextual data, then the generic notification mechanism will need
46 need to be enhanced to allow the observer to provide a routine to
47 deallocate the data when attaching the callback.
49 This file is currently maintained by hand, but the long term plan
50 if the number of different notifications starts growing is to create
51 a new script (observer.sh) that would generate this file, and the
52 associated documentation. */
59 static int observer_debug;
61 show_observer_debug (struct ui_file *file, int from_tty,
62 struct cmd_list_element *c, const char *value)
64 fprintf_filtered (file, _("Observer debugging is %s.\n"), value);
67 /* The internal generic observer. */
69 typedef void (generic_observer_notification_ftype) (const void *data,
74 generic_observer_notification_ftype *notify;
75 /* No memory management needed for the following field for now. */
79 /* A list of observers, maintained by the subject. A subject is
80 actually represented by its list of observers. */
84 struct observer_list *next;
85 struct observer *observer;
88 /* Allocate a struct observer_list, intended to be used as a node
89 in the list of observers maintained by a subject. */
91 static struct observer_list *
92 xalloc_observer_list_node (void)
94 struct observer_list *node = XMALLOC (struct observer_list);
95 node->observer = XMALLOC (struct observer);
99 /* The opposite of xalloc_observer_list_node, frees the memory for
103 xfree_observer_list_node (struct observer_list *node)
105 xfree (node->observer);
109 /* Attach the callback NOTIFY to a SUBJECT. The DATA is also stored,
110 in order for the subject to provide it back to the observer during
113 static struct observer *
114 generic_observer_attach (struct observer_list **subject,
115 generic_observer_notification_ftype * notify,
118 struct observer_list *observer_list = xalloc_observer_list_node ();
120 observer_list->next = *subject;
121 observer_list->observer->notify = notify;
122 observer_list->observer->data = data;
123 *subject = observer_list;
125 return observer_list->observer;
128 /* Remove the given OBSERVER from the SUBJECT. Once detached, OBSERVER
129 should no longer be used, as it is no longer valid. */
132 generic_observer_detach (struct observer_list **subject,
133 const struct observer *observer)
135 struct observer_list *previous_node = NULL;
136 struct observer_list *current_node = *subject;
138 while (current_node != NULL)
140 if (current_node->observer == observer)
142 if (previous_node != NULL)
143 previous_node->next = current_node->next;
145 *subject = current_node->next;
146 xfree_observer_list_node (current_node);
149 previous_node = current_node;
150 current_node = current_node->next;
153 /* We should never reach this point. However, this should not be
154 a very serious error, so simply report a warning to the user. */
155 warning (_("Failed to detach observer"));
158 /* Send a notification to all the observers of SUBJECT. ARGS is passed to
159 all observers as an argument to the notification callback. */
162 generic_observer_notify (struct observer_list *subject, const void *args)
164 struct observer_list *current_node = subject;
166 while (current_node != NULL)
168 (*current_node->observer->notify) (current_node->observer->data, args);
169 current_node = current_node->next;
174 /* The following code is only used to unit-test the observers from our
175 testsuite. DO NOT USE IT within observer.c (or anywhere else for
178 /* If we define these variables and functions as `static', the
179 compiler will optimize them out. */
181 int observer_test_first_observer = 0;
182 int observer_test_second_observer = 0;
183 int observer_test_third_observer = 0;
186 observer_test_first_notification_function (struct bpstats *bs)
188 observer_test_first_observer++;
192 observer_test_second_notification_function (struct bpstats *bs)
194 observer_test_second_observer++;
198 observer_test_third_notification_function (struct bpstats *bs)
200 observer_test_third_observer++;
203 extern initialize_file_ftype _initialize_observer; /* -Wmissing-prototypes */
206 _initialize_observer (void)
208 add_setshow_zinteger_cmd ("observer", class_maintenance,
209 &observer_debug, _("\
210 Set observer debugging."), _("\
211 Show observer debugging."), _("\
212 When non-zero, observer debugging is enabled."),
215 &setdebuglist, &showdebuglist);
218 #include "observer.inc"