Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +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 | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 25 | /* Use this file as a template to start implementing a module that |
| 26 | also declares objects types. All occurrences of 'xxo' should be changed |
| 27 | to something reasonable for your objects. After that, all other |
| 28 | occurrences of 'xx' should be changed to something reasonable for your |
| 29 | module. If your module is named foo your sourcefile should be named |
| 30 | foomodule.c. |
| 31 | |
| 32 | You will probably want to delete all references to 'x_attr' and add |
| 33 | your own types of attributes instead. Maybe you want to name your |
| 34 | local variables other than 'self'. If your object type is needed in |
| 35 | other files, you'll have to create a file "foobarobject.h"; see |
| 36 | intobject.h for an example. */ |
| 37 | |
| 38 | /* Xxo objects */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 39 | |
| 40 | #include "allobjects.h" |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 41 | #include "modsupport.h" /* For getargs() etc. */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 43 | static object *ErrorObject; |
| 44 | |
| 45 | typedef struct { |
| 46 | OB_HEAD |
| 47 | object *x_attr; /* Attributes dictionary */ |
| 48 | } xxoobject; |
| 49 | |
| 50 | staticforward typeobject Xxotype; |
| 51 | |
| 52 | #define is_xxoobject(v) ((v)->ob_type == &Xxotype) |
| 53 | |
| 54 | static xxoobject * |
| 55 | newxxoobject(arg) |
| 56 | object *arg; |
| 57 | { |
| 58 | xxoobject *self; |
| 59 | self = NEWOBJ(xxoobject, &Xxotype); |
| 60 | if (self == NULL) |
| 61 | return NULL; |
| 62 | self->x_attr = NULL; |
| 63 | return self; |
| 64 | } |
| 65 | |
| 66 | /* Xxo methods */ |
| 67 | |
| 68 | static void |
| 69 | xxo_dealloc(self) |
| 70 | xxoobject *self; |
| 71 | { |
| 72 | XDECREF(self->x_attr); |
| 73 | DEL(self); |
| 74 | } |
| 75 | |
| 76 | static object * |
| 77 | xxo_demo(self, args) |
| 78 | xxoobject *self; |
| 79 | object *args; |
| 80 | { |
| 81 | if (!getnoarg(args)) |
| 82 | return NULL; |
| 83 | INCREF(None); |
| 84 | return None; |
| 85 | } |
| 86 | |
| 87 | static struct methodlist xxo_methods[] = { |
| 88 | {"demo", (method)xxo_demo}, |
| 89 | {NULL, NULL} /* sentinel */ |
| 90 | }; |
| 91 | |
| 92 | static object * |
| 93 | xxo_getattr(self, name) |
| 94 | xxoobject *self; |
| 95 | char *name; |
| 96 | { |
| 97 | if (self->x_attr != NULL) { |
| 98 | object *v = dictlookup(self->x_attr, name); |
| 99 | if (v != NULL) { |
| 100 | INCREF(v); |
| 101 | return v; |
| 102 | } |
| 103 | } |
| 104 | return findmethod(xxo_methods, (object *)self, name); |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | xxo_setattr(self, name, v) |
| 109 | xxoobject *self; |
| 110 | char *name; |
| 111 | object *v; |
| 112 | { |
| 113 | if (self->x_attr == NULL) { |
| 114 | self->x_attr = newdictobject(); |
| 115 | if (self->x_attr == NULL) |
| 116 | return -1; |
| 117 | } |
| 118 | if (v == NULL) { |
| 119 | int rv = dictremove(self->x_attr, name); |
| 120 | if (rv < 0) |
| 121 | err_setstr(AttributeError, |
| 122 | "delete non-existing xxo attribute"); |
| 123 | return rv; |
| 124 | } |
| 125 | else |
| 126 | return dictinsert(self->x_attr, name, v); |
| 127 | } |
| 128 | |
| 129 | static typeobject Xxotype = { |
| 130 | OB_HEAD_INIT(&Typetype) |
| 131 | 0, /*ob_size*/ |
| 132 | "xxo", /*tp_name*/ |
| 133 | sizeof(xxoobject), /*tp_basicsize*/ |
| 134 | 0, /*tp_itemsize*/ |
| 135 | /* methods */ |
| 136 | (destructor)xxo_dealloc, /*tp_dealloc*/ |
| 137 | 0, /*tp_print*/ |
| 138 | (getattrfunc)xxo_getattr, /*tp_getattr*/ |
| 139 | (setattrfunc)xxo_setattr, /*tp_setattr*/ |
| 140 | 0, /*tp_compare*/ |
| 141 | 0, /*tp_repr*/ |
| 142 | 0, /*tp_as_number*/ |
| 143 | 0, /*tp_as_sequence*/ |
| 144 | 0, /*tp_as_mapping*/ |
| 145 | 0, /*tp_hash*/ |
| 146 | }; |
| 147 | /* --------------------------------------------------------------------- */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 148 | |
| 149 | /* Function of two integers returning integer */ |
| 150 | |
| 151 | static object * |
| 152 | xx_foo(self, args) |
| 153 | object *self; /* Not used */ |
| 154 | object *args; |
| 155 | { |
| 156 | long i, j; |
| 157 | long res; |
| 158 | if (!getargs(args, "(ll)", &i, &j)) |
| 159 | return NULL; |
| 160 | res = i+j; /* XXX Do something here */ |
| 161 | return newintobject(res); |
| 162 | } |
| 163 | |
| 164 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 165 | /* Function of no arguments returning new xxo object */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 166 | |
| 167 | static object * |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 168 | xx_new(self, args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 169 | object *self; /* Not used */ |
| 170 | object *args; |
| 171 | { |
| 172 | int i, j; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 173 | xxoobject *rv; |
| 174 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 175 | if (!getnoarg(args)) |
| 176 | return NULL; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 177 | rv = newxxoobject(args); |
| 178 | if ( rv == NULL ) |
| 179 | return NULL; |
| 180 | return (object *)rv; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
| 184 | /* List of functions defined in the module */ |
| 185 | |
| 186 | static struct methodlist xx_methods[] = { |
| 187 | {"foo", xx_foo}, |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 188 | {"new", xx_new}, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 189 | {NULL, NULL} /* sentinel */ |
| 190 | }; |
| 191 | |
| 192 | |
| 193 | /* Initialization function for the module (*must* be called initxx) */ |
| 194 | |
| 195 | void |
| 196 | initxx() |
| 197 | { |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 198 | object *m, *d; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 199 | |
| 200 | /* Create the module and add the functions */ |
| 201 | m = initmodule("xx", xx_methods); |
| 202 | |
| 203 | /* Add some symbolic constants to the module */ |
| 204 | d = getmoduledict(m); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 205 | ErrorObject = newstringobject("xx.error"); |
| 206 | dictinsert(d, "error", ErrorObject); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 207 | |
| 208 | /* Check for errors */ |
| 209 | if (err_occurred()) |
| 210 | fatal("can't initialize module xx"); |
| 211 | } |