-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.c
More file actions
175 lines (133 loc) · 3.62 KB
/
object.c
File metadata and controls
175 lines (133 loc) · 3.62 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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <memory.h>
#include "object.h"
typedef struct Object_TAG {
struct Object_DATA_TAG data;
} Object;
DEFINE_CLASS_CONSTRUCTOR(Object)(Object *self)
{
assert(self != NULL);
// put the guard
((Object_DATA *)&self->data)->guard = &Object_VT;
// the initial Object virtual table
Object_VT_update(self, &Object_VT);
return self;
}
static void Object_destroy(void *this)
{
printf("Object::destroy()\n");
}
static void Object_copy(void *this, void *other)
{
printf("Object::copy()\n");
}
struct Object_VT_TAG Object_VT = {
NULL,
sizeof(Object),
&Object_destroy,
&Object_copy
};
void Object_VT_update(void *this, void *vt)
{
assert(this != NULL);
assert(vt != NULL);
((Object *)this)->data.vt = vt;
}
Object * Object_allocate(void *size_data)
{
size_t size;
Object * obj;
assert(size_data != NULL);
size = ((struct Object_VT_TAG *)size_data)->size;
obj = (Object *)malloc(size);
if (!obj) {
fprintf(stderr, "Exception: Out of memory\n");
abort();
}
memset(obj, 0, size);
Object_construct(obj);
return obj;
}
static void copy_construct(void *dst, void *src, struct Object_VT_TAG *vt)
{
if (vt)
{
// call superclass copy first
copy_construct(dst, src, vt->svt);
// now set the virtual table to the current level
((Object *)dst)->data.vt = vt;
// call the copy on the current level
vt->copy(dst, src);
}
}
void Object_callCopyConstruct(void *dst, void *src, struct Object_VT_TAG *vt)
{
assert(src != NULL);
assert(dst != NULL);
assert(vt != NULL);
// by default, binary (shallow) copy is done
// inherited class can supply copy() to provide different semantics
memcpy(dst, src, vt->size);
copy_construct(dst, src, vt);
}
Object * Object_clone(void *src)
{
Object *dst;
struct Object_VT_TAG *vt;
if (!src) return NULL;
// or runtime check and abort()
assert(IS_INSTANCE_OF(src, Object));
vt = ((struct Object_DATA_TAG *)src)->vt;
dst = Object_allocate(vt);
Object_callCopyConstruct(dst, src, vt);
return dst;
}
void destroy(void *obj)
{
struct Object_VT_TAG *vt;
// or runtime check and abort()
assert(IS_INSTANCE_OF(obj, Object));
vt = ((struct Object_DATA_TAG *)obj)->vt;
while (vt)
{
if (!vt->destroy)
{
fprintf(stderr, "Pure virtual function call!\n");
abort();
}
vt->destroy(obj);
// move VT to superclass (will continue calling parent destructor)
((struct Object_DATA_TAG *)obj)->vt = vt = vt->svt;
}
}
void delete(void *obj)
{
if (obj) destroy(obj);
free(obj);
}
int Object_isInstanceOf(void *obj, void *type_vt)
{
struct Object_VT_TAG *obj_vt;
assert(type_vt != NULL);
if (!obj) return 0;
// check if the guard matches and there is a nonzero VT table pointer
if (*(void **)obj != &Object_VT || (*(void ***)obj + 1) == NULL) return 0;
// note the above still can fail in some cases - to solve this properly,
// building of registry of all know VTs woould need to be done and the
// object checked against that registry
// (the registry can be build/updated by Object_allocate() which is to
// be called for each valid object)
obj_vt = ((struct Object_DATA_TAG *)obj)->vt;
while (obj_vt)
{
if (obj_vt == type_vt) return 1;
obj_vt = obj_vt->svt;
}
return 0;
}
void * Object_dynamicCast(void *obj, void *type_vt)
{
return Object_isInstanceOf(obj, type_vt) ? obj : NULL;
}