Commit e7ed789d0cf5553d3754c71a5897407030227e58

Authored by Georg Hopp
1 parent 05da1896

some more work on dyntype

1 1 .*.swp
  2 +*.o
  3 +
... ...
... ... @@ -5,8 +5,11 @@
5 5 #include <sys/types.h>
6 6 #include <json/json.h>
7 7
8   -typedef void (* ctor)(void*, va_list*);
9   -typedef void (* dtor)(void*);
  8 +typedef void (* ctor)(void *, va_list *);
  9 +typedef void (* dtor)(void *);
  10 +typedef void (* jCtor)(void *, struct json_object *);
  11 +typedef struct json_object * (* jTo)(void *);
  12 +
10 13
11 14 struct BIGPOINT_CCLASS {
12 15 size_t size;
... ...
... ... @@ -13,16 +13,40 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params)
13 13 _this->type = va_arg(* params, enum BIGPOINT_DYNTYPE_TYPES);
14 14 _this->size = va_arg(* params, size_t);
15 15
16   - _this->data = calloc(_this->size, sizeof(char));
17   - memcpy(_this->data, va_arg(* params, void *), _this->size);
  16 + switch(_this->type) {
  17 + case BIGPOINT_DYNTYPE_INT:
  18 + (_this->data)._int = va_arg(* params, long);
  19 + break;
  20 +
  21 + default:
  22 + (_this->data)._object = NULL;
  23 + }
  24 +}
  25 +
  26 +static
  27 +void
  28 +__jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json)
  29 +{
  30 + switch (json_object_get_type(json)) {
  31 + case json_type_int:
  32 + _this->type = BIGPOINT_DYNTYPE_INT;
  33 + _this->size = sizeof(long);
  34 + (_this->data)._int = (long)json_object_get_int(json);
  35 + break;
  36 +
  37 + default:
  38 + _this->type = BIGPOINT_DYNTYPE_NULL;
  39 + _this->size = 0;
  40 + (_this->data)._object = NULL;
  41 + }
18 42 }
19 43
20 44 static
21 45 void
22 46 __destruct(struct BIGPOINT_DYNTYPE * _this)
23 47 {
24   - if (_this && _this->data) {
25   - free(_this->data);
  48 + if (_this && BIGPOINT_DYNTYPE_OBJECT == _this->type && (_this->data)._object) {
  49 + free((_this->data)._object);
26 50 }
27 51 }
28 52
... ... @@ -39,22 +63,13 @@ __toJson(struct BIGPOINT_DYNTYPE * _this)
39 63 return json;
40 64 }
41 65
42   -static
43   -void
44   -__jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json)
45   -{
46   - /**
47   - * @TODO: initialize by json....
48   - */
49   -}
50   -
51 66 static const
52 67 struct BIGPOINT_CCLASS _bigpoint_dyntype = {
53 68 sizeof(struct BIGPOINT_DYNTYPE),
54 69 (ctor)__construct,
55   - __jsonConst,
  70 + (jCtor)__jsonConst,
56 71 (dtor)__destruct,
57   - __toJson
  72 + (jTo)__toJson
58 73 };
59 74
60 75 const struct BIGPOINT_CCLASS * const BIGPOINT_DYNTYPE = &_bigpoint_dyntype;
... ...
... ... @@ -7,7 +7,8 @@
7 7
8 8
9 9 enum BIGPOINT_DYNTYPE_TYPES {
10   - BIGPOINT_DYNTYPE_BOOLEAN = 0,
  10 + BIGPOINT_DYNTYPE_NULL = 0,
  11 + BIGPOINT_DYNTYPE_BOOLEAN,
11 12 BIGPOINT_DYNTYPE_INT,
12 13 BIGPOINT_DYNTYPE_FLOAT,
13 14 BIGPOINT_DYNTYPE_STRING,
... ... @@ -20,7 +21,13 @@ struct BIGPOINT_DYNTYPE {
20 21 const struct BIGPOINT_CCLASS * const class;
21 22 enum BIGPOINT_DYNTYPE_TYPES type;
22 23 size_t size;
23   - void * data;
  24 + union _data {
  25 + unsigned char _boolean;
  26 + long _int;
  27 + double _float;
  28 + char * _string;
  29 + void * _object;
  30 + } data;
24 31 };
25 32
26 33 extern const struct BIGPOINT_CCLASS * const BIGPOINT_DYNTYPE;
... ...
Please register or login to post a comment