home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Objects / xxobject.c < prev   
Encoding:
C/C++ Source or Header  |  2000-10-25  |  2.4 KB  |  109 lines

  1.  
  2. /* Use this file as a template to start implementing a new object type.
  3.    If your objects will be called foobar, start by copying this file to
  4.    foobarobject.c, changing all occurrences of xx to foobar and all
  5.    occurrences of Xx by Foobar.  You will probably want to delete all
  6.    references to 'x_attr' and add your own types of attributes
  7.    instead.  Maybe you want to name your local variables other than
  8.    'xp'.  If your object type is needed in other files, you'll have to
  9.    create a file "foobarobject.h"; see intobject.h for an example. */
  10.  
  11.  
  12. /* Xx objects */
  13.  
  14. #include "Python.h"
  15.  
  16. typedef struct {
  17.     PyObject_HEAD
  18.     PyObject    *x_attr;    /* Attributes dictionary */
  19. } xxobject;
  20.  
  21. staticforward PyTypeObject Xxtype;
  22.  
  23. #define is_xxobject(v)        ((v)->ob_type == &Xxtype)
  24.  
  25. static xxobject *
  26. newxxobject(PyObject *arg)
  27. {
  28.     xxobject *xp;
  29.     xp = PyObject_NEW(xxobject, &Xxtype);
  30.     if (xp == NULL)
  31.         return NULL;
  32.     xp->x_attr = NULL;
  33.     return xp;
  34. }
  35.  
  36. /* Xx methods */
  37.  
  38. static void
  39. xx_dealloc(xxobject *xp)
  40. {
  41.     Py_XDECREF(xp->x_attr);
  42.     PyObject_DEL(xp);
  43. }
  44.  
  45. static PyObject *
  46. xx_demo(xxobject *self, PyObject *args)
  47. {
  48.     if (!PyArg_ParseTuple(args, ":demo"))
  49.         return NULL;
  50.     Py_INCREF(Py_None);
  51.     return Py_None;
  52. }
  53.  
  54. static PyMethodDef xx_methods[] = {
  55.     {"demo",    (PyCFunction)xx_demo,    METH_VARARGS},
  56.     {NULL,        NULL}        /* sentinel */
  57. };
  58.  
  59. static PyObject *
  60. xx_getattr(xxobject *xp, char *name)
  61. {
  62.     if (xp->x_attr != NULL) {
  63.         PyObject *v = PyDict_GetItemString(xp->x_attr, name);
  64.         if (v != NULL) {
  65.             Py_INCREF(v);
  66.             return v;
  67.         }
  68.     }
  69.     return Py_FindMethod(xx_methods, (PyObject *)xp, name);
  70. }
  71.  
  72. static int
  73. xx_setattr(xxobject *xp, char *name, PyObject *v)
  74. {
  75.     if (xp->x_attr == NULL) {
  76.         xp->x_attr = PyDict_New();
  77.         if (xp->x_attr == NULL)
  78.             return -1;
  79.     }
  80.     if (v == NULL) {
  81.         int rv = PyDict_DelItemString(xp->x_attr, name);
  82.         if (rv < 0)
  83.             PyErr_SetString(PyExc_AttributeError,
  84.                                         "delete non-existing xx attribute");
  85.         return rv;
  86.     }
  87.     else
  88.         return PyDict_SetItemString(xp->x_attr, name, v);
  89. }
  90.  
  91. static PyTypeObject Xxtype = {
  92.     PyObject_HEAD_INIT(&PyType_Type)
  93.     0,            /*ob_size*/
  94.     "xx",            /*tp_name*/
  95.     sizeof(xxobject),    /*tp_basicsize*/
  96.     0,            /*tp_itemsize*/
  97.     /* methods */
  98.     (destructor)xx_dealloc, /*tp_dealloc*/
  99.     0,            /*tp_print*/
  100.     (getattrfunc)xx_getattr, /*tp_getattr*/
  101.     (setattrfunc)xx_setattr, /*tp_setattr*/
  102.     0,            /*tp_compare*/
  103.     0,            /*tp_repr*/
  104.     0,            /*tp_as_number*/
  105.     0,            /*tp_as_sequence*/
  106.     0,            /*tp_as_mapping*/
  107.     0,            /*tp_hash*/
  108. };
  109.