This repository was archived by the owner on Dec 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtype.c
More file actions
220 lines (172 loc) · 4.16 KB
/
type.c
File metadata and controls
220 lines (172 loc) · 4.16 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "type.h"
#define INIT_REGISTRY_SIZE (1 << 12)
#define SIZE_TO_N_ENTRIES(s) ((s - sizeof(TypeRegistry)) / sizeof(TypeClass *))
#define N_ENTRIES_TO_SIZE(n) (n * sizeof(TypeClass *) + sizeof(TypeRegistry))
typedef struct _TypeRegistry TypeRegistry;
struct _TypeRegistry
{
size_t n_entries;
Type next_type;
TypeClass * classes[0];
};
static void type_class_init(TypeClass * self);
static TypeRegistry * type_registry = NULL;
static TypeRegistry * type_registry_get()
{
if(! type_registry) {
type_registry = malloc(INIT_REGISTRY_SIZE);
type_registry->next_type = 1;
type_registry->n_entries = SIZE_TO_N_ENTRIES(INIT_REGISTRY_SIZE);
type_registry->classes[0] = NULL;
}
return type_registry;
}
static TypeRegistry * type_registry_get_for_add()
{
TypeRegistry * self = type_registry_get();
if(self->next_type >= self->n_entries) {
size_t new_size = N_ENTRIES_TO_SIZE(self->n_entries);
self = realloc(self, new_size);
self->n_entries = SIZE_TO_N_ENTRIES(new_size);
type_registry = self;
}
return self;
}
static Type type_registry_add(TypeClass * clazz)
{
assert(NULL != clazz);
TypeRegistry * self = type_registry_get_for_add();
clazz->type = self->next_type ++;
self->classes[clazz->type] = clazz;
return clazz->type;
}
static Type type_registry_get_n_classes()
{
return type_registry_get()->next_type;
}
static TypeClass * type_registry_get_class(Type type)
{
assert(type_registry_get_n_classes() > type);
return type_registry_get()->classes[type];
}
Type type_register(Type parent,
const char * name,
size_t class_size,
ClassInitFunc class_init,
size_t inst_size,
TypeFlags flags)
{
assert(NULL != name && 3 <= strlen(name));
assert(type_registry_get_n_classes() > parent);
if(parent) {
assert(type_registry_get_class(parent)->class_size <= class_size);
assert(type_registry_get_class(parent)->inst_size <= inst_size);
}
else {
assert(sizeof(TypeClass) <= class_size);
assert(sizeof(TypeInstance) <= inst_size);
}
TypeClass * clazz = malloc(class_size);
* clazz = (TypeClass) {
.parent = parent,
.name = strdup(name),
.class_size = class_size,
.class_init = class_init,
.inst_size = inst_size,
.flags = flags,
.inited = 0
};
if(! clazz) {
fprintf(stderr, "Out of memory\n");
abort();
}
return type_registry_add(clazz);
}
TypeClass * type_get_class(Type self)
{
return type_registry_get_class(self);
}
static int type_class_is_inited(TypeClass * self)
{
return self->inited;
}
static void type_class_do_init(TypeClass * self, Type acestor)
{
TypeClass * acestor_class = type_get_class(acestor);
if(! type_class_is_inited(acestor_class)) {
type_class_init(acestor_class);
}
if(TYPE_INVALID != acestor_class->parent) {
type_class_do_init(acestor_class, acestor_class->parent);
}
if(acestor_class->class_init) {
acestor_class->class_init(self);
}
}
static void type_class_init(TypeClass * self)
{
assert(! type_class_is_inited(self));
if(TYPE_INVALID != self->parent) {
type_class_do_init(self, self->parent);
}
if(self->class_init) {
self->class_init(self);
}
self->inited = 1;
}
int type_is_instantiatable(Type self)
{
if(TYPE_INVALID == self) {
return 0;
}
if(TYPE_FLAGS_ABSTRACT & type_get_class(self)->flags) {
return 0;
}
return 1;
}
TypeInstance * type_create_instance(Type self)
{
assert(TYPE_INVALID != self);
assert(type_is_instantiatable(self));
TypeClass * clazz = type_get_class(self);
if(! type_class_is_inited(clazz)) {
type_class_init(clazz);
}
TypeInstance * inst = malloc(clazz->inst_size);
if(inst) {
inst->type = self;
}
return inst;
}
const char * type_get_name(Type self)
{
return type_registry_get_class(self)->name;
}
int type_is(Type self, Type other)
{
Type curr = self;
do {
if(curr == other) {
return 1;
}
curr = type_get_class(curr)->parent;
}
while(TYPE_INVALID != curr);
return 0;
}
TypeClass * type_instance_get_class(TypeInstance * self)
{
assert(NULL != self);
return type_get_class(self->type);
}
Type type_get_parent(Type type)
{
if(TYPE_INVALID == type) {
return TYPE_INVALID;
}
return type_get_class(type)->parent;
}