-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
230 lines (178 loc) · 4.93 KB
/
main.c
File metadata and controls
230 lines (178 loc) · 4.93 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
221
222
223
224
225
226
227
228
229
230
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
enum
{
CLASS_BASE,
CLASS_CHILDONE,
CLASS_CHILDTWO,
NUM_CLASSES
};
enum
{
VFUNC_DESTRUCTOR,
VFUNC_FUNC1,
VFUNC_FUNC2,
NUM_VFUNCS
};
typedef void (*Destructor)(void*);
typedef void (*Func1)();
typedef float (*Func2)(int arg1);
void*** g_allVTables = NULL;
// Base
typedef struct
{
void** vtable;
int a;
float b;
} Base;
void Base_Construct(Base* pThis)
{
pThis->vtable = g_allVTables[CLASS_BASE];
printf("Base_Construct\n");
pThis->a = 0;
pThis->b = 0.f;
}
void Base_Destruct(Base* pThis)
{
printf("Base_Destruct\n");
}
void Base_Func1()
{
printf("Base_Func1\n");
}
float Base_Func2(int arg1)
{
printf("Base_Func2 arg1=%d\n", arg1);
return 1.0f;
}
// ChildOne
typedef struct
{
Base base; // This is how we "inherit" from Base in C to have the same memory layout
char c;
} ChildOne;
void ChildOne_Construct(ChildOne* pThis)
{
Base_Construct((Base*)pThis);
((Base*)pThis)->vtable = g_allVTables[CLASS_CHILDONE];
printf("ChildOne_Construct\n");
pThis->c = 0;
}
void ChildOne_Destruct(ChildOne* pThis)
{
printf("ChildOne_Destruct\n");
Base_Destruct((Base*)pThis);
}
void ChildOne_Func1()
{
printf("ChildOne_Func1\n");
}
// ChildTwo
typedef struct
{
ChildOne base; // This is how we "inherit" from Base in C to have the same memory layout
short c;
short d;
} ChildTwo;
void ChildTwo_Construct(ChildTwo* pThis)
{
ChildOne_Construct((ChildOne*)pThis);
((Base*)pThis)->vtable = g_allVTables[CLASS_CHILDTWO];
printf("ChildTwo_Construct\n");
pThis->c = 0;
pThis->d = 0;
}
void ChildTwo_Destruct(ChildTwo* pThis)
{
printf("ChildTwo_Destruct\n");
ChildOne_Destruct((ChildOne*)pThis);
}
float ChildTwo_Func2(int arg1)
{
printf("ChildTwo_Func2 arg1=%d\n", arg1);
return 1.0f;
}
// These are the functions we need to implement ourselves in C that's automatically handled by the C++ compiler
Base* NewBase()
{
Base* pInstance = (Base*)malloc(sizeof(Base));
Base_Construct(pInstance);
return pInstance;
}
ChildOne* NewChildOne()
{
ChildOne* pInstance = (ChildOne*)malloc(sizeof(ChildOne));
ChildOne_Construct(pInstance);
return pInstance;
}
ChildTwo* NewChildTwo()
{
ChildTwo* pInstance = (ChildTwo*)malloc(sizeof(ChildTwo));
ChildTwo_Construct(pInstance);
return pInstance;
}
void DeleteBase(Base* pThis)
{
if (pThis != 0)
{
((Destructor)pThis->vtable[VFUNC_DESTRUCTOR])(pThis);
free(pThis);
}
}
void InitVTables()
{
int i;
// We need a pointer to a vtable per class
g_allVTables = (void***)malloc( sizeof(void**) * NUM_CLASSES );
// For each class, we allocate vtable - in our case, it's simple as we have a fixed number
// of virtual functions for each class.
for (i = 0; i < NUM_CLASSES; ++i)
{
g_allVTables[i] = (void**)malloc(sizeof(void*) * NUM_VFUNCS);
}
// Populate Base vtable entries
g_allVTables[CLASS_BASE][VFUNC_DESTRUCTOR] = (void*)&Base_Destruct;
g_allVTables[CLASS_BASE][VFUNC_FUNC1] = (void*)&Base_Func1;
g_allVTables[CLASS_BASE][VFUNC_FUNC2] = (void*)&Base_Func2;
// Populate ChildOne vtable entries
g_allVTables[CLASS_CHILDONE][VFUNC_DESTRUCTOR] = (void*)&ChildOne_Destruct;
g_allVTables[CLASS_CHILDONE][VFUNC_FUNC1] = (void*)&ChildOne_Func1;
g_allVTables[CLASS_CHILDONE][VFUNC_FUNC2] = (void*)&Base_Func2;
// Populate ChildTwo vtable entries
g_allVTables[CLASS_CHILDTWO][VFUNC_DESTRUCTOR] = (void*)&ChildTwo_Destruct;
g_allVTables[CLASS_CHILDTWO][VFUNC_FUNC1] = (void*)&ChildOne_Func1;
g_allVTables[CLASS_CHILDTWO][VFUNC_FUNC2] = (void*)&ChildTwo_Func2;
}
void DeleteVTables()
{
int i;
for (i = 0; i < NUM_CLASSES; ++i)
{
free(g_allVTables[i]);
}
free(g_allVTables);
g_allVTables = NULL;
}
int main()
{
Base* pBase;
// In C++, the vtables are built by the compiler and laid out in the executable already. In C,
// we need to allocate and build them at runtime.
InitVTables();
pBase = NewBase(); // pBase = new Base();
((Func1)pBase->vtable[VFUNC_FUNC1])(); // pBase->Func1();
((Func2)pBase->vtable[VFUNC_FUNC2])(11); // pBase->Func2(11);
DeleteBase(pBase); // delete pBase;
printf("\n");
pBase = (Base*)NewChildOne(); // pBase = new ChildOne();
((Func1)pBase->vtable[VFUNC_FUNC1])(); // pBase->Func1();
((Func2)pBase->vtable[VFUNC_FUNC2])(12); // pBase->Func2(12);
DeleteBase(pBase); // delete pBase;
printf("\n");
pBase = (Base*)NewChildTwo(); // pBase = new ChildTwo();
((Func1)pBase->vtable[VFUNC_FUNC1])(); // pBase->Func1();
((Func2)pBase->vtable[VFUNC_FUNC2])(13); // pBase->Func2(13);
DeleteBase(pBase); // delete pBase;
DeleteVTables();
}