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