1 /* Python interface to inferior thread event registries.
3 Copyright (C) 2009, 2010, 2011 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "py-events.h"
24 static PyTypeObject eventregistry_object_type
;
26 /* Implementation of EventRegistry.connect () -> NULL.
27 Add FUNCTION to the list of listeners. */
30 evregpy_connect (PyObject
*self
, PyObject
*function
)
33 PyObject
*callback_list
= (((eventregistry_object
*) self
)->callbacks
);
35 if (!PyArg_ParseTuple (function
, "O", &func
))
38 if (!PyCallable_Check (func
))
40 PyErr_SetString (PyExc_RuntimeError
, "Function is not callable");
44 if (PyList_Append (callback_list
, func
) < 0)
50 /* Implementation of EventRegistry.disconnect () -> NULL.
51 Remove FUNCTION from the list of listeners. */
54 evregpy_disconnect (PyObject
*self
, PyObject
*function
)
58 PyObject
*callback_list
= (((eventregistry_object
*) self
)->callbacks
);
60 if (!PyArg_ParseTuple (function
, "O", &func
))
63 index
= PySequence_Index (callback_list
, func
);
67 if (PySequence_DelItem (callback_list
, index
) < 0)
73 /* Create a new event registry. This function uses PyObject_New
74 and therefore returns a new reference that callers must handle. */
76 eventregistry_object
*
77 create_eventregistry_object (void)
79 eventregistry_object
*eventregistry_obj
;
81 eventregistry_obj
= PyObject_New (eventregistry_object
,
82 &eventregistry_object_type
);
84 if (!eventregistry_obj
)
87 eventregistry_obj
->callbacks
= PyList_New (0);
88 if (!eventregistry_obj
->callbacks
)
91 return eventregistry_obj
;
95 evregpy_dealloc (PyObject
*self
)
97 Py_XDECREF (((eventregistry_object
*) self
)->callbacks
);
98 self
->ob_type
->tp_free (self
);
101 /* Initialize the Python event registry code. */
104 gdbpy_initialize_eventregistry (void)
106 if (PyType_Ready (&eventregistry_object_type
) < 0)
109 Py_INCREF (&eventregistry_object_type
);
110 PyModule_AddObject (gdb_module
, "EventRegistry",
111 (PyObject
*) &eventregistry_object_type
);
114 /* Retern the number of listeners currently connected to this
118 evregpy_no_listeners_p (eventregistry_object
*registry
)
120 return PyList_Size (registry
->callbacks
) == 0;
123 static PyMethodDef eventregistry_object_methods
[] =
125 { "connect", evregpy_connect
, METH_VARARGS
, "Add function" },
126 { "disconnect", evregpy_disconnect
, METH_VARARGS
, "Remove function" },
127 { NULL
} /* Sentinel. */
130 static PyTypeObject eventregistry_object_type
=
132 PyObject_HEAD_INIT (NULL
)
134 "gdb.EventRegistry", /* tp_name */
135 sizeof (eventregistry_object
), /* tp_basicsize */
137 evregpy_dealloc
, /* tp_dealloc */
143 0, /* tp_as_number */
144 0, /* tp_as_sequence */
145 0, /* tp_as_mapping */
151 0, /* tp_as_buffer */
152 Py_TPFLAGS_DEFAULT
, /* tp_flags */
153 "GDB event registry object", /* tp_doc */
156 0, /* tp_richcompare */
157 0, /* tp_weaklistoffset */
160 eventregistry_object_methods
, /* tp_methods */
165 0, /* tp_descr_get */
166 0, /* tp_descr_set */
167 0, /* tp_dictoffset */