blob: 676f72772a8f6b1f14526c8789e5bcda46ff5abc [file] [log] [blame]
Guido van Rossum320a5cc1991-01-02 15:11:48 +00001/* Use this file as a template to start implementing a new object type.
2 If your objects will be called foobar, start by copying this file to
3 foobarobject.c, changing all occurrences of xx to foobar and all
4 occurrences of Xx by Foobar. You will probably want to delete all
5 references to 'x_attr' and add your own types of attributes
6 instead. Maybe you want to name your local variables other than
7 'xp'. If your object type is needed in other files, you'll have to
8 create a file "foobarobject.h"; see intobject.h for an example. */
9
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Xx objects */
12
Guido van Rossum320a5cc1991-01-02 15:11:48 +000013#include "allobjects.h"
14
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015typedef struct {
16 OB_HEAD
17 object *x_attr; /* Attributes dictionary */
18} xxobject;
19
20extern typeobject Xxtype; /* Really static, forward */
21
Guido van Rossum320a5cc1991-01-02 15:11:48 +000022#define is_xxobject(v) ((v)->ob_type == &Xxtype)
23
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024static xxobject *
25newxxobject(arg)
26 object *arg;
27{
Guido van Rossum94726d51991-01-02 15:12:51 +000028 xxobject *xp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029 xp = NEWOBJ(xxobject, &Xxtype);
30 if (xp == NULL)
31 return NULL;
32 xp->x_attr = NULL;
33 return xp;
34}
35
36/* Xx methods */
37
38static void
39xx_dealloc(xp)
40 xxobject *xp;
41{
Guido van Rossum320a5cc1991-01-02 15:11:48 +000042 XDECREF(xp->x_attr);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043 DEL(xp);
44}
45
46static object *
47xx_demo(self, args)
48 xxobject *self;
49 object *args;
50{
51 if (!getnoarg(args))
52 return NULL;
53 INCREF(None);
54 return None;
55}
56
57static struct methodlist xx_methods[] = {
58 "demo", xx_demo,
59 {NULL, NULL} /* sentinel */
60};
61
62static object *
63xx_getattr(xp, name)
64 xxobject *xp;
65 char *name;
66{
67 if (xp->x_attr != NULL) {
68 object *v = dictlookup(xp->x_attr, name);
69 if (v != NULL) {
70 INCREF(v);
71 return v;
72 }
73 }
74 return findmethod(xx_methods, (object *)xp, name);
75}
76
77static int
78xx_setattr(xp, name, v)
79 xxobject *xp;
80 char *name;
81 object *v;
82{
83 if (xp->x_attr == NULL) {
84 xp->x_attr = newdictobject();
85 if (xp->x_attr == NULL)
Guido van Rossum320a5cc1991-01-02 15:11:48 +000086 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 }
88 if (v == NULL)
89 return dictremove(xp->x_attr, name);
90 else
91 return dictinsert(xp->x_attr, name, v);
92}
93
94static typeobject Xxtype = {
95 OB_HEAD_INIT(&Typetype)
96 0, /*ob_size*/
97 "xx", /*tp_name*/
98 sizeof(xxobject), /*tp_size*/
99 0, /*tp_itemsize*/
100 /* methods */
101 xx_dealloc, /*tp_dealloc*/
102 0, /*tp_print*/
103 xx_getattr, /*tp_getattr*/
104 xx_setattr, /*tp_setattr*/
105 0, /*tp_compare*/
106 0, /*tp_repr*/
107};