forked from UniversesAurora/OOP-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoo_lan.h
executable file
·93 lines (65 loc) · 1.83 KB
/
oo_lan.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
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
//
// Created by Miguel Duarte on 5/12/2019.
//
#ifndef OOP_C_OO_LAN_H
#define OOP_C_OO_LAN_H
//#define debug
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#define static_member(name) (* name)
#define static_init(obj_mem, real_mem) (obj_mem = &(real_mem))
#define static_get(static_p) (*(static_p))
#define declare_cons(class) \
class new_##class(); \
void del_##class(class)
#define class_declaration(class) \
typedef struct __##class _##class,*class; \
struct __##class \
{ \
__##class##_content__ \
}; \
declare_cons(class);
#define new(class) ((void *)new_##class())
#define del(class, obj) (del_##class((class)obj))
#define func(func_name) (* func_name)
static inline void* __create_obj(size_t size)
{
void* new_obj = malloc(size);
if (new_obj)
memset(new_obj, 0, size);
return new_obj;
}
static inline void* __create_inher(void* ori, size_t size, size_t p_size, void (* p_del)(void* obj))
{
void* new_obj = realloc(ori, size);
if (new_obj)
memset(new_obj+p_size, 0, size-p_size);
else
p_del(ori);
return new_obj;
}
#define create_obj(class) \
__create_obj(sizeof(_##class))
#define create_inher(parent_class, class) \
__create_inher(new(parent_class), sizeof(_##class), sizeof(_##parent_class), (void(*)(void *))del_##parent_class)
#define class static
#define del_obj(obj) \
do \
{ \
if (obj) \
free(obj); \
} while(0)
#define deconstruct_void(class) \
void del_##class(class obj) \
{ \
del_obj(obj); \
}
#ifdef debug
#define log_cons(class) printf("Exec constructor of class " #class ".\n")
#define log_deco(class) printf("Exec deconstructor of class " #class ".\n")
#else
#define log_cons(class)
#define log_deco(class)
#endif
#endif //OOP_C_OO_LAN_H