Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Xx objects */ |
| 2 | |
| 3 | typedef struct { |
| 4 | OB_HEAD |
| 5 | object *x_attr; /* Attributes dictionary */ |
| 6 | } xxobject; |
| 7 | |
| 8 | extern typeobject Xxtype; /* Really static, forward */ |
| 9 | |
| 10 | static xxobject * |
| 11 | newxxobject(arg) |
| 12 | object *arg; |
| 13 | { |
| 14 | textobject *xp; |
| 15 | xp = NEWOBJ(xxobject, &Xxtype); |
| 16 | if (xp == NULL) |
| 17 | return NULL; |
| 18 | xp->x_attr = NULL; |
| 19 | return xp; |
| 20 | } |
| 21 | |
| 22 | /* Xx methods */ |
| 23 | |
| 24 | static void |
| 25 | xx_dealloc(xp) |
| 26 | xxobject *xp; |
| 27 | { |
| 28 | if (xp->x_attr != NULL) |
| 29 | DECREF(xp->x_attr); |
| 30 | DEL(xp); |
| 31 | } |
| 32 | |
| 33 | static object * |
| 34 | xx_demo(self, args) |
| 35 | xxobject *self; |
| 36 | object *args; |
| 37 | { |
| 38 | if (!getnoarg(args)) |
| 39 | return NULL; |
| 40 | INCREF(None); |
| 41 | return None; |
| 42 | } |
| 43 | |
| 44 | static struct methodlist xx_methods[] = { |
| 45 | "demo", xx_demo, |
| 46 | {NULL, NULL} /* sentinel */ |
| 47 | }; |
| 48 | |
| 49 | static object * |
| 50 | xx_getattr(xp, name) |
| 51 | xxobject *xp; |
| 52 | char *name; |
| 53 | { |
| 54 | if (xp->x_attr != NULL) { |
| 55 | object *v = dictlookup(xp->x_attr, name); |
| 56 | if (v != NULL) { |
| 57 | INCREF(v); |
| 58 | return v; |
| 59 | } |
| 60 | } |
| 61 | return findmethod(xx_methods, (object *)xp, name); |
| 62 | } |
| 63 | |
| 64 | static int |
| 65 | xx_setattr(xp, name, v) |
| 66 | xxobject *xp; |
| 67 | char *name; |
| 68 | object *v; |
| 69 | { |
| 70 | if (xp->x_attr == NULL) { |
| 71 | xp->x_attr = newdictobject(); |
| 72 | if (xp->x_attr == NULL) |
| 73 | return errno; |
| 74 | } |
| 75 | if (v == NULL) |
| 76 | return dictremove(xp->x_attr, name); |
| 77 | else |
| 78 | return dictinsert(xp->x_attr, name, v); |
| 79 | } |
| 80 | |
| 81 | static typeobject Xxtype = { |
| 82 | OB_HEAD_INIT(&Typetype) |
| 83 | 0, /*ob_size*/ |
| 84 | "xx", /*tp_name*/ |
| 85 | sizeof(xxobject), /*tp_size*/ |
| 86 | 0, /*tp_itemsize*/ |
| 87 | /* methods */ |
| 88 | xx_dealloc, /*tp_dealloc*/ |
| 89 | 0, /*tp_print*/ |
| 90 | xx_getattr, /*tp_getattr*/ |
| 91 | xx_setattr, /*tp_setattr*/ |
| 92 | 0, /*tp_compare*/ |
| 93 | 0, /*tp_repr*/ |
| 94 | }; |