blob: ac81f7781cdf0d96696ea82cd61fb6958d76d623 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum14ed0b21994-09-29 09:50:09 +000025/* 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 Rossum1984f1e1992-08-04 12:41:02 +000039
40#include "allobjects.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000041
Guido van Rossum14ed0b21994-09-29 09:50:09 +000042static object *ErrorObject;
43
44typedef struct {
45 OB_HEAD
46 object *x_attr; /* Attributes dictionary */
47} xxoobject;
48
49staticforward typeobject Xxotype;
50
51#define is_xxoobject(v) ((v)->ob_type == &Xxotype)
52
53static xxoobject *
54newxxoobject(arg)
55 object *arg;
56{
57 xxoobject *self;
58 self = NEWOBJ(xxoobject, &Xxotype);
59 if (self == NULL)
60 return NULL;
61 self->x_attr = NULL;
62 return self;
63}
64
65/* Xxo methods */
66
67static void
68xxo_dealloc(self)
69 xxoobject *self;
70{
71 XDECREF(self->x_attr);
72 DEL(self);
73}
74
75static object *
76xxo_demo(self, args)
77 xxoobject *self;
78 object *args;
79{
80 if (!getnoarg(args))
81 return NULL;
82 INCREF(None);
83 return None;
84}
85
86static struct methodlist xxo_methods[] = {
87 {"demo", (method)xxo_demo},
88 {NULL, NULL} /* sentinel */
89};
90
91static object *
92xxo_getattr(self, name)
93 xxoobject *self;
94 char *name;
95{
96 if (self->x_attr != NULL) {
97 object *v = dictlookup(self->x_attr, name);
98 if (v != NULL) {
99 INCREF(v);
100 return v;
101 }
102 }
103 return findmethod(xxo_methods, (object *)self, name);
104}
105
106static int
107xxo_setattr(self, name, v)
108 xxoobject *self;
109 char *name;
110 object *v;
111{
112 if (self->x_attr == NULL) {
113 self->x_attr = newdictobject();
114 if (self->x_attr == NULL)
115 return -1;
116 }
117 if (v == NULL) {
118 int rv = dictremove(self->x_attr, name);
119 if (rv < 0)
120 err_setstr(AttributeError,
121 "delete non-existing xxo attribute");
122 return rv;
123 }
124 else
125 return dictinsert(self->x_attr, name, v);
126}
127
Guido van Rossum8bdd3331995-02-18 14:53:14 +0000128staticforward typeobject Xxotype = {
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000129 OB_HEAD_INIT(&Typetype)
130 0, /*ob_size*/
131 "xxo", /*tp_name*/
132 sizeof(xxoobject), /*tp_basicsize*/
133 0, /*tp_itemsize*/
134 /* methods */
135 (destructor)xxo_dealloc, /*tp_dealloc*/
136 0, /*tp_print*/
137 (getattrfunc)xxo_getattr, /*tp_getattr*/
138 (setattrfunc)xxo_setattr, /*tp_setattr*/
139 0, /*tp_compare*/
140 0, /*tp_repr*/
141 0, /*tp_as_number*/
142 0, /*tp_as_sequence*/
143 0, /*tp_as_mapping*/
144 0, /*tp_hash*/
145};
146/* --------------------------------------------------------------------- */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147
148/* Function of two integers returning integer */
149
150static object *
151xx_foo(self, args)
152 object *self; /* Not used */
153 object *args;
154{
155 long i, j;
156 long res;
157 if (!getargs(args, "(ll)", &i, &j))
158 return NULL;
159 res = i+j; /* XXX Do something here */
160 return newintobject(res);
161}
162
163
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000164/* Function of no arguments returning new xxo object */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165
166static object *
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000167xx_new(self, args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000168 object *self; /* Not used */
169 object *args;
170{
171 int i, j;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000172 xxoobject *rv;
173
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174 if (!getnoarg(args))
175 return NULL;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000176 rv = newxxoobject(args);
177 if ( rv == NULL )
178 return NULL;
179 return (object *)rv;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180}
181
182
183/* List of functions defined in the module */
184
185static struct methodlist xx_methods[] = {
186 {"foo", xx_foo},
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000187 {"new", xx_new},
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000188 {NULL, NULL} /* sentinel */
189};
190
191
192/* Initialization function for the module (*must* be called initxx) */
193
194void
195initxx()
196{
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000197 object *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000198
199 /* Create the module and add the functions */
200 m = initmodule("xx", xx_methods);
201
202 /* Add some symbolic constants to the module */
203 d = getmoduledict(m);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000204 ErrorObject = newstringobject("xx.error");
205 dictinsert(d, "error", ErrorObject);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000206
207 /* Check for errors */
208 if (err_occurred())
209 fatal("can't initialize module xx");
210}