Commit 9a28fc52db1178258be0dc9b136a4c64194fd1ac
1 parent
e090c797
added more convenience macros for classes and used them in the classes
Showing
14 changed files
with
123 additions
and
141 deletions
| @@ -70,7 +70,7 @@ main(int argc, char * argv[]) | @@ -70,7 +70,7 @@ main(int argc, char * argv[]) | ||
| 70 | sizeof(DYNTYPE_HASH), | 70 | sizeof(DYNTYPE_HASH), |
| 71 | data)); | 71 | data)); |
| 72 | 72 | ||
| 73 | - json = toJson(packet); | 73 | + toJson(packet, &json); |
| 74 | json_str = json_object_to_json_string(json); | 74 | json_str = json_object_to_json_string(json); |
| 75 | length = strlen(json_str); | 75 | length = strlen(json_str); |
| 76 | 76 |
| @@ -33,41 +33,48 @@ | @@ -33,41 +33,48 @@ | ||
| 33 | #define ENDC(_class) } * _class; \ | 33 | #define ENDC(_class) } * _class; \ |
| 34 | extern const _CCLASS const __##_class; | 34 | extern const _CCLASS const __##_class; |
| 35 | 35 | ||
| 36 | -#define INIT_CCLASS(class, jsonConst, toJson) \ | ||
| 37 | - static const struct CCLASS _class = { \ | ||
| 38 | - CCLASS_MAGIC, \ | ||
| 39 | - sizeof(struct _##class), \ | ||
| 40 | - (ctor)__construct, \ | ||
| 41 | - (jCtor)jsonConst, \ | ||
| 42 | - (dtor)__destruct, \ | ||
| 43 | - (jTo)toJson \ | ||
| 44 | - }; const _CCLASS const __##class = (const _CCLASS const)&_class | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | typedef void (* ctor)(void *, va_list *); | 36 | typedef void (* ctor)(void *, va_list *); |
| 49 | typedef void (* dtor)(void *); | 37 | typedef void (* dtor)(void *); |
| 50 | typedef void (* jCtor)(void *, struct json_object *); | 38 | typedef void (* jCtor)(void *, struct json_object *); |
| 51 | -typedef struct json_object * (* jTo)(void *); | 39 | +typedef void (* jTo)(void *, struct json_object **); |
| 52 | 40 | ||
| 53 | 41 | ||
| 54 | typedef struct CCLASS { | 42 | typedef struct CCLASS { |
| 55 | const int magic; | 43 | const int magic; |
| 56 | size_t size; | 44 | size_t size; |
| 57 | void (* __construct)(void * _this, va_list * params); | 45 | void (* __construct)(void * _this, va_list * params); |
| 58 | - void (* __jsonConst)(void * _this, struct json_object * json); | ||
| 59 | void (* __destruct)(void * _this); | 46 | void (* __destruct)(void * _this); |
| 60 | - struct json_object * (* __toJson)(void * _this); | 47 | + void (* __jsonConst)(void * _this, struct json_object * json); |
| 48 | + void (* __toJson)(void * _this, struct json_object ** json); | ||
| 61 | } * _CCLASS; | 49 | } * _CCLASS; |
| 62 | #define CCLASS_PTR_SIZE sizeof(struct CCLASS *) | 50 | #define CCLASS_PTR_SIZE sizeof(struct CCLASS *) |
| 63 | #define CCLASS_SIZE sizeof(struct CCLASS) | 51 | #define CCLASS_SIZE sizeof(struct CCLASS) |
| 64 | 52 | ||
| 65 | -void * _new(const _CCLASS _class, ...); | ||
| 66 | -void * _newFromJson(const _CCLASS _class, struct json_object * json); | ||
| 67 | -void delete(void * _object); | ||
| 68 | -struct json_object * toJson(void * _object); | ||
| 69 | -int isObject(void * _object); | ||
| 70 | -int _instanceOf(const _CCLASS _class, void * _object); | 53 | +#define __construct(class) static void __construct(class _this, va_list * params) |
| 54 | +#define __destruct(class) static void __destruct(class _this) | ||
| 55 | +#define __jsonConst(class) static void __jsonConst(class _this, struct json_object * json) | ||
| 56 | +#define __toJson(class) static void __toJson(class _this, struct json_object ** json) | ||
| 57 | + | ||
| 58 | +#define INIT_CCLASS(class) \ | ||
| 59 | + __construct(class); \ | ||
| 60 | + __destruct(class); \ | ||
| 61 | + __jsonConst(class); \ | ||
| 62 | + __toJson(class); \ | ||
| 63 | + static const struct CCLASS _class = { \ | ||
| 64 | + CCLASS_MAGIC, \ | ||
| 65 | + sizeof(struct _##class), \ | ||
| 66 | + (ctor)__construct, \ | ||
| 67 | + (dtor)__destruct, \ | ||
| 68 | + (jCtor)__jsonConst, \ | ||
| 69 | + (jTo)__toJson \ | ||
| 70 | + }; const _CCLASS __##class = (const _CCLASS)&_class | ||
| 71 | + | ||
| 72 | +void * _new(const _CCLASS _class, ...); | ||
| 73 | +void * _newFromJson(const _CCLASS _class, struct json_object * json); | ||
| 74 | +void delete(void * _object); | ||
| 75 | +void toJson(void * _object, struct json_object ** json); | ||
| 76 | +int isObject(void * _object); | ||
| 77 | +int _instanceOf(const _CCLASS _class, void * _object); | ||
| 71 | 78 | ||
| 72 | #define new(class, ...) _new((__##class), __VA_ARGS__) | 79 | #define new(class, ...) _new((__##class), __VA_ARGS__) |
| 73 | #define newFromJson(class, json) _newFromJson((__##class), (json)) | 80 | #define newFromJson(class, json) _newFromJson((__##class), (json)) |
| @@ -16,17 +16,16 @@ | @@ -16,17 +16,16 @@ | ||
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ | 17 | */ |
| 18 | #ifndef __DYNTYPE_H__ | 18 | #ifndef __DYNTYPE_H__ |
| 19 | + | ||
| 20 | +#ifndef __DYNTYPE_HASH_H__ | ||
| 21 | +#include "token/dyntype/hash.h" | ||
| 22 | +#endif//__DYNTYPE_HASH_H__ | ||
| 23 | + | ||
| 19 | #define __DYNTYPE_H__ | 24 | #define __DYNTYPE_H__ |
| 20 | 25 | ||
| 21 | #include <sys/types.h> | 26 | #include <sys/types.h> |
| 22 | - | ||
| 23 | #include "token/cclass.h" | 27 | #include "token/cclass.h" |
| 24 | 28 | ||
| 25 | -struct _DYNTYPE; | ||
| 26 | - | ||
| 27 | -#include "token/dyntype/hash.h" | ||
| 28 | - | ||
| 29 | - | ||
| 30 | enum DYNTYPE_TYPES { | 29 | enum DYNTYPE_TYPES { |
| 31 | DYNTYPE_TYPE_NULL = 0, | 30 | DYNTYPE_TYPE_NULL = 0, |
| 32 | DYNTYPE_TYPE_BOOLEAN, | 31 | DYNTYPE_TYPE_BOOLEAN, |
| @@ -51,6 +50,8 @@ CLASS(DYNTYPE) | @@ -51,6 +50,8 @@ CLASS(DYNTYPE) | ||
| 51 | } data; | 50 | } data; |
| 52 | ENDC(DYNTYPE) | 51 | ENDC(DYNTYPE) |
| 53 | 52 | ||
| 53 | +#include "token/dyntype/hash.h" | ||
| 54 | + | ||
| 54 | #endif//__DYNTYPE_H__ | 55 | #endif//__DYNTYPE_H__ |
| 55 | 56 | ||
| 56 | // vim: set et ts=4 sw=4: | 57 | // vim: set et ts=4 sw=4: |
| @@ -16,25 +16,33 @@ | @@ -16,25 +16,33 @@ | ||
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ | 17 | */ |
| 18 | #ifndef __DYNTYPE_HASH_H__ | 18 | #ifndef __DYNTYPE_HASH_H__ |
| 19 | -#define __DYNTYPE_HASH_H__ | ||
| 20 | 19 | ||
| 21 | #include <sys/types.h> | 20 | #include <sys/types.h> |
| 22 | - | ||
| 23 | #include "token/cclass.h" | 21 | #include "token/cclass.h" |
| 24 | -#include "token/dyntype.h" | ||
| 25 | 22 | ||
| 23 | +#ifndef __DYNTYPE_H__ | ||
| 24 | + | ||
| 25 | +struct _DYNTYPE_HASH; | ||
| 26 | +#define DYNTYPE_HASH struct _DYNTYPE_HASH * | ||
| 27 | + | ||
| 28 | +#else | ||
| 29 | + | ||
| 30 | +#undef DYNTYPE_HASH | ||
| 31 | +#define __DYNTYPE_HASH_H__ | ||
| 26 | 32 | ||
| 27 | CLASS(DYNTYPE_HASH) | 33 | CLASS(DYNTYPE_HASH) |
| 28 | char ** keys; | 34 | char ** keys; |
| 29 | - struct _DYNTYPE ** values; | 35 | + DYNTYPE * values; |
| 30 | size_t size; | 36 | size_t size; |
| 31 | size_t used; | 37 | size_t used; |
| 32 | ENDC(DYNTYPE_HASH) | 38 | ENDC(DYNTYPE_HASH) |
| 33 | 39 | ||
| 34 | 40 | ||
| 35 | void dyntype_hash_set(DYNTYPE_HASH _this, const char * key, struct _DYNTYPE * value); | 41 | void dyntype_hash_set(DYNTYPE_HASH _this, const char * key, struct _DYNTYPE * value); |
| 36 | -struct _DYNTYPE * dyntype_hash_get(DYNTYPE_HASH _this, const char * key); | ||
| 37 | -struct _DYNTYPE * dyntype_hash_del(DYNTYPE_HASH _this, const char * key); | 42 | +DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key); |
| 43 | +void dyntype_hash_del(DYNTYPE_HASH _this, const char * key); | ||
| 44 | + | ||
| 45 | +#endif | ||
| 38 | 46 | ||
| 39 | #endif//__DYNTYPE_HASH_H__ | 47 | #endif//__DYNTYPE_HASH_H__ |
| 40 | 48 |
| @@ -22,6 +22,10 @@ | @@ -22,6 +22,10 @@ | ||
| 22 | 22 | ||
| 23 | #include "token/cclass.h" | 23 | #include "token/cclass.h" |
| 24 | 24 | ||
| 25 | +#undef __construct | ||
| 26 | +#undef __destruct | ||
| 27 | +#undef __jsonConst | ||
| 28 | +#undef __toJson | ||
| 25 | 29 | ||
| 26 | void * | 30 | void * |
| 27 | _new(const _CCLASS _class, ...) | 31 | _new(const _CCLASS _class, ...) |
| @@ -70,16 +74,14 @@ delete(void * _object) | @@ -70,16 +74,14 @@ delete(void * _object) | ||
| 70 | *(void**)_object = NULL; | 74 | *(void**)_object = NULL; |
| 71 | } | 75 | } |
| 72 | 76 | ||
| 73 | -struct json_object * | ||
| 74 | -toJson(void * _object) | 77 | +void |
| 78 | +toJson(void * _object, struct json_object ** json) | ||
| 75 | { | 79 | { |
| 76 | const struct CCLASS ** class = _object; | 80 | const struct CCLASS ** class = _object; |
| 77 | 81 | ||
| 78 | if (_object && *class && (*class)->__toJson) { | 82 | if (_object && *class && (*class)->__toJson) { |
| 79 | - return (*class)->__toJson(_object); | 83 | + (*class)->__toJson(_object, json); |
| 80 | } | 84 | } |
| 81 | - | ||
| 82 | - return NULL; | ||
| 83 | } | 85 | } |
| 84 | 86 | ||
| 85 | int | 87 | int |
| @@ -28,9 +28,9 @@ | @@ -28,9 +28,9 @@ | ||
| 28 | #include "token/crypt.h" | 28 | #include "token/crypt.h" |
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | -static | ||
| 32 | -void | ||
| 33 | -__construct(CRYPT _this, va_list * params) | 31 | +INIT_CCLASS(CRYPT); |
| 32 | + | ||
| 33 | +__construct(CRYPT) | ||
| 34 | { | 34 | { |
| 35 | _this->algorithm = va_arg(* params, const char * const); | 35 | _this->algorithm = va_arg(* params, const char * const); |
| 36 | _this->mode = va_arg(* params, const char * const); | 36 | _this->mode = va_arg(* params, const char * const); |
| @@ -45,9 +45,7 @@ __construct(CRYPT _this, va_list * params) | @@ -45,9 +45,7 @@ __construct(CRYPT _this, va_list * params) | ||
| 45 | _this->keysize = mcrypt_enc_get_key_size(_this->mcrypt); | 45 | _this->keysize = mcrypt_enc_get_key_size(_this->mcrypt); |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | -static | ||
| 49 | -void | ||
| 50 | -__destruct(CRYPT _this) | 48 | +__destruct(CRYPT) |
| 51 | { | 49 | { |
| 52 | if (_this->iv) { | 50 | if (_this->iv) { |
| 53 | free(_this->iv); | 51 | free(_this->iv); |
| @@ -56,7 +54,8 @@ __destruct(CRYPT _this) | @@ -56,7 +54,8 @@ __destruct(CRYPT _this) | ||
| 56 | mcrypt_module_close(_this->mcrypt); | 54 | mcrypt_module_close(_this->mcrypt); |
| 57 | } | 55 | } |
| 58 | 56 | ||
| 59 | -INIT_CCLASS(CRYPT, NULL, NULL); | 57 | +__jsonConst(CRYPT) {} |
| 58 | +__toJson(CRYPT) {} | ||
| 60 | 59 | ||
| 61 | void * | 60 | void * |
| 62 | crypt_createIv(CRYPT _this) | 61 | crypt_createIv(CRYPT _this) |
| @@ -94,7 +93,7 @@ createKey(CRYPT _this, const char * const password) | @@ -94,7 +93,7 @@ createKey(CRYPT _this, const char * const password) | ||
| 94 | _this->keysize, | 93 | _this->keysize, |
| 95 | NULL, | 94 | NULL, |
| 96 | 0, | 95 | 0, |
| 97 | - (char *)password, // @TODO: bad karma...now this might change password. | 96 | + (mutils_word8 *)password, // @TODO: bad karma...this might change password. |
| 98 | strlen(password)); | 97 | strlen(password)); |
| 99 | 98 | ||
| 100 | return key; | 99 | return key; |
| @@ -24,9 +24,9 @@ | @@ -24,9 +24,9 @@ | ||
| 24 | #include "token/dyntype/hash.h" | 24 | #include "token/dyntype/hash.h" |
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | -static | ||
| 28 | -void | ||
| 29 | -__construct(DYNTYPE _this, va_list * params) | 27 | +INIT_CCLASS(DYNTYPE); |
| 28 | + | ||
| 29 | +__construct(DYNTYPE) | ||
| 30 | { | 30 | { |
| 31 | _this->type = va_arg(* params, enum DYNTYPE_TYPES); | 31 | _this->type = va_arg(* params, enum DYNTYPE_TYPES); |
| 32 | _this->size = va_arg(* params, size_t); | 32 | _this->size = va_arg(* params, size_t); |
| @@ -50,9 +50,7 @@ __construct(DYNTYPE _this, va_list * params) | @@ -50,9 +50,7 @@ __construct(DYNTYPE _this, va_list * params) | ||
| 50 | } | 50 | } |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | -static | ||
| 54 | -void | ||
| 55 | -__jsonConst(DYNTYPE _this, struct json_object * json) | 53 | +__jsonConst(DYNTYPE) |
| 56 | { | 54 | { |
| 57 | switch (json_object_get_type(json)) { | 55 | switch (json_object_get_type(json)) { |
| 58 | case json_type_int: | 56 | case json_type_int: |
| @@ -86,9 +84,7 @@ __jsonConst(DYNTYPE _this, struct json_object * json) | @@ -86,9 +84,7 @@ __jsonConst(DYNTYPE _this, struct json_object * json) | ||
| 86 | } | 84 | } |
| 87 | } | 85 | } |
| 88 | 86 | ||
| 89 | -static | ||
| 90 | -void | ||
| 91 | -__destruct(DYNTYPE _this) | 87 | +__destruct(DYNTYPE) |
| 92 | { | 88 | { |
| 93 | if (_this) { | 89 | if (_this) { |
| 94 | switch(_this->type) { | 90 | switch(_this->type) { |
| @@ -106,32 +102,24 @@ __destruct(DYNTYPE _this) | @@ -106,32 +102,24 @@ __destruct(DYNTYPE _this) | ||
| 106 | } | 102 | } |
| 107 | } | 103 | } |
| 108 | 104 | ||
| 109 | -static | ||
| 110 | -struct json_object * | ||
| 111 | -__toJson(DYNTYPE _this) | 105 | +__toJson(DYNTYPE) |
| 112 | { | 106 | { |
| 113 | - struct json_object * json = NULL; | ||
| 114 | - | ||
| 115 | switch(_this->type) { | 107 | switch(_this->type) { |
| 116 | case DYNTYPE_TYPE_INT: | 108 | case DYNTYPE_TYPE_INT: |
| 117 | - json = json_object_new_int((_this->data)._int); | 109 | + *json = json_object_new_int((_this->data)._int); |
| 118 | break; | 110 | break; |
| 119 | 111 | ||
| 120 | case DYNTYPE_TYPE_STRING: | 112 | case DYNTYPE_TYPE_STRING: |
| 121 | - json = json_object_new_string((_this->data)._string); | 113 | + *json = json_object_new_string((_this->data)._string); |
| 122 | break; | 114 | break; |
| 123 | 115 | ||
| 124 | case DYNTYPE_TYPE_HASH: | 116 | case DYNTYPE_TYPE_HASH: |
| 125 | - json = toJson((_this->data)._hash); | 117 | + toJson((_this->data)._hash, json); |
| 126 | break; | 118 | break; |
| 127 | 119 | ||
| 128 | default: | 120 | default: |
| 129 | - json = NULL; | 121 | + *json = NULL; |
| 130 | } | 122 | } |
| 131 | - | ||
| 132 | - return json; | ||
| 133 | } | 123 | } |
| 134 | 124 | ||
| 135 | -INIT_CCLASS(DYNTYPE, __jsonConst, __toJson); | ||
| 136 | - | ||
| 137 | // vim: set et ts=4 sw=4: | 125 | // vim: set et ts=4 sw=4: |
| @@ -29,18 +29,17 @@ | @@ -29,18 +29,17 @@ | ||
| 29 | 29 | ||
| 30 | static void _updateHashSize(DYNTYPE_HASH _this); | 30 | static void _updateHashSize(DYNTYPE_HASH _this); |
| 31 | 31 | ||
| 32 | -static | ||
| 33 | -void | ||
| 34 | -__construct(DYNTYPE_HASH _this, va_list * params) | 32 | +INIT_CCLASS(DYNTYPE_HASH); |
| 33 | + | ||
| 34 | +__construct(DYNTYPE_HASH) | ||
| 35 | { | 35 | { |
| 36 | _this->size = 0; | 36 | _this->size = 0; |
| 37 | _this->used = 0; | 37 | _this->used = 0; |
| 38 | _updateHashSize(_this); | 38 | _updateHashSize(_this); |
| 39 | } | 39 | } |
| 40 | +#undef __construct | ||
| 40 | 41 | ||
| 41 | -static | ||
| 42 | -void | ||
| 43 | -__jsonConst(DYNTYPE_HASH _this, struct json_object * json) | 42 | +__jsonConst(DYNTYPE_HASH) |
| 44 | { | 43 | { |
| 45 | __construct(_this, NULL); | 44 | __construct(_this, NULL); |
| 46 | 45 | ||
| @@ -53,9 +52,7 @@ __jsonConst(DYNTYPE_HASH _this, struct json_object * json) | @@ -53,9 +52,7 @@ __jsonConst(DYNTYPE_HASH _this, struct json_object * json) | ||
| 53 | } | 52 | } |
| 54 | } | 53 | } |
| 55 | 54 | ||
| 56 | -static | ||
| 57 | -void | ||
| 58 | -__destruct(DYNTYPE_HASH _this) | 55 | +__destruct(DYNTYPE_HASH) |
| 59 | { | 56 | { |
| 60 | size_t index; | 57 | size_t index; |
| 61 | 58 | ||
| @@ -67,24 +64,19 @@ __destruct(DYNTYPE_HASH _this) | @@ -67,24 +64,19 @@ __destruct(DYNTYPE_HASH _this) | ||
| 67 | free(_this->values); | 64 | free(_this->values); |
| 68 | } | 65 | } |
| 69 | 66 | ||
| 70 | -static | ||
| 71 | -struct json_object * | ||
| 72 | -__toJson(DYNTYPE_HASH _this) | 67 | +__toJson(DYNTYPE_HASH) |
| 73 | { | 68 | { |
| 74 | size_t index; | 69 | size_t index; |
| 75 | - struct json_object * json = json_object_new_object(); | 70 | + *json = json_object_new_object(); |
| 76 | 71 | ||
| 77 | for (index = 0; index < _this->used; index ++) { | 72 | for (index = 0; index < _this->used; index ++) { |
| 78 | - json_object_object_add( | ||
| 79 | - json, | ||
| 80 | - _this->keys[index], | ||
| 81 | - toJson(_this->values[index])); | ||
| 82 | - } | 73 | + struct json_object * values; |
| 83 | 74 | ||
| 84 | - return json; | ||
| 85 | -} | 75 | + toJson(_this->values[index], &values); |
| 86 | 76 | ||
| 87 | -INIT_CCLASS(DYNTYPE_HASH, __jsonConst, __toJson); | 77 | + json_object_object_add(*json, _this->keys[index], values); |
| 78 | + } | ||
| 79 | +} | ||
| 88 | 80 | ||
| 89 | static | 81 | static |
| 90 | void | 82 | void |
| @@ -137,7 +129,8 @@ dyntype_hash_set(DYNTYPE_HASH _this, const char * key, DYNTYPE value) | @@ -137,7 +129,8 @@ dyntype_hash_set(DYNTYPE_HASH _this, const char * key, DYNTYPE value) | ||
| 137 | _updateHashSize(_this); | 129 | _updateHashSize(_this); |
| 138 | } | 130 | } |
| 139 | 131 | ||
| 140 | -DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key) | 132 | +DYNTYPE |
| 133 | +dyntype_hash_get(DYNTYPE_HASH _this, const char * key) | ||
| 141 | { | 134 | { |
| 142 | size_t index = _getHashIdx(_this, key); | 135 | size_t index = _getHashIdx(_this, key); |
| 143 | 136 | ||
| @@ -148,9 +141,9 @@ DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key) | @@ -148,9 +141,9 @@ DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key) | ||
| 148 | return _this->values[index]; | 141 | return _this->values[index]; |
| 149 | } | 142 | } |
| 150 | 143 | ||
| 151 | -DYNTYPE dyntype_hash_del(DYNTYPE_HASH _this, const char * key) | 144 | +void |
| 145 | +dyntype_hash_del(DYNTYPE_HASH _this, const char * key) | ||
| 152 | { | 146 | { |
| 153 | - DYNTYPE found = NULL; | ||
| 154 | size_t index = _getHashIdx(_this, key); | 147 | size_t index = _getHashIdx(_this, key); |
| 155 | 148 | ||
| 156 | if (index == _this->used) { | 149 | if (index == _this->used) { |
| @@ -20,16 +20,14 @@ | @@ -20,16 +20,14 @@ | ||
| 20 | #include "token/packet.h" | 20 | #include "token/packet.h" |
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | -static | ||
| 24 | -void | ||
| 25 | -__construct(PACKET _this, va_list * params) | 23 | +INIT_CCLASS(PACKET); |
| 24 | + | ||
| 25 | +__construct(PACKET) | ||
| 26 | { | 26 | { |
| 27 | packet_set_default_content(_this); | 27 | packet_set_default_content(_this); |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | -static | ||
| 31 | -void | ||
| 32 | -__jsonConst(PACKET _this, struct json_object * json) | 30 | +__jsonConst(PACKET) |
| 33 | { | 31 | { |
| 34 | struct json_object * header = NULL; | 32 | struct json_object * header = NULL; |
| 35 | struct json_object * data = NULL; | 33 | struct json_object * data = NULL; |
| @@ -51,25 +49,20 @@ __jsonConst(PACKET _this, struct json_object * json) | @@ -51,25 +49,20 @@ __jsonConst(PACKET _this, struct json_object * json) | ||
| 51 | packet_setData(_this, newFromJson(DYNTYPE, data)); | 49 | packet_setData(_this, newFromJson(DYNTYPE, data)); |
| 52 | } | 50 | } |
| 53 | 51 | ||
| 54 | -static | ||
| 55 | -void | ||
| 56 | -__destruct(PACKET _this) | ||
| 57 | -{ | ||
| 58 | -} | 52 | +__destruct(PACKET) {} |
| 59 | 53 | ||
| 60 | -static | ||
| 61 | -struct json_object * | ||
| 62 | -__toJson(PACKET _this) | 54 | +__toJson(PACKET) |
| 63 | { | 55 | { |
| 64 | - struct json_object * json = json_object_new_array(); | 56 | + struct json_object * value; |
| 65 | 57 | ||
| 66 | - json_object_array_add(json, toJson(packet_getHeader(_this))); | ||
| 67 | - json_object_array_add(json, toJson(packet_getData(_this))); | 58 | + *json = json_object_new_array(); |
| 68 | 59 | ||
| 69 | - return json; | ||
| 70 | -} | 60 | + toJson(packet_getHeader(_this), &value); |
| 61 | + json_object_array_add(*json, value); | ||
| 71 | 62 | ||
| 72 | -INIT_CCLASS(PACKET, __jsonConst, __toJson); | 63 | + toJson(packet_getData(_this), &value); |
| 64 | + json_object_array_add(*json, value); | ||
| 65 | +} | ||
| 73 | 66 | ||
| 74 | DYNTYPE packet_getHeader(PACKET _this) | 67 | DYNTYPE packet_getHeader(PACKET _this) |
| 75 | { | 68 | { |
| @@ -6,24 +6,15 @@ | @@ -6,24 +6,15 @@ | ||
| 6 | 6 | ||
| 7 | char _called; | 7 | char _called; |
| 8 | 8 | ||
| 9 | -void | ||
| 10 | -inline | ||
| 11 | -_reset() | ||
| 12 | -{ | ||
| 13 | - _called = 0; | ||
| 14 | -} | 9 | +INIT_CCLASS(MOCK_CLASS); |
| 15 | 10 | ||
| 16 | -static | ||
| 17 | -void | ||
| 18 | -__construct(MOCK_CLASS _this, va_list * params) | 11 | +__construct(MOCK_CLASS) |
| 19 | { | 12 | { |
| 20 | _called = 1; | 13 | _called = 1; |
| 21 | _this->value = va_arg(* params, int); | 14 | _this->value = va_arg(* params, int); |
| 22 | } | 15 | } |
| 23 | 16 | ||
| 24 | -static | ||
| 25 | -void | ||
| 26 | -__jsonConst(MOCK_CLASS _this, json_object * json) | 17 | +__jsonConst(MOCK_CLASS) |
| 27 | { | 18 | { |
| 28 | _called = 1; | 19 | _called = 1; |
| 29 | assert(json_type_int == json_object_get_type(json)); | 20 | assert(json_type_int == json_object_get_type(json)); |
| @@ -31,25 +22,17 @@ __jsonConst(MOCK_CLASS _this, json_object * json) | @@ -31,25 +22,17 @@ __jsonConst(MOCK_CLASS _this, json_object * json) | ||
| 31 | _this->value = json_object_get_int(json); | 22 | _this->value = json_object_get_int(json); |
| 32 | } | 23 | } |
| 33 | 24 | ||
| 34 | -static | ||
| 35 | -void | ||
| 36 | -__destruct(MOCK_CLASS _this) | 25 | +__destruct(MOCK_CLASS) |
| 37 | { | 26 | { |
| 38 | _called = 1; | 27 | _called = 1; |
| 39 | } | 28 | } |
| 40 | 29 | ||
| 41 | -static | ||
| 42 | -struct json_object * | ||
| 43 | -__toJson(MOCK_CLASS _this) | 30 | +__toJson(MOCK_CLASS) |
| 44 | { | 31 | { |
| 45 | - struct json_object * json = json_object_new_int(_this->value); | ||
| 46 | - | 32 | + *json = json_object_new_int(_this->value); |
| 47 | _called = 1; | 33 | _called = 1; |
| 48 | - return json; | ||
| 49 | } | 34 | } |
| 50 | 35 | ||
| 51 | -INIT_CCLASS(MOCK_CLASS, __jsonConst, __toJson); | ||
| 52 | - | ||
| 53 | /** | 36 | /** |
| 54 | * ~~~ method implementations ~~~~~~~~ | 37 | * ~~~ method implementations ~~~~~~~~ |
| 55 | */ | 38 | */ |
| @@ -3,10 +3,18 @@ | @@ -3,10 +3,18 @@ | ||
| 3 | 3 | ||
| 4 | #include "token/cclass.h" | 4 | #include "token/cclass.h" |
| 5 | 5 | ||
| 6 | - | ||
| 7 | extern char _called; | 6 | extern char _called; |
| 8 | 7 | ||
| 9 | -extern void inline _reset(); | 8 | +#ifndef _RESET |
| 9 | +#define _RESET | ||
| 10 | +void | ||
| 11 | +inline | ||
| 12 | +_reset() | ||
| 13 | +{ | ||
| 14 | + _called = 0; | ||
| 15 | +} | ||
| 16 | +#endif//_RESET | ||
| 17 | + | ||
| 10 | 18 | ||
| 11 | CLASS(MOCK_CLASS) | 19 | CLASS(MOCK_CLASS) |
| 12 | int value; | 20 | int value; |
| @@ -55,7 +55,7 @@ main(int argc, char * argv[]) | @@ -55,7 +55,7 @@ main(int argc, char * argv[]) | ||
| 55 | printf("running tests for %s\n", testname); | 55 | printf("running tests for %s\n", testname); |
| 56 | 56 | ||
| 57 | for (index=0; index<count; index++) { | 57 | for (index=0; index<count; index++) { |
| 58 | - int result, _setUp = 0; // initialize setup to false | 58 | + int result = TEST_ERROR, _setUp = 0; // initialize setup to false |
| 59 | 59 | ||
| 60 | if (NULL != setUp) { | 60 | if (NULL != setUp) { |
| 61 | if (TEST_OK == (result = setUp())) { | 61 | if (TEST_OK == (result = setUp())) { |
Please
register
or
login
to post a comment