forked from mirkokiefer/LivelyC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCKeyValue.c
82 lines (68 loc) · 2.37 KB
/
LCKeyValue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "LCKeyValue.h"
typedef struct keyValueData* keyValueDataRef;
void keyValueDealloc(LCObjectRef object);
LCCompare keyValueCompare(LCObjectRef object1, LCObjectRef object2);
void keyValueWalkChildren(LCObjectRef object, void *cookie, childCallback cb);
void keyValueStoreChildren(LCObjectRef object, char *key, LCObjectRef objects[], size_t length);
static void* keyValueInitData();
struct keyValueData {
LCObjectRef key;
LCObjectRef value;
};
struct LCType typeKeyValue = {
.name = "LCKeyValue",
.immutable = false,
.dealloc = keyValueDealloc,
.compare = keyValueCompare,
.initData = keyValueInitData,
.walkChildren = keyValueWalkChildren,
.storeChildren = keyValueStoreChildren
};
LCTypeRef LCTypeKeyValue = &typeKeyValue;
LCKeyValueRef LCKeyValueCreate(LCObjectRef key, LCObjectRef value) {
keyValueDataRef newKeyValue = keyValueInitData();
newKeyValue->key=objectRetain(key);
newKeyValue->value=objectRetain(value);
return objectCreate(LCTypeKeyValue, newKeyValue);
};
static void* keyValueInitData() {
keyValueDataRef newKeyValue = malloc(sizeof(struct keyValueData));
if (newKeyValue) {
newKeyValue->key = NULL;
newKeyValue->value = NULL;
}
return newKeyValue;
}
LCObjectRef LCKeyValueKey(LCKeyValueRef keyValue) {
keyValueDataRef keyValueData = objectData(keyValue);
return keyValueData->key;
}
LCObjectRef LCKeyValueValue(LCKeyValueRef keyValue) {
keyValueDataRef keyValueData = objectData(keyValue);
return keyValueData->value;
}
LCCompare keyValueCompare(LCObjectRef object1, LCObjectRef object2) {
return objectCompare(LCKeyValueKey(object1), LCKeyValueKey(object2));
}
void keyValueDealloc(LCObjectRef object) {
keyValueDataRef keyValueData = objectData(object);
objectRelease(keyValueData->key);
objectRelease(keyValueData->value);
lcFree(objectData(object));
}
void keyValueWalkChildren(LCObjectRef object, void *cookie, childCallback cb) {
LCObjectRef key = LCKeyValueKey(object);
LCObjectRef value = LCKeyValueValue(object);
cb(cookie, "key", &key, 1, false);
cb(cookie, "value", &value, 1, false);
}
void keyValueStoreChildren(LCObjectRef object, char *key, LCObjectRef objects[], size_t length) {
keyValueDataRef data = objectData(object);
if (strcmp(key, "key")==0) {
data->key = objectRetain(*objects);
return;
} else
if (strcmp(key, "value")==0) {
data->value = objectRetain(*objects);
}
}