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 | 34 | _new(const CCLASS _class, ...) |
| 35 | 35 | { |
| 36 | 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 | 42 | if (class->__construct) { |
| 42 | 43 | va_list params; |
| ... | ... | @@ -53,9 +54,10 @@ void * |
| 53 | 54 | _newFromJson(const CCLASS _class, struct json_object * json) |
| 54 | 55 | { |
| 55 | 56 | const CCLASS class = _class; |
| 56 | - void * object = calloc(1, class->size); | |
| 57 | + void * object = calloc(1, class->size + sizeof(CCLASS)); | |
| 57 | 58 | |
| 58 | 59 | * (const struct _CCLASS **) object = class; |
| 60 | + object += sizeof(CCLASS); | |
| 59 | 61 | |
| 60 | 62 | if (class->__jsonConst && json) { |
| 61 | 63 | class->__jsonConst(object, json); |
| ... | ... | @@ -67,7 +69,7 @@ _newFromJson(const CCLASS _class, struct json_object * json) |
| 67 | 69 | int |
| 68 | 70 | _instanceOf(const CCLASS _class, void * _object) |
| 69 | 71 | { |
| 70 | - const CCLASS * class = _object; | |
| 72 | + const CCLASS * class = _object - sizeof(CCLASS); | |
| 71 | 73 | |
| 72 | 74 | return (class && _class == *class); |
| 73 | 75 | } |
| ... | ... | @@ -76,7 +78,7 @@ _instanceOf(const CCLASS _class, void * _object) |
| 76 | 78 | void |
| 77 | 79 | clear(void * _object) |
| 78 | 80 | { |
| 79 | - const CCLASS * class = _object; | |
| 81 | + const CCLASS * class = _object - sizeof(CCLASS); | |
| 80 | 82 | |
| 81 | 83 | if (_object && *class && (*class)->__clear) { |
| 82 | 84 | (*class)->__clear(_object); |
| ... | ... | @@ -86,20 +88,20 @@ clear(void * _object) |
| 86 | 88 | void |
| 87 | 89 | delete(void * _object) |
| 88 | 90 | { |
| 89 | - const CCLASS * class = *(void**)_object; | |
| 91 | + const CCLASS * class = (*(void**)_object) - sizeof(CCLASS); | |
| 90 | 92 | |
| 91 | 93 | if (*(void**)_object && *class && (*class)->__destruct) { |
| 92 | 94 | (*class)->__destruct(*(void**)_object); |
| 93 | 95 | } |
| 94 | 96 | |
| 95 | - free(*(void**)_object); | |
| 97 | + free((void *)class); | |
| 96 | 98 | *(void**)_object = NULL; |
| 97 | 99 | } |
| 98 | 100 | |
| 99 | 101 | void |
| 100 | 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 | 106 | if (_object && *class && (*class)->__toJson) { |
| 105 | 107 | (*class)->__toJson(_object, json); |
| ... | ... | @@ -109,7 +111,7 @@ toJson(void * _object, struct json_object ** json) |
| 109 | 111 | int |
| 110 | 112 | isObject(void * _object) |
| 111 | 113 | { |
| 112 | - const CCLASS * class = _object; | |
| 114 | + const CCLASS * class = _object - sizeof(CCLASS); | |
| 113 | 115 | |
| 114 | 116 | return (_object && (*class) && CCLASS_MAGIC == (*class)->magic); |
| 115 | 117 | } | ... | ... |
Please
register
or
login
to post a comment