Browse Source

added more convenience macros for classes and used them in the classes

master
Georg Hopp 14 years ago
parent
commit
9a28fc52db
  1. 2
      createToken.c
  2. 39
      include/token/cclass.h
  3. 13
      include/token/dyntype.h
  4. 20
      include/token/dyntype/hash.h
  5. 12
      src/cclass.c
  6. 15
      src/crypt.c
  7. 32
      src/dyntype.c
  8. 41
      src/dyntype/hash.c
  9. 33
      src/packet.c
  10. 2
      tests/cclassTest.c
  11. 29
      tests/mock/class.c
  12. 12
      tests/mock/class.h
  13. 2
      tests/packetTest.c
  14. 2
      tests/runtest.c

2
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);

39
include/token/cclass.h

@ -33,39 +33,46 @@
#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)
#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);
struct json_object * toJson(void * _object);
void toJson(void * _object, struct json_object ** json);
int isObject(void * _object);
int _instanceOf(const _CCLASS _class, void * _object);

13
include/token/dyntype.h

@ -16,17 +16,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __DYNTYPE_H__
#ifndef __DYNTYPE_HASH_H__
#include "token/dyntype/hash.h"
#endif//__DYNTYPE_HASH_H__
#define __DYNTYPE_H__
#include <sys/types.h>
#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:

20
include/token/dyntype/hash.h

@ -16,25 +16,33 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __DYNTYPE_HASH_H__
#define __DYNTYPE_HASH_H__
#include <sys/types.h>
#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__

12
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

15
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;

32
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:

41
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) {

33
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)
{

2
tests/cclassTest.c

@ -88,7 +88,7 @@ testToJson(void)
int value;
_reset();
json = toJson(mock);
toJson(mock, &json);
ASSERT_NOT_NULL(json);

29
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 ~~~~~~~~
*/

12
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;

2
tests/packetTest.c

@ -5,7 +5,7 @@
const char testname[] = "packetTest";
struct PACKET * packet = NULL;
PACKET packet = NULL;
static

2
tests/runtest.c

@ -55,7 +55,7 @@ main(int argc, char * argv[])
printf("running tests for %s\n", testname);
for (index=0; index<count; index++) {
int result, _setUp = 0; // initialize setup to false
int result = TEST_ERROR, _setUp = 0; // initialize setup to false
if (NULL != setUp) {
if (TEST_OK == (result = setUp())) {

Loading…
Cancel
Save