Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / python / py-evtregistry.c
1 /* Python interface to inferior thread event registries.
2
3    Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
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
24 static PyTypeObject eventregistry_object_type;
25
26 /* Implementation of EventRegistry.connect () -> NULL.
27    Add FUNCTION to the list of listeners.  */
28
29 static PyObject *
30 evregpy_connect (PyObject *self, PyObject *function)
31 {
32   PyObject *func;
33   PyObject *callback_list = (((eventregistry_object *) self)->callbacks);
34
35   if (!PyArg_ParseTuple (function, "O", &func))
36     return NULL;
37
38   if (!PyCallable_Check (func))
39     {
40       PyErr_SetString (PyExc_RuntimeError, "Function is not callable");
41       return NULL;
42     }
43
44   if (PyList_Append (callback_list, func) < 0)
45     return NULL;
46
47   Py_RETURN_NONE;
48 }
49
50 /* Implementation of EventRegistry.disconnect () -> NULL.
51    Remove FUNCTION from the list of listeners.  */
52
53 static PyObject *
54 evregpy_disconnect (PyObject *self, PyObject *function)
55 {
56   PyObject *func;
57   int index;
58   PyObject *callback_list = (((eventregistry_object *) self)->callbacks);
59
60   if (!PyArg_ParseTuple (function, "O", &func))
61     return NULL;
62
63   index = PySequence_Index (callback_list, func);
64   if (index < 0)
65     Py_RETURN_NONE;
66
67   if (PySequence_DelItem (callback_list, index) < 0)
68     return NULL;
69
70   Py_RETURN_NONE;
71 }
72
73 /* Create a new event registry.  This function uses PyObject_New
74    and therefore returns a new reference that callers must handle.  */
75
76 eventregistry_object *
77 create_eventregistry_object (void)
78 {
79   eventregistry_object *eventregistry_obj;
80
81   eventregistry_obj = PyObject_New (eventregistry_object,
82                                     &eventregistry_object_type);
83
84   if (!eventregistry_obj)
85     return NULL;
86
87   eventregistry_obj->callbacks = PyList_New (0);
88   if (!eventregistry_obj->callbacks)
89     return NULL;
90
91   return eventregistry_obj;
92 }
93
94 static void
95 evregpy_dealloc (PyObject *self)
96 {
97   Py_XDECREF (((eventregistry_object *) self)->callbacks);
98   self->ob_type->tp_free (self);
99 }
100
101 /* Initialize the Python event registry code.  */
102
103 void
104 gdbpy_initialize_eventregistry (void)
105 {
106   if (PyType_Ready (&eventregistry_object_type) < 0)
107     return;
108
109   Py_INCREF (&eventregistry_object_type);
110   PyModule_AddObject (gdb_module, "EventRegistry",
111                       (PyObject *) &eventregistry_object_type);
112 }
113
114 /* Retern the number of listeners currently connected to this
115    registry.  */
116
117 int
118 evregpy_no_listeners_p (eventregistry_object *registry)
119 {
120   return PyList_Size (registry->callbacks) == 0;
121 }
122
123 static PyMethodDef eventregistry_object_methods[] =
124 {
125   { "connect", evregpy_connect, METH_VARARGS, "Add function" },
126   { "disconnect", evregpy_disconnect, METH_VARARGS, "Remove function" },
127   { NULL } /* Sentinel.  */
128 };
129
130 static PyTypeObject eventregistry_object_type =
131 {
132   PyObject_HEAD_INIT (NULL)
133   0,                                          /* ob_size */
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 };