-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.c
73 lines (63 loc) · 1.54 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
#include "object.h"
#include "object.r"
#include "def.h"
#include "basic.h"
/* methods prototypes */
private_fun char* Object_toString(void* obj);
private_fun void* Object_clone(void* obj);
private_fun boolean Object_equals(void* obj, void* obj2);
void delete(void* obj)
{
if (!obj) return;
if (!def_hashtable_is_object(DEF_GLOBAL_HASHTABLE, obj)) return;
Object** this = obj;
(*this)->o_IF->dtor(obj);
}
/* protected functions */
Object* Object_ctor(const char* info, void* link)
{
if (!info) return NULL;
if (!link) return NULL;
Object* this;
ObjectIF* thisIF;
o_Object* self;
MALLOC(Object, 1, this);
_MALLOC(ObjectIF, 1, thisIF);
_MALLOC(o_Object, 1, self);
self->sub = link;
self->toString = basic_strcpy(info);
this->self = self;
this->o_IF = thisIF;
thisIF->clone = &Object_clone;
thisIF->toString = &Object_toString;
thisIF->equals = &Object_equals;
thisIF->dtor = &Object_dtor;
return this;
}
/* Object methods */
void Object_dtor(void* obj)
{
CAST_OBJECT(obj, , );
_FREE(self->toString);
_FREE(self);
_FREE(this->o_IF);
FREE(this);
}
private_fun char* Object_toString(void* obj)
{
CAST_OBJECT(obj, NULL, );
return self->toString;
}
private_fun void* Object_clone(void* obj)
{
CAST_OBJECT(obj, NULL, );
return Object_ctor(self->toString, self->sub);
}
private_fun boolean Object_equals(void* obj, void* obj2)
{
CAST_OBJECT(obj, false, );
CAST_OBJECT(obj2, false, 1);
if (basic_strcmp(def_hashtable_get_type(DEF_GLOBAL_HASHTABLE, obj),
def_hashtable_get_type(DEF_GLOBAL_HASHTABLE, obj2))) return true;
return false;
}