forked from lucklove/zobject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zobject.c
364 lines (330 loc) · 9.93 KB
/
zobject.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "zobject.h"
#include "mem.h"
#include "rb_tree.h"
#include "container.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define to_class(node) container_of(node, struct ZObjClass, rb_node)
#define to_interface(node) container_of(node, struct InterfaceInfo, rb_node)
struct InterfaceInfo {
const char *interface_name;
// struct InterfaceInfo *parent;
mem_t parents;
unsigned int struct_size;
struct RBNode rb_node;
};
// 保存类型
static RBTree class_tree;
static RBTree interface_tree;
//need by makeRBTree()
static void *
get_class(struct RBNode *node)
{
return to_class(node);
}
static void *
get_interface(struct RBNode *node)
{
return to_interface(node);
}
//need by makeRBTree()
static int
cmp_class(void *n1, void *n2)
{
return strcmp(((struct ZObjClass *)n1)->class_name,
((struct ZObjClass *)n2)->class_name);
}
static int
cmp_interface(void *n1, void *n2)
{
return strcmp(((struct InterfaceInfo *)n1)->interface_name,
((struct InterfaceInfo *)n2)->interface_name);
}
//need by makeRBTree()
static struct RBNode *
make_class(void *class_info)
{
struct ZObjClass *class = malloc(sizeof(struct ZObjClass));
assert(class != NULL);
*class = *(struct ZObjClass *)class_info;
return &class->rb_node;
}
static struct RBNode *
make_interface(void *interface_info)
{
struct InterfaceInfo *info = malloc(sizeof(struct InterfaceInfo));
assert(info != NULL);
*info = *(struct InterfaceInfo *)interface_info;
return &info->rb_node;
}
//need by makeRBTree()
static void
free_class(struct RBNode *node)
{
free(to_class(node));
}
static void
free_interface(struct RBNode *node)
{
free(to_interface(node));
}
//init object system
void
zObjInit()
{
class_tree = makeRBTree(get_class, cmp_class, make_class, free_class);
interface_tree = makeRBTree(get_interface, cmp_interface, make_interface, free_interface);
}
//find class in rb_tree by class name
static struct ZObjClass *
find_class(const char *name)
{
struct ZObjClass tmp_class = { name };
struct RBNode *node = rbSearch(class_tree, &tmp_class);
if(node == NULL)
return NULL;
return to_class(node);
}
static struct InterfaceInfo *
find_interface(const char *name)
{
struct InterfaceInfo tmp_info = { name };
struct RBNode *node = rbSearch(interface_tree, &tmp_info);
if(node == NULL)
return NULL;
return to_interface(node);
}
static void
dup_parent_class(struct ZObjClass *self)
{
if(self->parent == NULL)
return;
struct ZObjClass *new_parent = malloc(sizeof(struct ZObjClass));
assert(new_parent != NULL);
memset(new_parent, 0, sizeof(struct ZObjClass));
new_parent->class_name = self->parent->class_name;
new_parent->parent = self->parent->parent;
new_parent->constructor = self->parent->constructor;
new_parent->destructor = self->parent->destructor;
new_parent->interfaces = makeMem(getMemIndex(self->parent->interfaces));
new_parent->class_body = makeMem(getMemIndex(self->parent->class_body));
memCpy(&new_parent->class_body, getMemPtr(&self->parent->class_body, 0, 0),
getMemIndex(self->parent->class_body));
for(int i = 0; i < GET_ITEM_NUM(self->parent->interfaces, struct ZObjInterface); ++i) {
struct InterfaceInfo *info = find_interface(GET_TYPE_MEM(&self->parent->interfaces,
struct ZObjInterface, i)->interface_name);
if(info == NULL) {
printf("CRITICAL: can't find interface %s when dup_parent_class\n",
GET_TYPE_MEM(&self->parent->interfaces,struct ZObjInterface, i)->interface_name);
continue;
}
void *body = malloc(info->struct_size);
assert(body != NULL);
if(GET_TYPE_MEM(&self->parent->interfaces, struct ZObjInterface, i)->interface_body)
memcpy(body,
GET_TYPE_MEM(&self->parent->interfaces, struct ZObjInterface, i)->interface_body,
info->struct_size);
ADD_ITEM(&new_parent->interfaces, struct ZObjInterface,
((struct ZObjInterface){
GET_TYPE_MEM(&self->parent->interfaces,struct ZObjInterface, i)->interface_name,
body }));
}
self->parent = new_parent;
dup_parent_class(self->parent);
}
static int
strip_class(const char *self)
{
struct ZObjClass *dst_class = NULL;
if((dst_class = find_class(self)) == NULL) {
printf("CRITICAL: class %s not found.\n", self);
return -1;
}
dup_parent_class(dst_class);
return 0;
}
int
zRegistClass(const char *class_name, const char *parent_name, void *(*cons)(void *, void *),
void (*des)(void *), unsigned int ins_size, void *class_body, unsigned int size_of_class)
{
mem_t class = makeMem(size_of_class);
struct ZObjClass *parent = NULL;
if(find_class(class_name) != NULL) {
printf("CRITICAL: redefined class %s.\n", class_name);
return -1;
}
if(parent_name != NULL) {
parent = find_class(parent_name);
if(parent == NULL)
printf("CRITICAL: parent not found.\n");
}
if(class_body != NULL)
memCpy(&class, class_body, size_of_class);
struct ZObjClass reg_class = { class_name, parent, cons, des, ins_size, makeMem(0), class };
rbInsert(&class_tree, ®_class);
return strip_class(class_name);
}
bool
zRegistInterface(const char *interface_name, unsigned int size_of_interface)
{
struct InterfaceInfo reg_interface = { interface_name, makeMem(0), size_of_interface };
return rbInsert(&interface_tree, ®_interface);
}
bool
zInterfaceAddParent(const char *interface_name, const char *parent_name)
{
struct InterfaceInfo *self = NULL;
struct InterfaceInfo *parent = NULL;
self = find_interface(interface_name);
if(self == NULL) {
printf("CRITICAL: interface %s not found.\n", interface_name);
return false;
}
parent = find_interface(parent_name);
if(parent == NULL) {
printf("CRITICAL: interface %s not found.\n", parent_name);
return false;
}
ADD_ITEM(&self->parents, struct InterfaceInfo *, parent);
return true;
}
static void *
get_interface_in_class(const char *class_name, const char *interface_name)
{
struct ZObjClass *dst_class = find_class(class_name);
if(dst_class == NULL) {
printf("CRITICAL: class %s not found.\n", class_name);
return NULL;
}
while(dst_class != NULL) {
for(int i = 0; i < GET_ITEM_NUM(dst_class->interfaces, struct ZObjInterface); ++i) {
if(strcmp(interface_name, GET_TYPE_MEM(&dst_class->interfaces,
struct ZObjInterface, i)->interface_name) == 0)
return GET_TYPE_MEM(&dst_class->interfaces, struct ZObjInterface, i)->interface_body;
}
dst_class = dst_class->parent;
}
return NULL;
}
static void
add_interface_to_class(struct ZObjClass *dst_class, struct InterfaceInfo *dst_interface)
{
if(get_interface_in_class(dst_class->class_name, dst_interface->interface_name))
return;
for(int i = 0; i < GET_ITEM_NUM(dst_interface->parents, struct InterfaceInfo *); ++i) {
add_interface_to_class(dst_class,
GET_TYPE_ITEM(&dst_interface->parents, struct InterfaceInfo *, i));
}
void *interface_body = malloc(dst_interface->struct_size);
assert(interface_body != NULL);
memset(interface_body, 0, dst_interface->struct_size);
ADD_ITEM(&dst_class->interfaces, struct ZObjInterface,
((struct ZObjInterface){ dst_interface->interface_name, interface_body }));
}
bool
zAddInterface(const char *class_name, const char *interface_name)
{
struct ZObjClass *dst_class = find_class(class_name);
struct InterfaceInfo *dst_interface = find_interface(interface_name);
if(dst_class == NULL) {
printf("CRITICAL: class %s not found.\n", class_name);
return false;
}
if(dst_interface == NULL) {
printf("CRITICAL: interface %s not found.\n", interface_name);
return false;
}
add_interface_to_class(dst_class, dst_interface);
return true;
}
void *
zGetInterfaceByName(const char *class_name, const char *interface_name)
{
void *ret = get_interface_in_class(class_name, interface_name);
if(ret == NULL)
printf("CRITICAL: interface %s not found.\n", interface_name);
return ret;
}
void *
zGetInterfaceByInstance(struct ZObjInstance *ins, const char *interface_name)
{
return zGetInterfaceByName(ins->class->class_name, interface_name);
}
static struct ZObjInstance *
construct_instance(struct ZObjClass *class, void *data)
{
struct ZObjInstance *instance = malloc(sizeof(*instance));
assert(instance != NULL);
memset(instance, 0, sizeof(*instance));
instance->class = class;
instance->instance_body = malloc(class->instance_size);
assert(instance->instance_body != NULL);
memset(instance->instance_body, 0, class->instance_size);
void *parent_data = class->constructor(instance->instance_body, data);
if(class->parent != NULL)
instance->parent = construct_instance(class->parent, parent_data);
return instance;
}
struct ZObjInstance *
zNewInstance(const char *class_name, void *data)
{
struct ZObjClass *dst_class = find_class(class_name);
if(dst_class == NULL) {
printf("CRITICAL: class %s not found.\n", class_name);
return NULL;
}
return construct_instance(dst_class, data);
}
void
zDesInstance(struct ZObjInstance *ins)
{
if(ins->parent != NULL)
zDesInstance(ins->parent);
if(ins->class->destructor) {
ins->class->destructor(ins->instance_body);
free(ins->instance_body);
}
}
void *
zGetClassByName(const char *self, const char *class_name)
{
struct ZObjClass *dst_class = NULL;
if((dst_class = find_class(self)) == NULL) {
printf("CRITICAL: class %s not found.\n", self);
return NULL;
}
if(class_name != NULL) {
while(dst_class != NULL && strcmp(dst_class->class_name, class_name) != 0)
dst_class = dst_class->parent;
if(dst_class == NULL) {
printf("CRITICAL: class %s is not on inheritance chain of %s\n", class_name, self);
return NULL;
}
}
return getMemPtr(&dst_class->class_body, 0, 0);
}
void *
zGetClassByInstance(struct ZObjInstance *ins, const char *class_name)
{
if(class_name == NULL)
return getMemPtr(&ins->class->class_body, 0, 0);
return zGetClassByName(ins->class->class_name, class_name);
}
void *
zGetInstance(struct ZObjInstance *ins, const char *class_name)
{
if(ins == NULL)
return NULL;
if(class_name != NULL) {
while(strcmp(ins->class->class_name, class_name) != 0) {
if(ins->class->parent == NULL) {
printf("CRITICAL: class %s not found.\n", class_name);
return NULL;
}
ins = ins->parent;
}
}
return ins->instance_body;
}