blob: 52f48954e53f18c786420b530f300837bc5b9f2d [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossuma1c996c1994-01-02 02:11:40 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossum34679b71993-01-26 13:33:44 +00003Amsterdam, The 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 Rossum14ed0b21994-09-29 09:50:09 +000041#include "modsupport.h" /* For getargs() etc. */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000042
Guido van Rossum14ed0b21994-09-29 09:50:09 +000043static object *ErrorObject;
44
45typedef struct {
46 OB_HEAD
47 object *x_attr; /* Attributes dictionary */
48} xxoobject;
49
50staticforward typeobject Xxotype;
51
52#define is_xxoobject(v) ((v)->ob_type == &Xxotype)
53
54static xxoobject *
55newxxoobject(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
68static void
69xxo_dealloc(self)
70 xxoobject *self;
71{
72 XDECREF(self->x_attr);
73 DEL(self);
74}
75
76static object *
77xxo_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
87static struct methodlist xxo_methods[] = {
88 {"demo", (method)xxo_demo},
89 {NULL, NULL} /* sentinel */
90};
91
92static object *
93xxo_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
107static int
108xxo_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
129static 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 Rossum1984f1e1992-08-04 12:41:02 +0000148
149/* Function of two integers returning integer */
150
151static object *
152xx_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 Rossum14ed0b21994-09-29 09:50:09 +0000165/* Function of no arguments returning new xxo object */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000166
167static object *
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000168xx_new(self, args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169 object *self; /* Not used */
170 object *args;
171{
172 int i, j;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000173 xxoobject *rv;
174
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000175 if (!getnoarg(args))
176 return NULL;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000177 rv = newxxoobject(args);
178 if ( rv == NULL )
179 return NULL;
180 return (object *)rv;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181}
182
183
184/* List of functions defined in the module */
185
186static struct methodlist xx_methods[] = {
187 {"foo", xx_foo},
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000188 {"new", xx_new},
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000189 {NULL, NULL} /* sentinel */
190};
191
192
193/* Initialization function for the module (*must* be called initxx) */
194
195void
196initxx()
197{
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000198 object *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199
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 Rossum14ed0b21994-09-29 09:50:09 +0000205 ErrorObject = newstringobject("xx.error");
206 dictinsert(d, "error", ErrorObject);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000207
208 /* Check for errors */
209 if (err_occurred())
210 fatal("can't initialize module xx");
211}