]>
Commit | Line | Data |
---|---|---|
c17a9e46 HZ |
1 | /* Python interface to inferior thread event registries. |
2 | ||
0b302171 | 3 | Copyright (C) 2009-2012 Free Software Foundation, Inc. |
c17a9e46 HZ |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "command.h" | |
22 | #include "py-events.h" | |
23 | ||
eed8f803 TG |
24 | events_object gdb_py_events; |
25 | ||
c17a9e46 HZ |
26 | static PyTypeObject eventregistry_object_type; |
27 | ||
28 | /* Implementation of EventRegistry.connect () -> NULL. | |
29 | Add FUNCTION to the list of listeners. */ | |
30 | ||
31 | static PyObject * | |
32 | evregpy_connect (PyObject *self, PyObject *function) | |
33 | { | |
34 | PyObject *func; | |
35 | PyObject *callback_list = (((eventregistry_object *) self)->callbacks); | |
36 | ||
37 | if (!PyArg_ParseTuple (function, "O", &func)) | |
38 | return NULL; | |
39 | ||
40 | if (!PyCallable_Check (func)) | |
41 | { | |
42 | PyErr_SetString (PyExc_RuntimeError, "Function is not callable"); | |
43 | return NULL; | |
44 | } | |
45 | ||
46 | if (PyList_Append (callback_list, func) < 0) | |
47 | return NULL; | |
48 | ||
49 | Py_RETURN_NONE; | |
50 | } | |
51 | ||
52 | /* Implementation of EventRegistry.disconnect () -> NULL. | |
53 | Remove FUNCTION from the list of listeners. */ | |
54 | ||
55 | static PyObject * | |
56 | evregpy_disconnect (PyObject *self, PyObject *function) | |
57 | { | |
58 | PyObject *func; | |
59 | int index; | |
60 | PyObject *callback_list = (((eventregistry_object *) self)->callbacks); | |
61 | ||
62 | if (!PyArg_ParseTuple (function, "O", &func)) | |
63 | return NULL; | |
64 | ||
65 | index = PySequence_Index (callback_list, func); | |
66 | if (index < 0) | |
67 | Py_RETURN_NONE; | |
68 | ||
69 | if (PySequence_DelItem (callback_list, index) < 0) | |
70 | return NULL; | |
71 | ||
72 | Py_RETURN_NONE; | |
73 | } | |
74 | ||
75 | /* Create a new event registry. This function uses PyObject_New | |
76 | and therefore returns a new reference that callers must handle. */ | |
77 | ||
78 | eventregistry_object * | |
79 | create_eventregistry_object (void) | |
80 | { | |
81 | eventregistry_object *eventregistry_obj; | |
82 | ||
83 | eventregistry_obj = PyObject_New (eventregistry_object, | |
84 | &eventregistry_object_type); | |
85 | ||
86 | if (!eventregistry_obj) | |
87 | return NULL; | |
88 | ||
89 | eventregistry_obj->callbacks = PyList_New (0); | |
90 | if (!eventregistry_obj->callbacks) | |
91 | return NULL; | |
92 | ||
93 | return eventregistry_obj; | |
94 | } | |
95 | ||
96 | static void | |
97 | evregpy_dealloc (PyObject *self) | |
98 | { | |
99 | Py_XDECREF (((eventregistry_object *) self)->callbacks); | |
100 | self->ob_type->tp_free (self); | |
101 | } | |
102 | ||
103 | /* Initialize the Python event registry code. */ | |
104 | ||
105 | void | |
106 | gdbpy_initialize_eventregistry (void) | |
107 | { | |
108 | if (PyType_Ready (&eventregistry_object_type) < 0) | |
109 | return; | |
110 | ||
111 | Py_INCREF (&eventregistry_object_type); | |
112 | PyModule_AddObject (gdb_module, "EventRegistry", | |
113 | (PyObject *) &eventregistry_object_type); | |
114 | } | |
115 | ||
116 | /* Retern the number of listeners currently connected to this | |
117 | registry. */ | |
118 | ||
119 | int | |
120 | evregpy_no_listeners_p (eventregistry_object *registry) | |
121 | { | |
122 | return PyList_Size (registry->callbacks) == 0; | |
123 | } | |
124 | ||
125 | static PyMethodDef eventregistry_object_methods[] = | |
126 | { | |
127 | { "connect", evregpy_connect, METH_VARARGS, "Add function" }, | |
128 | { "disconnect", evregpy_disconnect, METH_VARARGS, "Remove function" }, | |
129 | { NULL } /* Sentinel. */ | |
130 | }; | |
131 | ||
132 | static PyTypeObject eventregistry_object_type = | |
133 | { | |
134 | PyObject_HEAD_INIT (NULL) | |
135 | 0, /* ob_size */ | |
136 | "gdb.EventRegistry", /* tp_name */ | |
137 | sizeof (eventregistry_object), /* tp_basicsize */ | |
138 | 0, /* tp_itemsize */ | |
139 | evregpy_dealloc, /* tp_dealloc */ | |
140 | 0, /* tp_print */ | |
141 | 0, /* tp_getattr */ | |
142 | 0, /* tp_setattr */ | |
143 | 0, /* tp_compare */ | |
144 | 0, /* tp_repr */ | |
145 | 0, /* tp_as_number */ | |
146 | 0, /* tp_as_sequence */ | |
147 | 0, /* tp_as_mapping */ | |
148 | 0, /* tp_hash */ | |
149 | 0, /* tp_call */ | |
150 | 0, /* tp_str */ | |
151 | 0, /* tp_getattro */ | |
152 | 0, /* tp_setattro */ | |
153 | 0, /* tp_as_buffer */ | |
154 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
155 | "GDB event registry object", /* tp_doc */ | |
156 | 0, /* tp_traverse */ | |
157 | 0, /* tp_clear */ | |
158 | 0, /* tp_richcompare */ | |
159 | 0, /* tp_weaklistoffset */ | |
160 | 0, /* tp_iter */ | |
161 | 0, /* tp_iternext */ | |
162 | eventregistry_object_methods, /* tp_methods */ | |
163 | 0, /* tp_members */ | |
164 | 0, /* tp_getset */ | |
165 | 0, /* tp_base */ | |
166 | 0, /* tp_dict */ | |
167 | 0, /* tp_descr_get */ | |
168 | 0, /* tp_descr_set */ | |
169 | 0, /* tp_dictoffset */ | |
170 | 0, /* tp_init */ | |
171 | 0 /* tp_alloc */ | |
172 | }; |