GDB (API)
/home/stan/gdb/src/gdb/python/py-evtregistry.c
Go to the documentation of this file.
00001 /* Python interface to inferior thread event registries.
00002 
00003    Copyright (C) 2009-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "command.h"
00022 #include "py-events.h"
00023 
00024 events_object gdb_py_events;
00025 
00026 static PyTypeObject eventregistry_object_type
00027     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("eventregistry_object");
00028 
00029 /* Implementation of EventRegistry.connect () -> NULL.
00030    Add FUNCTION to the list of listeners.  */
00031 
00032 static PyObject *
00033 evregpy_connect (PyObject *self, PyObject *function)
00034 {
00035   PyObject *func;
00036   PyObject *callback_list = (((eventregistry_object *) self)->callbacks);
00037 
00038   if (!PyArg_ParseTuple (function, "O", &func))
00039     return NULL;
00040 
00041   if (!PyCallable_Check (func))
00042     {
00043       PyErr_SetString (PyExc_RuntimeError, "Function is not callable");
00044       return NULL;
00045     }
00046 
00047   if (PyList_Append (callback_list, func) < 0)
00048     return NULL;
00049 
00050   Py_RETURN_NONE;
00051 }
00052 
00053 /* Implementation of EventRegistry.disconnect () -> NULL.
00054    Remove FUNCTION from the list of listeners.  */
00055 
00056 static PyObject *
00057 evregpy_disconnect (PyObject *self, PyObject *function)
00058 {
00059   PyObject *func;
00060   int index;
00061   PyObject *callback_list = (((eventregistry_object *) self)->callbacks);
00062 
00063   if (!PyArg_ParseTuple (function, "O", &func))
00064     return NULL;
00065 
00066   index = PySequence_Index (callback_list, func);
00067   if (index < 0)
00068     Py_RETURN_NONE;
00069 
00070   if (PySequence_DelItem (callback_list, index) < 0)
00071     return NULL;
00072 
00073   Py_RETURN_NONE;
00074 }
00075 
00076 /* Create a new event registry.  This function uses PyObject_New
00077    and therefore returns a new reference that callers must handle.  */
00078 
00079 eventregistry_object *
00080 create_eventregistry_object (void)
00081 {
00082   eventregistry_object *eventregistry_obj;
00083 
00084   eventregistry_obj = PyObject_New (eventregistry_object,
00085                                     &eventregistry_object_type);
00086 
00087   if (!eventregistry_obj)
00088     return NULL;
00089 
00090   eventregistry_obj->callbacks = PyList_New (0);
00091   if (!eventregistry_obj->callbacks)
00092     {
00093       Py_DECREF (eventregistry_obj);
00094       return NULL;
00095     }
00096 
00097   return eventregistry_obj;
00098 }
00099 
00100 static void
00101 evregpy_dealloc (PyObject *self)
00102 {
00103   Py_XDECREF (((eventregistry_object *) self)->callbacks);
00104   Py_TYPE (self)->tp_free (self);
00105 }
00106 
00107 /* Initialize the Python event registry code.  */
00108 
00109 int
00110 gdbpy_initialize_eventregistry (void)
00111 {
00112   if (PyType_Ready (&eventregistry_object_type) < 0)
00113     return -1;
00114 
00115   return gdb_pymodule_addobject (gdb_module, "EventRegistry",
00116                                  (PyObject *) &eventregistry_object_type);
00117 }
00118 
00119 /* Retern the number of listeners currently connected to this
00120    registry.  */
00121 
00122 int
00123 evregpy_no_listeners_p (eventregistry_object *registry)
00124 {
00125   return PyList_Size (registry->callbacks) == 0;
00126 }
00127 
00128 static PyMethodDef eventregistry_object_methods[] =
00129 {
00130   { "connect", evregpy_connect, METH_VARARGS, "Add function" },
00131   { "disconnect", evregpy_disconnect, METH_VARARGS, "Remove function" },
00132   { NULL } /* Sentinel.  */
00133 };
00134 
00135 static PyTypeObject eventregistry_object_type =
00136 {
00137   PyVarObject_HEAD_INIT (NULL, 0)
00138   "gdb.EventRegistry",                        /* tp_name */
00139   sizeof (eventregistry_object),              /* tp_basicsize */
00140   0,                                          /* tp_itemsize */
00141   evregpy_dealloc,                            /* tp_dealloc */
00142   0,                                          /* tp_print */
00143   0,                                          /* tp_getattr */
00144   0,                                          /* tp_setattr */
00145   0,                                          /* tp_compare */
00146   0,                                          /* tp_repr */
00147   0,                                          /* tp_as_number */
00148   0,                                          /* tp_as_sequence */
00149   0,                                          /* tp_as_mapping */
00150   0,                                          /* tp_hash  */
00151   0,                                          /* tp_call */
00152   0,                                          /* tp_str */
00153   0,                                          /* tp_getattro */
00154   0,                                          /* tp_setattro */
00155   0,                                          /* tp_as_buffer */
00156   Py_TPFLAGS_DEFAULT,                         /* tp_flags */
00157   "GDB event registry object",                /* tp_doc */
00158   0,                                          /* tp_traverse */
00159   0,                                          /* tp_clear */
00160   0,                                          /* tp_richcompare */
00161   0,                                          /* tp_weaklistoffset */
00162   0,                                          /* tp_iter */
00163   0,                                          /* tp_iternext */
00164   eventregistry_object_methods,               /* tp_methods */
00165   0,                                          /* tp_members */
00166   0,                                          /* tp_getset */
00167   0,                                          /* tp_base */
00168   0,                                          /* tp_dict */
00169   0,                                          /* tp_descr_get */
00170   0,                                          /* tp_descr_set */
00171   0,                                          /* tp_dictoffset */
00172   0,                                          /* tp_init */
00173   0                                           /* tp_alloc */
00174 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines