Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame^] | 1 | /* Use this file as a template to start implementing a new object type. |
| 2 | If your objects will be called foobar, start by copying this file to |
| 3 | foobarobject.c, changing all occurrences of xx to foobar and all |
| 4 | occurrences of Xx by Foobar. You will probably want to delete all |
| 5 | references to 'x_attr' and add your own types of attributes |
| 6 | instead. Maybe you want to name your local variables other than |
| 7 | 'xp'. If your object type is needed in other files, you'll have to |
| 8 | create a file "foobarobject.h"; see intobject.h for an example. */ |
| 9 | |
| 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* Xx objects */ |
| 12 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame^] | 13 | #include "allobjects.h" |
| 14 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 15 | typedef struct { |
| 16 | OB_HEAD |
| 17 | object *x_attr; /* Attributes dictionary */ |
| 18 | } xxobject; |
| 19 | |
| 20 | extern typeobject Xxtype; /* Really static, forward */ |
| 21 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame^] | 22 | #define is_xxobject(v) ((v)->ob_type == &Xxtype) |
| 23 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | static xxobject * |
| 25 | newxxobject(arg) |
| 26 | object *arg; |
| 27 | { |
| 28 | textobject *xp; |
| 29 | xp = NEWOBJ(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(xp) |
| 40 | xxobject *xp; |
| 41 | { |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame^] | 42 | XDECREF(xp->x_attr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | DEL(xp); |
| 44 | } |
| 45 | |
| 46 | static object * |
| 47 | xx_demo(self, args) |
| 48 | xxobject *self; |
| 49 | object *args; |
| 50 | { |
| 51 | if (!getnoarg(args)) |
| 52 | return NULL; |
| 53 | INCREF(None); |
| 54 | return None; |
| 55 | } |
| 56 | |
| 57 | static struct methodlist xx_methods[] = { |
| 58 | "demo", xx_demo, |
| 59 | {NULL, NULL} /* sentinel */ |
| 60 | }; |
| 61 | |
| 62 | static object * |
| 63 | xx_getattr(xp, name) |
| 64 | xxobject *xp; |
| 65 | char *name; |
| 66 | { |
| 67 | if (xp->x_attr != NULL) { |
| 68 | object *v = dictlookup(xp->x_attr, name); |
| 69 | if (v != NULL) { |
| 70 | INCREF(v); |
| 71 | return v; |
| 72 | } |
| 73 | } |
| 74 | return findmethod(xx_methods, (object *)xp, name); |
| 75 | } |
| 76 | |
| 77 | static int |
| 78 | xx_setattr(xp, name, v) |
| 79 | xxobject *xp; |
| 80 | char *name; |
| 81 | object *v; |
| 82 | { |
| 83 | if (xp->x_attr == NULL) { |
| 84 | xp->x_attr = newdictobject(); |
| 85 | if (xp->x_attr == NULL) |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame^] | 86 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | } |
| 88 | if (v == NULL) |
| 89 | return dictremove(xp->x_attr, name); |
| 90 | else |
| 91 | return dictinsert(xp->x_attr, name, v); |
| 92 | } |
| 93 | |
| 94 | static typeobject Xxtype = { |
| 95 | OB_HEAD_INIT(&Typetype) |
| 96 | 0, /*ob_size*/ |
| 97 | "xx", /*tp_name*/ |
| 98 | sizeof(xxobject), /*tp_size*/ |
| 99 | 0, /*tp_itemsize*/ |
| 100 | /* methods */ |
| 101 | xx_dealloc, /*tp_dealloc*/ |
| 102 | 0, /*tp_print*/ |
| 103 | xx_getattr, /*tp_getattr*/ |
| 104 | xx_setattr, /*tp_setattr*/ |
| 105 | 0, /*tp_compare*/ |
| 106 | 0, /*tp_repr*/ |
| 107 | }; |