Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 25 | /* Use this file as a template to start implementing a new object type. |
| 26 | If your objects will be called foobar, start by copying this file to |
| 27 | foobarobject.c, changing all occurrences of xx to foobar and all |
| 28 | occurrences of Xx by Foobar. You will probably want to delete all |
| 29 | references to 'x_attr' and add your own types of attributes |
| 30 | instead. Maybe you want to name your local variables other than |
| 31 | 'xp'. If your object type is needed in other files, you'll have to |
| 32 | create a file "foobarobject.h"; see intobject.h for an example. */ |
| 33 | |
| 34 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | /* Xx objects */ |
| 36 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 37 | #include "allobjects.h" |
Guido van Rossum | 5dc8eb0 | 1992-06-19 13:57:27 +0000 | [diff] [blame] | 38 | #include "modsupport.h" /* For getargs() etc. */ |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | typedef struct { |
| 41 | OB_HEAD |
| 42 | object *x_attr; /* Attributes dictionary */ |
| 43 | } xxobject; |
| 44 | |
| 45 | extern typeobject Xxtype; /* Really static, forward */ |
| 46 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 47 | #define is_xxobject(v) ((v)->ob_type == &Xxtype) |
| 48 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | static xxobject * |
| 50 | newxxobject(arg) |
| 51 | object *arg; |
| 52 | { |
Guido van Rossum | 94726d5 | 1991-01-02 15:12:51 +0000 | [diff] [blame] | 53 | xxobject *xp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | xp = NEWOBJ(xxobject, &Xxtype); |
| 55 | if (xp == NULL) |
| 56 | return NULL; |
| 57 | xp->x_attr = NULL; |
| 58 | return xp; |
| 59 | } |
| 60 | |
| 61 | /* Xx methods */ |
| 62 | |
| 63 | static void |
| 64 | xx_dealloc(xp) |
| 65 | xxobject *xp; |
| 66 | { |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 67 | XDECREF(xp->x_attr); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | DEL(xp); |
| 69 | } |
| 70 | |
| 71 | static object * |
| 72 | xx_demo(self, args) |
| 73 | xxobject *self; |
| 74 | object *args; |
| 75 | { |
| 76 | if (!getnoarg(args)) |
| 77 | return NULL; |
| 78 | INCREF(None); |
| 79 | return None; |
| 80 | } |
| 81 | |
| 82 | static struct methodlist xx_methods[] = { |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 83 | {"demo", xx_demo}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | {NULL, NULL} /* sentinel */ |
| 85 | }; |
| 86 | |
| 87 | static object * |
| 88 | xx_getattr(xp, name) |
| 89 | xxobject *xp; |
| 90 | char *name; |
| 91 | { |
| 92 | if (xp->x_attr != NULL) { |
| 93 | object *v = dictlookup(xp->x_attr, name); |
| 94 | if (v != NULL) { |
| 95 | INCREF(v); |
| 96 | return v; |
| 97 | } |
| 98 | } |
| 99 | return findmethod(xx_methods, (object *)xp, name); |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | xx_setattr(xp, name, v) |
| 104 | xxobject *xp; |
| 105 | char *name; |
| 106 | object *v; |
| 107 | { |
| 108 | if (xp->x_attr == NULL) { |
| 109 | xp->x_attr = newdictobject(); |
| 110 | if (xp->x_attr == NULL) |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 111 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 113 | if (v == NULL) { |
| 114 | int rv = dictremove(xp->x_attr, name); |
| 115 | if (rv < 0) |
| 116 | err_setstr(AttributeError, |
| 117 | "delete non-existing xx attribute"); |
| 118 | return rv; |
| 119 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | else |
| 121 | return dictinsert(xp->x_attr, name, v); |
| 122 | } |
| 123 | |
| 124 | static typeobject Xxtype = { |
| 125 | OB_HEAD_INIT(&Typetype) |
| 126 | 0, /*ob_size*/ |
| 127 | "xx", /*tp_name*/ |
| 128 | sizeof(xxobject), /*tp_size*/ |
| 129 | 0, /*tp_itemsize*/ |
| 130 | /* methods */ |
Guido van Rossum | 70d7a31 | 1992-08-14 12:04:19 +0000 | [diff] [blame] | 131 | xx_dealloc, /*tp_dealloc*/ |
| 132 | 0, /*tp_print*/ |
| 133 | xx_getattr, /*tp_getattr*/ |
| 134 | xx_setattr, /*tp_setattr*/ |
| 135 | 0, /*tp_compare*/ |
| 136 | 0, /*tp_repr*/ |
| 137 | 0, /*tp_as_number*/ |
| 138 | 0, /*tp_as_sequence*/ |
| 139 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 140 | 0, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 141 | }; |