forked from shichao-an/ooc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoint.c
91 lines (71 loc) · 1.64 KB
/
Point.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <assert.h>
#include "Point.h"
#include "Point.r"
/*
* Point
*/
static void * Point_ctor (void * _self, va_list * app)
{ struct Point * self = super_ctor(Point, _self, app);
self -> x = va_arg(* app, int);
self -> y = va_arg(* app, int);
return self;
}
static void Point_draw (const void * _self)
{ const struct Point * self = _self;
printf("\".\" at %d,%d\n", self -> x, self -> y);
}
void draw (const void * _self)
{ const struct PointClass * class = classOf(_self);
assert(class -> draw);
class -> draw(_self);
}
void super_draw (const void * _class, const void * _self)
{ const struct PointClass * superclass = super(_class);
assert(_self && superclass -> draw);
superclass -> draw(_self);
}
void move (void * _self, int dx, int dy)
{ struct Point * self = _self;
self -> x += dx, self -> y += dy;
}
/*
* PointClass
*/
static void * PointClass_ctor (void * _self, va_list * app)
{ struct PointClass * self
= super_ctor(PointClass, _self, app);
typedef void (* voidf) ();
voidf selector;
#ifdef va_copy
va_list ap; va_copy(ap, * app);
#else
va_list ap = * app;
#endif
while ((selector = va_arg(ap, voidf)))
{ voidf method = va_arg(ap, voidf);
if (selector == (voidf) draw)
* (voidf *) & self -> draw = method;
}
#ifdef va_copy
va_end(ap);
#endif
return self;
}
/*
* initialization
*/
const void * PointClass, * Point;
void initPoint (void)
{
if (! PointClass)
PointClass = new(Class, "PointClass",
Class, sizeof(struct PointClass),
ctor, PointClass_ctor,
0);
if (! Point)
Point = new(PointClass, "Point",
Object, sizeof(struct Point),
ctor, Point_ctor,
draw, Point_draw,
0);
}