summaryrefslogtreecommitdiff
path: root/src/cobj.h
blob: 328a6aa8be276516bc15f673e93efd5bf4743350 (plain) (blame)
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
#if defined(__COBJ_PUB__) && defined(__COBJ_DECL__) || defined(__COBJ_IMPL__) && defined(__COBJ_H__)
#	undef Atom
#	undef Attr
#	undef Call
#	undef Class
#	undef DefaultDelete
#	undef Define
#	undef Del
#	undef Func
#	undef GET
#	undef Get
#	undef List
#	undef New
#	undef Prop
#	undef Self
#	undef Set
#	undef TYPE
#	undef UNUSED
#	undef __COBJ_DECL__
#	undef __COBJ_PUB__
#endif


#if ! defined(__COBJ_H__) && ! defined(__COBJ_IMPL__) && ! defined(__COBJ_DECL__)
#	define Get(instance, field, ...) \
			(instance)->get_##field(instance, ## __VA_ARGS__)
#	define Set(instance, field, ...) \
			(instance)->set_##field(instance, ## __VA_ARGS__)
#	define Call(instance, fun, ...) \
			(instance)->fun(instance, ## __VA_ARGS__)
#	define New(klass, ...) \
			klass##_new(__VA_ARGS__)
#	define Del(instance) \
			(instance)->destroy((instance)), instance = NULL
#elif defined(__COBJ_DECL__)
#	define Class(klass, data, ...) \
		typedef struct __##klass##_impl * klass; \
		klass klass ## _new(__VA_ARGS__); \
		void  klass ## _delete(klass); \
		struct __##klass##_impl data

#	define List(klass, field, type, dim, ...) \
			type _property_ ## field dim; \
			type* (*get_ ## field)(klass, ## __VA_ARGS__); \
			bool  (*set_ ## field)(klass, ## __VA_ARGS__, const type * const)

#	define Atom(klass, field, type, ...) \
			type _property_ ## field; \
			type (*get_ ## field)(klass, ## __VA_ARGS__);\
			bool (*set_ ## field)(klass, ## __VA_ARGS__, type)

#	define Func(klass, name, type, ...) \
		type (*name)(klass, ## __VA_ARGS__)

#	define Del(klass) \
		void (*destroy)(klass)
#elif defined(__COBJ_IMPL__)
#	define UNUSED(arg) while(0 && (arg))
#	define TYPE(klass) \
					struct __ ## klass ## _impl
#	define Get(klass, field, type, body, ...) \
					type klass##_get_##field(klass self, ## __VA_ARGS__) \
					body
#	define Set(klass, field, type, body, ...) \
					bool klass##_set_##field(klass self, ## __VA_ARGS__, type value) \
					body
#	define Def(klass, name, type, body, ...) \
					type klass##_##name(klass self, ## __VA_ARGS__) \
					body
#	define New(klass, body, ...) \
				klass klass ## _new(__VA_ARGS__) { \
					klass self = (klass)malloc(sizeof(TYPE(klass))); \
					if(self != NULL) { \
						self->destroy = (& klass ## _delete); \
						body; \
					} \
					return self; \
				}
#	define Del(klass, body, ...) \
				void   klass ## _delete(klass self, ## __VA_ARGS__) \
				body

#	define Prop(klass, name) \
			(self->get_ ## name) = (& klass ## _ ## get_ ## name), \
			(self->set_ ## name) = (& klass ## _ ## set_ ## name)

#	define Func(klass, name) \
			(self->name) = (& klass ## _ ## name)

#	define Self(name) \
			self->_property_ ## name
#	define Attr(instance, name) \
			(instance)->_property_ ## name
#	define Call(instance, fun, ...) \
			(instance)->fun(instance, ## __VA_ARGS__)
#	define DefaultDelete { \
		if(self != NULL) free(self); \
	}
#endif