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
... | ... | @@ -33,41 +33,48 @@ |
33 | 33 | #define ENDC(_class) } * _class; \ |
34 | 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 | 36 | typedef void (* ctor)(void *, va_list *); |
49 | 37 | typedef void (* dtor)(void *); |
50 | 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 | 42 | typedef struct CCLASS { |
55 | 43 | const int magic; |
56 | 44 | size_t size; |
57 | 45 | void (* __construct)(void * _this, va_list * params); |
58 | - void (* __jsonConst)(void * _this, struct json_object * json); | |
59 | 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 | 49 | } * _CCLASS; |
62 | 50 | #define CCLASS_PTR_SIZE sizeof(struct CCLASS *) |
63 | 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 | 79 | #define new(class, ...) _new((__##class), __VA_ARGS__) |
73 | 80 | #define newFromJson(class, json) _newFromJson((__##class), (json)) | ... | ... |
... | ... | @@ -16,17 +16,16 @@ |
16 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | */ |
18 | 18 | #ifndef __DYNTYPE_H__ |
19 | + | |
20 | +#ifndef __DYNTYPE_HASH_H__ | |
21 | +#include "token/dyntype/hash.h" | |
22 | +#endif//__DYNTYPE_HASH_H__ | |
23 | + | |
19 | 24 | #define __DYNTYPE_H__ |
20 | 25 | |
21 | 26 | #include <sys/types.h> |
22 | - | |
23 | 27 | #include "token/cclass.h" |
24 | 28 | |
25 | -struct _DYNTYPE; | |
26 | - | |
27 | -#include "token/dyntype/hash.h" | |
28 | - | |
29 | - | |
30 | 29 | enum DYNTYPE_TYPES { |
31 | 30 | DYNTYPE_TYPE_NULL = 0, |
32 | 31 | DYNTYPE_TYPE_BOOLEAN, |
... | ... | @@ -51,6 +50,8 @@ CLASS(DYNTYPE) |
51 | 50 | } data; |
52 | 51 | ENDC(DYNTYPE) |
53 | 52 | |
53 | +#include "token/dyntype/hash.h" | |
54 | + | |
54 | 55 | #endif//__DYNTYPE_H__ |
55 | 56 | |
56 | 57 | // vim: set et ts=4 sw=4: | ... | ... |
... | ... | @@ -16,25 +16,33 @@ |
16 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | */ |
18 | 18 | #ifndef __DYNTYPE_HASH_H__ |
19 | -#define __DYNTYPE_HASH_H__ | |
20 | 19 | |
21 | 20 | #include <sys/types.h> |
22 | - | |
23 | 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 | 33 | CLASS(DYNTYPE_HASH) |
28 | 34 | char ** keys; |
29 | - struct _DYNTYPE ** values; | |
35 | + DYNTYPE * values; | |
30 | 36 | size_t size; |
31 | 37 | size_t used; |
32 | 38 | ENDC(DYNTYPE_HASH) |
33 | 39 | |
34 | 40 | |
35 | 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 | 47 | #endif//__DYNTYPE_HASH_H__ |
40 | 48 | ... | ... |
... | ... | @@ -22,6 +22,10 @@ |
22 | 22 | |
23 | 23 | #include "token/cclass.h" |
24 | 24 | |
25 | +#undef __construct | |
26 | +#undef __destruct | |
27 | +#undef __jsonConst | |
28 | +#undef __toJson | |
25 | 29 | |
26 | 30 | void * |
27 | 31 | _new(const _CCLASS _class, ...) |
... | ... | @@ -70,16 +74,14 @@ delete(void * _object) |
70 | 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 | 80 | const struct CCLASS ** class = _object; |
77 | 81 | |
78 | 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 | 87 | int | ... | ... |
... | ... | @@ -28,9 +28,9 @@ |
28 | 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 | 35 | _this->algorithm = va_arg(* params, const char * const); |
36 | 36 | _this->mode = va_arg(* params, const char * const); |
... | ... | @@ -45,9 +45,7 @@ __construct(CRYPT _this, va_list * params) |
45 | 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 | 50 | if (_this->iv) { |
53 | 51 | free(_this->iv); |
... | ... | @@ -56,7 +54,8 @@ __destruct(CRYPT _this) |
56 | 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 | 60 | void * |
62 | 61 | crypt_createIv(CRYPT _this) |
... | ... | @@ -94,7 +93,7 @@ createKey(CRYPT _this, const char * const password) |
94 | 93 | _this->keysize, |
95 | 94 | NULL, |
96 | 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 | 97 | strlen(password)); |
99 | 98 | |
100 | 99 | return key; | ... | ... |
... | ... | @@ -24,9 +24,9 @@ |
24 | 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 | 31 | _this->type = va_arg(* params, enum DYNTYPE_TYPES); |
32 | 32 | _this->size = va_arg(* params, size_t); |
... | ... | @@ -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 | 55 | switch (json_object_get_type(json)) { |
58 | 56 | case json_type_int: |
... | ... | @@ -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 | 89 | if (_this) { |
94 | 90 | switch(_this->type) { |
... | ... | @@ -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 | 107 | switch(_this->type) { |
116 | 108 | case DYNTYPE_TYPE_INT: |
117 | - json = json_object_new_int((_this->data)._int); | |
109 | + *json = json_object_new_int((_this->data)._int); | |
118 | 110 | break; |
119 | 111 | |
120 | 112 | case DYNTYPE_TYPE_STRING: |
121 | - json = json_object_new_string((_this->data)._string); | |
113 | + *json = json_object_new_string((_this->data)._string); | |
122 | 114 | break; |
123 | 115 | |
124 | 116 | case DYNTYPE_TYPE_HASH: |
125 | - json = toJson((_this->data)._hash); | |
117 | + toJson((_this->data)._hash, json); | |
126 | 118 | break; |
127 | 119 | |
128 | 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 | 125 | // vim: set et ts=4 sw=4: | ... | ... |
... | ... | @@ -29,18 +29,17 @@ |
29 | 29 | |
30 | 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 | 36 | _this->size = 0; |
37 | 37 | _this->used = 0; |
38 | 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 | 44 | __construct(_this, NULL); |
46 | 45 | |
... | ... | @@ -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 | 57 | size_t index; |
61 | 58 | |
... | ... | @@ -67,24 +64,19 @@ __destruct(DYNTYPE_HASH _this) |
67 | 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 | 69 | size_t index; |
75 | - struct json_object * json = json_object_new_object(); | |
70 | + *json = json_object_new_object(); | |
76 | 71 | |
77 | 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 | 81 | static |
90 | 82 | void |
... | ... | @@ -137,7 +129,8 @@ dyntype_hash_set(DYNTYPE_HASH _this, const char * key, DYNTYPE value) |
137 | 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 | 135 | size_t index = _getHashIdx(_this, key); |
143 | 136 | |
... | ... | @@ -148,9 +141,9 @@ DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key) |
148 | 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 | 147 | size_t index = _getHashIdx(_this, key); |
155 | 148 | |
156 | 149 | if (index == _this->used) { | ... | ... |
... | ... | @@ -20,16 +20,14 @@ |
20 | 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 | 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 | 32 | struct json_object * header = NULL; |
35 | 33 | struct json_object * data = NULL; |
... | ... | @@ -51,25 +49,20 @@ __jsonConst(PACKET _this, struct json_object * json) |
51 | 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 | 67 | DYNTYPE packet_getHeader(PACKET _this) |
75 | 68 | { | ... | ... |
... | ... | @@ -6,24 +6,15 @@ |
6 | 6 | |
7 | 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 | 13 | _called = 1; |
21 | 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 | 19 | _called = 1; |
29 | 20 | assert(json_type_int == json_object_get_type(json)); |
... | ... | @@ -31,25 +22,17 @@ __jsonConst(MOCK_CLASS _this, json_object * json) |
31 | 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 | 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 | 33 | _called = 1; |
48 | - return json; | |
49 | 34 | } |
50 | 35 | |
51 | -INIT_CCLASS(MOCK_CLASS, __jsonConst, __toJson); | |
52 | - | |
53 | 36 | /** |
54 | 37 | * ~~~ method implementations ~~~~~~~~ |
55 | 38 | */ | ... | ... |
... | ... | @@ -3,10 +3,18 @@ |
3 | 3 | |
4 | 4 | #include "token/cclass.h" |
5 | 5 | |
6 | - | |
7 | 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 | 19 | CLASS(MOCK_CLASS) |
12 | 20 | int value; | ... | ... |
... | ... | @@ -55,7 +55,7 @@ main(int argc, char * argv[]) |
55 | 55 | printf("running tests for %s\n", testname); |
56 | 56 | |
57 | 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 | 60 | if (NULL != setUp) { |
61 | 61 | if (TEST_OK == (result = setUp())) { | ... | ... |
Please
register
or
login
to post a comment