blob: 1cf2d91e88101c6513de4332f994eff669552d85 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
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 Rossum320a5cc1991-01-02 15:11:48 +000025/* 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 Rossum85a5fbb1990-10-14 12:07:46 +000035/* Xx objects */
36
Guido van Rossum320a5cc1991-01-02 15:11:48 +000037#include "allobjects.h"
38
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039typedef struct {
40 OB_HEAD
41 object *x_attr; /* Attributes dictionary */
42} xxobject;
43
44extern typeobject Xxtype; /* Really static, forward */
45
Guido van Rossum320a5cc1991-01-02 15:11:48 +000046#define is_xxobject(v) ((v)->ob_type == &Xxtype)
47
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048static xxobject *
49newxxobject(arg)
50 object *arg;
51{
Guido van Rossum94726d51991-01-02 15:12:51 +000052 xxobject *xp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053 xp = NEWOBJ(xxobject, &Xxtype);
54 if (xp == NULL)
55 return NULL;
56 xp->x_attr = NULL;
57 return xp;
58}
59
60/* Xx methods */
61
62static void
63xx_dealloc(xp)
64 xxobject *xp;
65{
Guido van Rossum320a5cc1991-01-02 15:11:48 +000066 XDECREF(xp->x_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 DEL(xp);
68}
69
70static object *
71xx_demo(self, args)
72 xxobject *self;
73 object *args;
74{
75 if (!getnoarg(args))
76 return NULL;
77 INCREF(None);
78 return None;
79}
80
81static struct methodlist xx_methods[] = {
82 "demo", xx_demo,
83 {NULL, NULL} /* sentinel */
84};
85
86static object *
87xx_getattr(xp, name)
88 xxobject *xp;
89 char *name;
90{
91 if (xp->x_attr != NULL) {
92 object *v = dictlookup(xp->x_attr, name);
93 if (v != NULL) {
94 INCREF(v);
95 return v;
96 }
97 }
98 return findmethod(xx_methods, (object *)xp, name);
99}
100
101static int
102xx_setattr(xp, name, v)
103 xxobject *xp;
104 char *name;
105 object *v;
106{
107 if (xp->x_attr == NULL) {
108 xp->x_attr = newdictobject();
109 if (xp->x_attr == NULL)
Guido van Rossum320a5cc1991-01-02 15:11:48 +0000110 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 }
112 if (v == NULL)
113 return dictremove(xp->x_attr, name);
114 else
115 return dictinsert(xp->x_attr, name, v);
116}
117
118static typeobject Xxtype = {
119 OB_HEAD_INIT(&Typetype)
120 0, /*ob_size*/
121 "xx", /*tp_name*/
122 sizeof(xxobject), /*tp_size*/
123 0, /*tp_itemsize*/
124 /* methods */
125 xx_dealloc, /*tp_dealloc*/
126 0, /*tp_print*/
127 xx_getattr, /*tp_getattr*/
128 xx_setattr, /*tp_setattr*/
129 0, /*tp_compare*/
130 0, /*tp_repr*/
131};