diff --git a/createToken.c b/createToken.c index b90d487..b0161ab 100644 --- a/createToken.c +++ b/createToken.c @@ -70,7 +70,7 @@ main(int argc, char * argv[]) sizeof(DYNTYPE_HASH), data)); - json = toJson(packet); + toJson(packet, &json); json_str = json_object_to_json_string(json); length = strlen(json_str); diff --git a/include/token/cclass.h b/include/token/cclass.h index f5ea742..86681c3 100644 --- a/include/token/cclass.h +++ b/include/token/cclass.h @@ -33,41 +33,48 @@ #define ENDC(_class) } * _class; \ extern const _CCLASS const __##_class; -#define INIT_CCLASS(class, jsonConst, toJson) \ - static const struct CCLASS _class = { \ - CCLASS_MAGIC, \ - sizeof(struct _##class), \ - (ctor)__construct, \ - (jCtor)jsonConst, \ - (dtor)__destruct, \ - (jTo)toJson \ - }; const _CCLASS const __##class = (const _CCLASS const)&_class - - - typedef void (* ctor)(void *, va_list *); typedef void (* dtor)(void *); typedef void (* jCtor)(void *, struct json_object *); -typedef struct json_object * (* jTo)(void *); +typedef void (* jTo)(void *, struct json_object **); typedef struct CCLASS { const int magic; size_t size; void (* __construct)(void * _this, va_list * params); - void (* __jsonConst)(void * _this, struct json_object * json); void (* __destruct)(void * _this); - struct json_object * (* __toJson)(void * _this); + void (* __jsonConst)(void * _this, struct json_object * json); + void (* __toJson)(void * _this, struct json_object ** json); } * _CCLASS; #define CCLASS_PTR_SIZE sizeof(struct CCLASS *) #define CCLASS_SIZE sizeof(struct CCLASS) -void * _new(const _CCLASS _class, ...); -void * _newFromJson(const _CCLASS _class, struct json_object * json); -void delete(void * _object); -struct json_object * toJson(void * _object); -int isObject(void * _object); -int _instanceOf(const _CCLASS _class, void * _object); +#define __construct(class) static void __construct(class _this, va_list * params) +#define __destruct(class) static void __destruct(class _this) +#define __jsonConst(class) static void __jsonConst(class _this, struct json_object * json) +#define __toJson(class) static void __toJson(class _this, struct json_object ** json) + +#define INIT_CCLASS(class) \ + __construct(class); \ + __destruct(class); \ + __jsonConst(class); \ + __toJson(class); \ + static const struct CCLASS _class = { \ + CCLASS_MAGIC, \ + sizeof(struct _##class), \ + (ctor)__construct, \ + (dtor)__destruct, \ + (jCtor)__jsonConst, \ + (jTo)__toJson \ + }; const _CCLASS __##class = (const _CCLASS)&_class + +void * _new(const _CCLASS _class, ...); +void * _newFromJson(const _CCLASS _class, struct json_object * json); +void delete(void * _object); +void toJson(void * _object, struct json_object ** json); +int isObject(void * _object); +int _instanceOf(const _CCLASS _class, void * _object); #define new(class, ...) _new((__##class), __VA_ARGS__) #define newFromJson(class, json) _newFromJson((__##class), (json)) diff --git a/include/token/dyntype.h b/include/token/dyntype.h index 87f11cf..3e6b73c 100644 --- a/include/token/dyntype.h +++ b/include/token/dyntype.h @@ -16,17 +16,16 @@ * along with this program. If not, see . */ #ifndef __DYNTYPE_H__ + +#ifndef __DYNTYPE_HASH_H__ +#include "token/dyntype/hash.h" +#endif//__DYNTYPE_HASH_H__ + #define __DYNTYPE_H__ #include - #include "token/cclass.h" -struct _DYNTYPE; - -#include "token/dyntype/hash.h" - - enum DYNTYPE_TYPES { DYNTYPE_TYPE_NULL = 0, DYNTYPE_TYPE_BOOLEAN, @@ -51,6 +50,8 @@ CLASS(DYNTYPE) } data; ENDC(DYNTYPE) +#include "token/dyntype/hash.h" + #endif//__DYNTYPE_H__ // vim: set et ts=4 sw=4: diff --git a/include/token/dyntype/hash.h b/include/token/dyntype/hash.h index 7096fbc..02e3455 100644 --- a/include/token/dyntype/hash.h +++ b/include/token/dyntype/hash.h @@ -16,25 +16,33 @@ * along with this program. If not, see . */ #ifndef __DYNTYPE_HASH_H__ -#define __DYNTYPE_HASH_H__ #include - #include "token/cclass.h" -#include "token/dyntype.h" +#ifndef __DYNTYPE_H__ + +struct _DYNTYPE_HASH; +#define DYNTYPE_HASH struct _DYNTYPE_HASH * + +#else + +#undef DYNTYPE_HASH +#define __DYNTYPE_HASH_H__ CLASS(DYNTYPE_HASH) char ** keys; - struct _DYNTYPE ** values; + DYNTYPE * values; size_t size; size_t used; ENDC(DYNTYPE_HASH) void dyntype_hash_set(DYNTYPE_HASH _this, const char * key, struct _DYNTYPE * value); -struct _DYNTYPE * dyntype_hash_get(DYNTYPE_HASH _this, const char * key); -struct _DYNTYPE * dyntype_hash_del(DYNTYPE_HASH _this, const char * key); +DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key); +void dyntype_hash_del(DYNTYPE_HASH _this, const char * key); + +#endif #endif//__DYNTYPE_HASH_H__ diff --git a/src/cclass.c b/src/cclass.c index 9b16bb0..6d1713b 100644 --- a/src/cclass.c +++ b/src/cclass.c @@ -22,6 +22,10 @@ #include "token/cclass.h" +#undef __construct +#undef __destruct +#undef __jsonConst +#undef __toJson void * _new(const _CCLASS _class, ...) @@ -70,16 +74,14 @@ delete(void * _object) *(void**)_object = NULL; } -struct json_object * -toJson(void * _object) +void +toJson(void * _object, struct json_object ** json) { const struct CCLASS ** class = _object; if (_object && *class && (*class)->__toJson) { - return (*class)->__toJson(_object); + (*class)->__toJson(_object, json); } - - return NULL; } int diff --git a/src/crypt.c b/src/crypt.c index e056e6f..3bde1ee 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -28,9 +28,9 @@ #include "token/crypt.h" -static -void -__construct(CRYPT _this, va_list * params) +INIT_CCLASS(CRYPT); + +__construct(CRYPT) { _this->algorithm = va_arg(* params, const char * const); _this->mode = va_arg(* params, const char * const); @@ -45,9 +45,7 @@ __construct(CRYPT _this, va_list * params) _this->keysize = mcrypt_enc_get_key_size(_this->mcrypt); } -static -void -__destruct(CRYPT _this) +__destruct(CRYPT) { if (_this->iv) { free(_this->iv); @@ -56,7 +54,8 @@ __destruct(CRYPT _this) mcrypt_module_close(_this->mcrypt); } -INIT_CCLASS(CRYPT, NULL, NULL); +__jsonConst(CRYPT) {} +__toJson(CRYPT) {} void * crypt_createIv(CRYPT _this) @@ -94,7 +93,7 @@ createKey(CRYPT _this, const char * const password) _this->keysize, NULL, 0, - (char *)password, // @TODO: bad karma...now this might change password. + (mutils_word8 *)password, // @TODO: bad karma...this might change password. strlen(password)); return key; diff --git a/src/dyntype.c b/src/dyntype.c index e764e76..516fbe6 100644 --- a/src/dyntype.c +++ b/src/dyntype.c @@ -24,9 +24,9 @@ #include "token/dyntype/hash.h" -static -void -__construct(DYNTYPE _this, va_list * params) +INIT_CCLASS(DYNTYPE); + +__construct(DYNTYPE) { _this->type = va_arg(* params, enum DYNTYPE_TYPES); _this->size = va_arg(* params, size_t); @@ -50,9 +50,7 @@ __construct(DYNTYPE _this, va_list * params) } } -static -void -__jsonConst(DYNTYPE _this, struct json_object * json) +__jsonConst(DYNTYPE) { switch (json_object_get_type(json)) { case json_type_int: @@ -86,9 +84,7 @@ __jsonConst(DYNTYPE _this, struct json_object * json) } } -static -void -__destruct(DYNTYPE _this) +__destruct(DYNTYPE) { if (_this) { switch(_this->type) { @@ -106,32 +102,24 @@ __destruct(DYNTYPE _this) } } -static -struct json_object * -__toJson(DYNTYPE _this) +__toJson(DYNTYPE) { - struct json_object * json = NULL; - switch(_this->type) { case DYNTYPE_TYPE_INT: - json = json_object_new_int((_this->data)._int); + *json = json_object_new_int((_this->data)._int); break; case DYNTYPE_TYPE_STRING: - json = json_object_new_string((_this->data)._string); + *json = json_object_new_string((_this->data)._string); break; case DYNTYPE_TYPE_HASH: - json = toJson((_this->data)._hash); + toJson((_this->data)._hash, json); break; default: - json = NULL; + *json = NULL; } - - return json; } -INIT_CCLASS(DYNTYPE, __jsonConst, __toJson); - // vim: set et ts=4 sw=4: diff --git a/src/dyntype/hash.c b/src/dyntype/hash.c index dc2aa1e..306098d 100644 --- a/src/dyntype/hash.c +++ b/src/dyntype/hash.c @@ -29,18 +29,17 @@ static void _updateHashSize(DYNTYPE_HASH _this); -static -void -__construct(DYNTYPE_HASH _this, va_list * params) +INIT_CCLASS(DYNTYPE_HASH); + +__construct(DYNTYPE_HASH) { _this->size = 0; _this->used = 0; _updateHashSize(_this); } +#undef __construct -static -void -__jsonConst(DYNTYPE_HASH _this, struct json_object * json) +__jsonConst(DYNTYPE_HASH) { __construct(_this, NULL); @@ -53,9 +52,7 @@ __jsonConst(DYNTYPE_HASH _this, struct json_object * json) } } -static -void -__destruct(DYNTYPE_HASH _this) +__destruct(DYNTYPE_HASH) { size_t index; @@ -67,24 +64,19 @@ __destruct(DYNTYPE_HASH _this) free(_this->values); } -static -struct json_object * -__toJson(DYNTYPE_HASH _this) +__toJson(DYNTYPE_HASH) { size_t index; - struct json_object * json = json_object_new_object(); + *json = json_object_new_object(); for (index = 0; index < _this->used; index ++) { - json_object_object_add( - json, - _this->keys[index], - toJson(_this->values[index])); - } + struct json_object * values; - return json; -} + toJson(_this->values[index], &values); -INIT_CCLASS(DYNTYPE_HASH, __jsonConst, __toJson); + json_object_object_add(*json, _this->keys[index], values); + } +} static void @@ -137,7 +129,8 @@ dyntype_hash_set(DYNTYPE_HASH _this, const char * key, DYNTYPE value) _updateHashSize(_this); } -DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key) +DYNTYPE +dyntype_hash_get(DYNTYPE_HASH _this, const char * key) { size_t index = _getHashIdx(_this, key); @@ -148,9 +141,9 @@ DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key) return _this->values[index]; } -DYNTYPE dyntype_hash_del(DYNTYPE_HASH _this, const char * key) +void +dyntype_hash_del(DYNTYPE_HASH _this, const char * key) { - DYNTYPE found = NULL; size_t index = _getHashIdx(_this, key); if (index == _this->used) { diff --git a/src/packet.c b/src/packet.c index 3367564..9a37ef9 100644 --- a/src/packet.c +++ b/src/packet.c @@ -20,16 +20,14 @@ #include "token/packet.h" -static -void -__construct(PACKET _this, va_list * params) +INIT_CCLASS(PACKET); + +__construct(PACKET) { packet_set_default_content(_this); } -static -void -__jsonConst(PACKET _this, struct json_object * json) +__jsonConst(PACKET) { struct json_object * header = NULL; struct json_object * data = NULL; @@ -51,25 +49,20 @@ __jsonConst(PACKET _this, struct json_object * json) packet_setData(_this, newFromJson(DYNTYPE, data)); } -static -void -__destruct(PACKET _this) -{ -} +__destruct(PACKET) {} -static -struct json_object * -__toJson(PACKET _this) +__toJson(PACKET) { - struct json_object * json = json_object_new_array(); + struct json_object * value; - json_object_array_add(json, toJson(packet_getHeader(_this))); - json_object_array_add(json, toJson(packet_getData(_this))); + *json = json_object_new_array(); - return json; -} + toJson(packet_getHeader(_this), &value); + json_object_array_add(*json, value); -INIT_CCLASS(PACKET, __jsonConst, __toJson); + toJson(packet_getData(_this), &value); + json_object_array_add(*json, value); +} DYNTYPE packet_getHeader(PACKET _this) { diff --git a/tests/cclassTest.c b/tests/cclassTest.c index 4b69753..ba27b2b 100644 --- a/tests/cclassTest.c +++ b/tests/cclassTest.c @@ -88,7 +88,7 @@ testToJson(void) int value; _reset(); - json = toJson(mock); + toJson(mock, &json); ASSERT_NOT_NULL(json); diff --git a/tests/mock/class.c b/tests/mock/class.c index 237a93a..da28fd3 100644 --- a/tests/mock/class.c +++ b/tests/mock/class.c @@ -6,24 +6,15 @@ char _called; -void -inline -_reset() -{ - _called = 0; -} +INIT_CCLASS(MOCK_CLASS); -static -void -__construct(MOCK_CLASS _this, va_list * params) +__construct(MOCK_CLASS) { _called = 1; _this->value = va_arg(* params, int); } -static -void -__jsonConst(MOCK_CLASS _this, json_object * json) +__jsonConst(MOCK_CLASS) { _called = 1; assert(json_type_int == json_object_get_type(json)); @@ -31,25 +22,17 @@ __jsonConst(MOCK_CLASS _this, json_object * json) _this->value = json_object_get_int(json); } -static -void -__destruct(MOCK_CLASS _this) +__destruct(MOCK_CLASS) { _called = 1; } -static -struct json_object * -__toJson(MOCK_CLASS _this) +__toJson(MOCK_CLASS) { - struct json_object * json = json_object_new_int(_this->value); - + *json = json_object_new_int(_this->value); _called = 1; - return json; } -INIT_CCLASS(MOCK_CLASS, __jsonConst, __toJson); - /** * ~~~ method implementations ~~~~~~~~ */ diff --git a/tests/mock/class.h b/tests/mock/class.h index e56dfca..c34d513 100644 --- a/tests/mock/class.h +++ b/tests/mock/class.h @@ -3,10 +3,18 @@ #include "token/cclass.h" - extern char _called; -extern void inline _reset(); +#ifndef _RESET +#define _RESET +void +inline +_reset() +{ + _called = 0; +} +#endif//_RESET + CLASS(MOCK_CLASS) int value; diff --git a/tests/packetTest.c b/tests/packetTest.c index 7b97be8..4f6afe5 100644 --- a/tests/packetTest.c +++ b/tests/packetTest.c @@ -5,7 +5,7 @@ const char testname[] = "packetTest"; -struct PACKET * packet = NULL; +PACKET packet = NULL; static diff --git a/tests/runtest.c b/tests/runtest.c index 92590ae..8d5dea4 100644 --- a/tests/runtest.c +++ b/tests/runtest.c @@ -55,7 +55,7 @@ main(int argc, char * argv[]) printf("running tests for %s\n", testname); for (index=0; index