-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.c
174 lines (153 loc) · 4.53 KB
/
object.c
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
#include "object.h"
#include "object.r"
#include "typecheck.h"
void *new_o(void *const _self, ...)
{
struct object_vt_s *const self = _self;
if(!self)
return 0;
if(!self->safecheck || self->safecheck != passed)
exit_error("new_o", ENOTANOBJ);
void *o_ptr = calloc(1, self->size);
if(o_ptr == 0 && errno == ENOMEM)
exit_error("new_o", ERR_NO);
*(struct object_vt_s**)o_ptr = self;
((struct object_class_s *)o_ptr)->safecheck = passed;
if(!self->ctor)
return o_ptr;
va_list ap;
va_start(ap, _self);
self->ctor(o_ptr, &ap);
va_end(ap);
return o_ptr;
}
void *delete_o(void ** _self)
{
if(!_self) { return 0; }
struct object_class_s * self = *_self;
if(CHECK_IF_OBJ(self))
exit_error("delete_o", ENOTANOBJ);
if(self->vt->dtor)
self->vt->dtor(_self);
free(self);
*_self = 0;
return _self;
}
void print_o(const void *const _self)
{
const struct object_class_s *const self = _self;
if(CHECK_IF_OBJ(self) || !self->vt->print)
exit_error("print_o", EWRONGOBJ);
self->vt->print(_self);
}
void *copy_o(const void *const _self)
{
const struct object_class_s *const self = _self;
if(CHECK_IF_OBJ(self) || !self->vt->copy)
exit_error("copy_o", EWRONGOBJ);
return self->vt->copy(_self);
}
int compare_o(const void *const _self, const void *const _other)
{
const struct object_class_s *const self = _self;
const struct object_class_s *const other = _other;
if (CHECK_IF_OBJ(self) || !self->vt->compare ||
CHECK_IF_OBJ(other) || !other->vt->compare) {
exit_error("compare_o", EWRONGOBJ);
}
if (self->vt != other->vt) {
exit_error("compare_o", EDIFFOBJ);
}
return self->vt->compare(_self, _other);
}
void *add_o(const void *const _self, const void *const _other)
{
const struct object_class_s *const self = _self;
const struct object_class_s *const other = _other;
if (CHECK_IF_OBJ(self) || !self->vt->add ||
CHECK_IF_OBJ(other) || !other->vt->add) {
exit_error("add_o", EWRONGOBJ);
}
if (self->vt != other->vt) {
exit_error("add_o", EDIFFOBJ);
}
return self->vt->add(_self, _other);
}
void *sub_o(const void *const _self, const void *const _other)
{
const struct object_class_s *const self = _self;
const struct object_class_s *const other = _other;
if (CHECK_IF_OBJ(self) || !self->vt->sub ||
CHECK_IF_OBJ(other) || !other->vt->sub) {
exit_error("sub_o", EWRONGOBJ);
}
if (self->vt != other->vt) {
exit_error("sub_o", EDIFFOBJ);
}
return self->vt->sub(_self, _other);
}
void *mult_o(const void *const _self, const void *const _other)
{
const struct object_class_s *const self = _self;
const struct object_class_s *const other = _other;
if (CHECK_IF_OBJ(self) || !self->vt->mult ||
CHECK_IF_OBJ(other) || !other->vt->mult) {
exit_error("mult_o", EWRONGOBJ);
}
if (self->vt != other->vt) {
exit_error("mult_o", EDIFFOBJ);
}
return self->vt->mult(_self, _other);
}
void *div_o(const void *const _self, const void *const _other)
{
const struct object_class_s *const self = _self;
const struct object_class_s *const other = _other;
if (CHECK_IF_OBJ(self) || !self->vt->div ||
CHECK_IF_OBJ(other) || !other->vt->div) {
exit_error("div_o", EWRONGOBJ);
}
if (self->vt != other->vt) {
exit_error("div_o", EDIFFOBJ);
}
return self->vt->div(_self, _other);
}
size_t get_size_o(const void *const _self)
{
const struct object_class_s *const self = _self;
if (CHECK_IF_OBJ(self) || !self->vt->size)
exit_error("get_size_o", EWRONGOBJ);
return self->vt->size;
}
void *object_ctor(void *const _self, va_list *app)
{
return _self;
}
void *object_dtor(void *_self)
{
return 0;
}
void object_print(const void *const _self)
{
fputs("\n", stdout);
}
void *object_copy(const void *const _self)
{
return new_o(object);
}
int object_compare(const void *const _self, const void *const _other)
{
return 0;
}
void *object_add(const void *const _self, const void *const _other)
{ return new_o(object); }
void *object_sub(const void *const _self, const void *const _other)
{ return new_o(object); }
void *object_mult(const void *const _self, const void *const _other)
{ return new_o(object); }
void *object_div(const void *const _self, const void *const _other)
{ return new_o(object); }
struct object_vt_s object_vt = {
OBJECT_VT_INIT
};
void *const object = &object_vt;