-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.h
60 lines (47 loc) · 1.8 KB
/
new.h
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
#ifndef __NEW_H__
#define __NEW_H__
#include <stddef.h>
#include <stdarg.h>
/* Not entirely certain of what the best syntax is
* Either vtable(struct Vtable, object)->func()
* Or something more involved giving access to the entire class.
*/
//#define vtable(type, object) ((struct type *) (object->class->vtable))
#define vtable(type, object) ((struct type *) (((struct Object *) object)->vtable))
// #define vtable1(type, object) ((struct type *) object) no.
typedef const struct Class* Class_t;
struct Object {
Class_t class;
// This is loosely defined as an "array" of function pointers
// with variable length parameters. Theese are defined
// in the object headers.
// To be precise it's a struct, that is then treated here
// as an array due to it being of an unknow quantity etc.
void (**vtable) (void * self, ...);
};
typedef struct Object Object_t;
// Object constructor, takes class and arguments
typedef void *(*constructor_fun) (void * self, va_list * app);
// Object destructor
typedef void *(*destructor_fun) (void * self);
// Clone function
typedef void *(*clone_fun) (const void * self);
// Compare function, think Java equals, returns 0 if equal, any non-zero
// integer should be looked in the object documentation
typedef int (*differ_fun) (const void * a, const void * b);
// All object are required to export this class,
// especially size, constructor and destructor.
struct Class {
size_t size; // Size of object
constructor_fun constructor;
destructor_fun destructor;
clone_fun clone;
differ_fun differ;
};
// Creates a new object based on it's class
void * new(Class_t class, ...);
// Deletes an object
void delete(void * self);
// Gets the size of an object (based on it's class)
size_t sizeOf(const void * _self);
#endif