Commit 5b8db017e885e14b57d0a0136326b843d6396205
1 parent
f00abcf6
change cclass so that the internal structure is no longer visible by the rest of the code
Showing
1 changed file
with
11 additions
and
9 deletions
@@ -34,9 +34,10 @@ void * | @@ -34,9 +34,10 @@ void * | ||
34 | _new(const CCLASS _class, ...) | 34 | _new(const CCLASS _class, ...) |
35 | { | 35 | { |
36 | const CCLASS class = _class; | 36 | const CCLASS class = _class; |
37 | - void * object = calloc(1, class->size); | 37 | + void * object = calloc(1, class->size + sizeof(CCLASS)); |
38 | 38 | ||
39 | - * (const struct _CCLASS **) object = class; | 39 | + * (const struct _CCLASS **)object = class; |
40 | + object += sizeof(CCLASS); | ||
40 | 41 | ||
41 | if (class->__construct) { | 42 | if (class->__construct) { |
42 | va_list params; | 43 | va_list params; |
@@ -53,9 +54,10 @@ void * | @@ -53,9 +54,10 @@ void * | ||
53 | _newFromJson(const CCLASS _class, struct json_object * json) | 54 | _newFromJson(const CCLASS _class, struct json_object * json) |
54 | { | 55 | { |
55 | const CCLASS class = _class; | 56 | const CCLASS class = _class; |
56 | - void * object = calloc(1, class->size); | 57 | + void * object = calloc(1, class->size + sizeof(CCLASS)); |
57 | 58 | ||
58 | * (const struct _CCLASS **) object = class; | 59 | * (const struct _CCLASS **) object = class; |
60 | + object += sizeof(CCLASS); | ||
59 | 61 | ||
60 | if (class->__jsonConst && json) { | 62 | if (class->__jsonConst && json) { |
61 | class->__jsonConst(object, json); | 63 | class->__jsonConst(object, json); |
@@ -67,7 +69,7 @@ _newFromJson(const CCLASS _class, struct json_object * json) | @@ -67,7 +69,7 @@ _newFromJson(const CCLASS _class, struct json_object * json) | ||
67 | int | 69 | int |
68 | _instanceOf(const CCLASS _class, void * _object) | 70 | _instanceOf(const CCLASS _class, void * _object) |
69 | { | 71 | { |
70 | - const CCLASS * class = _object; | 72 | + const CCLASS * class = _object - sizeof(CCLASS); |
71 | 73 | ||
72 | return (class && _class == *class); | 74 | return (class && _class == *class); |
73 | } | 75 | } |
@@ -76,7 +78,7 @@ _instanceOf(const CCLASS _class, void * _object) | @@ -76,7 +78,7 @@ _instanceOf(const CCLASS _class, void * _object) | ||
76 | void | 78 | void |
77 | clear(void * _object) | 79 | clear(void * _object) |
78 | { | 80 | { |
79 | - const CCLASS * class = _object; | 81 | + const CCLASS * class = _object - sizeof(CCLASS); |
80 | 82 | ||
81 | if (_object && *class && (*class)->__clear) { | 83 | if (_object && *class && (*class)->__clear) { |
82 | (*class)->__clear(_object); | 84 | (*class)->__clear(_object); |
@@ -86,20 +88,20 @@ clear(void * _object) | @@ -86,20 +88,20 @@ clear(void * _object) | ||
86 | void | 88 | void |
87 | delete(void * _object) | 89 | delete(void * _object) |
88 | { | 90 | { |
89 | - const CCLASS * class = *(void**)_object; | 91 | + const CCLASS * class = (*(void**)_object) - sizeof(CCLASS); |
90 | 92 | ||
91 | if (*(void**)_object && *class && (*class)->__destruct) { | 93 | if (*(void**)_object && *class && (*class)->__destruct) { |
92 | (*class)->__destruct(*(void**)_object); | 94 | (*class)->__destruct(*(void**)_object); |
93 | } | 95 | } |
94 | 96 | ||
95 | - free(*(void**)_object); | 97 | + free((void *)class); |
96 | *(void**)_object = NULL; | 98 | *(void**)_object = NULL; |
97 | } | 99 | } |
98 | 100 | ||
99 | void | 101 | void |
100 | toJson(void * _object, struct json_object ** json) | 102 | toJson(void * _object, struct json_object ** json) |
101 | { | 103 | { |
102 | - const CCLASS * class = _object; | 104 | + const CCLASS * class = _object - sizeof(CCLASS); |
103 | 105 | ||
104 | if (_object && *class && (*class)->__toJson) { | 106 | if (_object && *class && (*class)->__toJson) { |
105 | (*class)->__toJson(_object, json); | 107 | (*class)->__toJson(_object, json); |
@@ -109,7 +111,7 @@ toJson(void * _object, struct json_object ** json) | @@ -109,7 +111,7 @@ toJson(void * _object, struct json_object ** json) | ||
109 | int | 111 | int |
110 | isObject(void * _object) | 112 | isObject(void * _object) |
111 | { | 113 | { |
112 | - const CCLASS * class = _object; | 114 | + const CCLASS * class = _object - sizeof(CCLASS); |
113 | 115 | ||
114 | return (_object && (*class) && CCLASS_MAGIC == (*class)->magic); | 116 | return (_object && (*class) && CCLASS_MAGIC == (*class)->magic); |
115 | } | 117 | } |
Please
register
or
login
to post a comment