Veritable Lasagna
An Allocator & Data Structure Library for C.
Loading...
Searching...
No Matches
vl_msgpack.h
Go to the documentation of this file.
1#ifndef VL_MSGPACK_H
2#define VL_MSGPACK_H
3
4#include "vl_hashtable.h"
5#include "vl_arena.h"
6#include "vl_buffer.h"
7#include <string.h>
8
9#define VL_MSGPACK_ITER_INVALID VL_HASHTABLE_ITER_INVALID
10#define VL_MSGPACK_EXT_NONE -127
11
12#define VL_MSGPACK_FOREACH_CHILD(packPtr, parentIter, childIterSymbol) \
13 for(vl_msgpack_iter childIterSymbol = vlMsgPackFirstChild((packPtr), (parentIter)); \
14 childIterSymbol != VL_MSGPACK_ITER_INVALID; \
15 childIterSymbol = vlMsgPackNextSibling((packPtr), (childIterSymbol)))
16
18
70
92typedef struct vl_msgpack_{
93 vl_hashtable nodes; //hierarchy
95 vl_msgpack_iter root; //root node
97
104void vlMsgPackInit(vl_msgpack* pack);
105
113void vlMsgPackFree(vl_msgpack* pack);
114
124
133void vlMsgPackDelete(vl_msgpack* pack);
134
144void vlMsgPackClear(vl_msgpack* pack);
145
146
163
172
181
190
199
208
217
225vl_int8_t vlMsgPackExtType(vl_msgpack* pack, vl_msgpack_iter iter);
226
245 vl_msgpack *pack,
246 vl_msgpack_type type,
247 vl_int8_t subType,
248 vl_msgpack_iter parent,
249 const void *keyPtr,
250 vl_memsize_t keyLen,
251 const void *dataPtr,
252 vl_memsize_t dataLen
253);
254
270static inline vl_msgpack_iter vlMsgPackInsert(
271 vl_msgpack *pack,
272 vl_msgpack_type type,
273 vl_msgpack_iter parent,
274 const void *keyPtr,
275 vl_memsize_t keyLen,
276 const void *dataPtr,
277 vl_memsize_t dataLen
278) {
279 return vlMsgPackInsertExt(pack, type, VL_MSGPACK_EXT_NONE, parent, keyPtr, keyLen, dataPtr, dataLen);
280}
281
292
305 vl_msgpack *pack,
306 vl_msgpack_iter parent,
307 const void *key,
308 vl_memsize_t keyLen
309);
310
323static inline vl_msgpack_iter vlMsgPackFindChildNamed(
324 vl_msgpack *pack,
325 vl_msgpack_iter parent,
326 const char *name
327) {
328 return vlMsgPackFindChild(pack, parent, name, strlen(name));
329}
330
343 vl_msgpack *pack,
344 vl_msgpack_iter iter,
345 vl_dsidx_t idx
346);
347
361 vl_msgpack *pack,
362 vl_msgpack_iter iter,
363 vl_memsize_t *size
364);
365
375static inline vl_dsidx_t vlMsgPackSampleKeyIndex(
376 vl_msgpack *pack,
377 vl_msgpack_iter iter
378) {
379 return *((const vl_dsidx_t *) vlMsgPackSampleKey(pack, iter, NULL));
380}
381
395 vl_msgpack *pack,
396 vl_msgpack_iter iter,
397 vl_memsize_t *size
398);
399
400#ifndef vlMsgPackRoot
410#define vlMsgPackRoot(packPtr) ((packPtr)->root)
411#endif
412
425static inline vl_msgpack_iter vlMsgPackSetMapNamed(vl_msgpack* pack, vl_msgpack_iter parent, const char* key){
426 return vlMsgPackInsert(pack, VL_MSGPACK_MAP, parent, key, strlen(key), NULL, 0);
427}
428
441static inline vl_msgpack_iter vlMsgPackSetMapIndexed(vl_msgpack* pack, vl_msgpack_iter parent, vl_dsidx_t idx){
442 return vlMsgPackInsert(pack, VL_MSGPACK_MAP, parent, &idx, sizeof(vl_dsidx_t), NULL, 0);
443}
444
458static inline vl_msgpack_iter vlMsgPackSetArrayNamed(vl_msgpack* pack, vl_msgpack_iter parent, vl_dsidx_t arrayLen, const char* key){
459 return vlMsgPackInsert(pack, VL_MSGPACK_ARRAY, parent, key, strlen(key), &arrayLen, sizeof(vl_dsidx_t));
460}
461
475static inline vl_msgpack_iter vlMsgPackSetArrayIndexed(vl_msgpack* pack, vl_msgpack_iter parent, vl_dsidx_t arrayLen, vl_dsidx_t idx){
476 return vlMsgPackInsert(pack, VL_MSGPACK_ARRAY, parent, &idx, sizeof(vl_dsidx_t), &arrayLen, sizeof(vl_dsidx_t));
477}
478
492static inline vl_msgpack_iter vlMsgPackSetBoolNamed(vl_msgpack* pack, vl_msgpack_iter parent, vl_bool_t value, const char* key){
493 return vlMsgPackInsert(pack, VL_MSGPACK_BOOL, parent, key, strlen(key), &value, sizeof(vl_bool_t));
494}
495
509static inline vl_msgpack_iter vlMsgPackSetBoolIndexed(vl_msgpack* pack, vl_msgpack_iter parent, vl_bool_t value, vl_dsidx_t idx){
510 return vlMsgPackInsert(pack, VL_MSGPACK_BOOL, parent, &idx, sizeof(vl_dsidx_t), &value, sizeof(vl_bool_t));
511}
512
526static inline vl_msgpack_iter vlMsgPackSetIntNamed(vl_msgpack* pack, vl_msgpack_iter parent, vl_ilarge_t value, const char* key){
527 return vlMsgPackInsert(pack, VL_MSGPACK_INT, parent, key, strlen(key), &value, sizeof(vl_ilarge_t));
528}
529
543static inline vl_msgpack_iter vlMsgPackSetIntIndexed(vl_msgpack* pack, vl_msgpack_iter parent, vl_ilarge_t value, vl_dsidx_t idx){
544 return vlMsgPackInsert(pack, VL_MSGPACK_INT, parent, &idx, sizeof(vl_dsidx_t), &value, sizeof(vl_ilarge_t));
545}
559static inline vl_msgpack_iter vlMsgPackSetUIntNamed(vl_msgpack* pack, vl_msgpack_iter parent, vl_ularge_t value, const char* key){
560 return vlMsgPackInsert(pack, VL_MSGPACK_UINT, parent, key, strlen(key), &value, sizeof(vl_ularge_t));
561}
562
576static inline vl_msgpack_iter vlMsgPackSetUIntIndexed(vl_msgpack* pack, vl_msgpack_iter parent, vl_ularge_t value, vl_dsidx_t idx){
577 return vlMsgPackInsert(pack, VL_MSGPACK_UINT, parent, &idx, sizeof(vl_dsidx_t), &value, sizeof(vl_ularge_t));
578}
579
593static inline vl_msgpack_iter vlMsgPackSetFloat32Named(vl_msgpack* pack, vl_msgpack_iter parent, vl_float32_t value, const char* key){
594 return vlMsgPackInsert(pack, VL_MSGPACK_FLOAT32, parent, key, strlen(key), &value, sizeof(vl_float32_t));
595}
596
610static inline vl_msgpack_iter vlMsgPackSetFloat32Indexed(vl_msgpack* pack, vl_msgpack_iter parent, vl_float32_t value, vl_dsidx_t idx){
611 return vlMsgPackInsert(pack, VL_MSGPACK_FLOAT32, parent, &idx, sizeof(vl_dsidx_t), &value, sizeof(vl_float32_t));
612}
613
627static inline vl_msgpack_iter vlMsgPackSetFloat64Named(vl_msgpack* pack, vl_msgpack_iter parent, vl_float64_t value, const char* key){
628 return vlMsgPackInsert(pack, VL_MSGPACK_FLOAT64, parent, key, strlen(key), &value, sizeof(vl_float64_t));
629}
630
644static inline vl_msgpack_iter vlMsgPackSetFloat64Indexed(vl_msgpack* pack, vl_msgpack_iter parent, vl_float64_t value, vl_dsidx_t idx){
645 return vlMsgPackInsert(pack, VL_MSGPACK_FLOAT64, parent, &idx, sizeof(vl_dsidx_t), &value, sizeof(vl_float64_t));
646}
647
661static inline vl_msgpack_iter vlMsgPackSetStringNamed(vl_msgpack* pack, vl_msgpack_iter parent, const char* value, const char* key){
662 return vlMsgPackInsert(pack, VL_MSGPACK_STRING, parent, key, strlen(key), value, strlen(value));
663}
664
678static inline vl_msgpack_iter vlMsgPackSetStringIndexed(vl_msgpack* pack, vl_msgpack_iter parent, const char* value, vl_dsidx_t idx){
679 return vlMsgPackInsert(pack, VL_MSGPACK_STRING, parent, &idx, sizeof(vl_dsidx_t), value, strlen(value));
680}
681
696static inline vl_msgpack_iter vlMsgPackSetBinaryNamed(vl_msgpack* pack, vl_msgpack_iter parent, const void* value, vl_memsize_t valLen, const char* key){
697 return vlMsgPackInsert(pack, VL_MSGPACK_BINARY, parent, key, strlen(key), value, valLen);
698}
699
714static inline vl_msgpack_iter vlMsgPackSetBinaryIndexed(vl_msgpack* pack, vl_msgpack_iter parent, const void* value, vl_memsize_t valLen, vl_dsidx_t idx){
715 return vlMsgPackInsert(pack, VL_MSGPACK_BINARY, parent, &idx, sizeof(vl_dsidx_t), value, valLen);
716}
717
730static inline vl_bool_t vlMsgPackGetBool(vl_msgpack* pack, vl_msgpack_iter iter, vl_bool_t defaultValue){
731 if(iter == VL_MSGPACK_ITER_INVALID)
732 return defaultValue;
733 if((vlMsgPackType(pack, iter) != VL_MSGPACK_BOOL))
734 return defaultValue;
735
736 return *((const vl_bool_t*) vlMsgPackSampleValue(pack, iter, NULL));
737}
738
751static inline vl_ilarge_t vlMsgPackGetInt(vl_msgpack* pack, vl_msgpack_iter iter, vl_ilarge_t defaultValue){
752 if(iter == VL_MSGPACK_ITER_INVALID)
753 return defaultValue;
754 if((vlMsgPackType(pack, iter) != VL_MSGPACK_INT))
755 return defaultValue;
756
757 return *((const vl_ilarge_t*) vlMsgPackSampleValue(pack, iter, NULL));
758}
759
772static inline vl_ilarge_t vlMsgPackGetUInt(vl_msgpack* pack, vl_msgpack_iter iter, vl_ularge_t defaultValue){
773 if(iter == VL_MSGPACK_ITER_INVALID)
774 return defaultValue;
775 if((vlMsgPackType(pack, iter) != VL_MSGPACK_UINT))
776 return defaultValue;
777
778 return *((const vl_ularge_t*) vlMsgPackSampleValue(pack, iter, NULL));
779}
780
793static inline vl_float32_t vlMsgPackGetFloat32(vl_msgpack* pack, vl_msgpack_iter iter, vl_float32_t defaultValue){
794 if(iter == VL_MSGPACK_ITER_INVALID)
795 return defaultValue;
796 if(vlMsgPackType(pack, iter) != VL_MSGPACK_FLOAT32)
797 return defaultValue;
798
799 return *((const vl_float32_t*) vlMsgPackSampleValue(pack, iter, NULL));
800}
801
814static inline vl_float64_t vlMsgPackGetFloat64(vl_msgpack* pack, vl_msgpack_iter iter, vl_float64_t defaultValue){
815 if(iter == VL_MSGPACK_ITER_INVALID)
816 return defaultValue;
817 if(vlMsgPackType(pack, iter) != VL_MSGPACK_FLOAT64)
818 return defaultValue;
819
820 return *((const vl_float64_t*) vlMsgPackSampleValue(pack, iter, NULL));
821}
822
823#endif //VL_MSGPACK_H
An arena allocator.
Definition vl_arena.h:57
vl_arena_ptr vl_hash_iter
Definition vl_hashtable.h:25
A dynamically-sized hash table.
Definition vl_hashtable.h:45
VL_MEMORY_SIZE_T vl_memsize_t
Definition vl_memory.h:18
VL_MEMORY_T vl_transient
Definition vl_memory.h:85
vl_msgpack_iter vlMsgPackInsert(vl_msgpack *pack, vl_msgpack_type type, vl_msgpack_iter parent, const void *keyPtr, vl_memsize_t keyLen, const void *dataPtr, vl_memsize_t dataLen)
vl_msgpack * vlMsgPackNew()
Allocates and initializes a MessagePack DOM.
Definition vl_msgpack.c:170
vl_msgpack * vlMsgPackClone(vl_msgpack *src, vl_msgpack *dest)
Clones the specified MessagePack DOM to another.
Definition vl_msgpack.c:187
vl_dsidx_t vlMsgPackTotalChildren(vl_msgpack *pack, vl_msgpack_iter iter)
Retrieves the total number of children of a given node.
Definition vl_msgpack.c:202
vl_msgpack_iter vlMsgPackFindChildIndexed(vl_msgpack *pack, vl_msgpack_iter iter, vl_dsidx_t idx)
Finds a child element by index.
Definition vl_msgpack.c:428
vl_msgpack_iter vlMsgPackFindChild(vl_msgpack *pack, vl_msgpack_iter parent, const void *key, vl_memsize_t keyLen)
Finds a child element by key.
Definition vl_msgpack.c:412
vl_msgpack_iter vlMsgPackParent(vl_msgpack *pack, vl_msgpack_iter iter)
Retrieves the parent node of a given node in the MessagePack DOM.
Definition vl_msgpack.c:198
void vlMsgPackClear(vl_msgpack *pack)
Clears the MessagePack DOM, resetting it for reuse.
Definition vl_msgpack.c:181
vl_msgpack_type vlMsgPackType(vl_msgpack *pack, vl_msgpack_iter iter)
Retrieves the type of a given node in the MessagePack DOM.
Definition vl_msgpack.c:234
vl_hashtable nodes
Definition vl_msgpack.h:93
void vlMsgPackInit(vl_msgpack *pack)
Initializes the specified MessagePack DOM.
Definition vl_msgpack.c:156
void vlMsgPackFree(vl_msgpack *pack)
Frees the specified MessagePack DOM.
Definition vl_msgpack.c:164
vl_hash_iter vl_msgpack_iter
Definition vl_msgpack.h:17
vl_msgpack_iter vlMsgPackFirstChild(vl_msgpack *pack, vl_msgpack_iter iter)
Retrieves the first child of a given node.
Definition vl_msgpack.c:214
void vlMsgPackRemove(vl_msgpack *pack, vl_msgpack_iter iter)
Removes an element from the MessagePack DOM.
Definition vl_msgpack.c:344
vl_msgpack_iter vlMsgPackInsertExt(vl_msgpack *pack, vl_msgpack_type type, vl_int8_t subType, vl_msgpack_iter parent, const void *keyPtr, vl_memsize_t keyLen, const void *dataPtr, vl_memsize_t dataLen)
Inserts a new element into the MessagePack DOM with an extended type.
Definition vl_msgpack.c:242
void vlMsgPackDelete(vl_msgpack *pack)
Deletes the specified MessagePack DOM.
Definition vl_msgpack.c:176
vl_msgpack_type
All MessagePack Types.
Definition vl_msgpack.h:22
@ VL_MSGPACK_MAP
MessagePack Map. A collection of node key-value pairs of an arbitrary size.
Definition vl_msgpack.h:63
@ VL_MSGPACK_ARRAY
MessagePack Array. A sequence of dynamically-typed nodes of a well-determined length.
Definition vl_msgpack.h:58
@ VL_MSGPACK_UINT
MessagePack Unsigned Integer. Implemented as vl_ularge_t.
Definition vl_msgpack.h:38
@ VL_MSGPACK_FLOAT64
MessagePack 64-bit Float. Implemented as vl_float64_t.
Definition vl_msgpack.h:46
@ VL_MSGPACK_STRING
MessagePack String. Must have UTF-8 encoding.
Definition vl_msgpack.h:50
@ VL_MSGPACK_BOOL
MessagePack Boolean. Either True (1) or False (0).
Definition vl_msgpack.h:30
@ VL_MSGPACK_EXT
MessagePack Extension Type. Has a type identifier in range of 0...127 and arbitrary data.
Definition vl_msgpack.h:68
@ VL_MSGPACK_FLOAT32
MessagePack 32-bit Float. Implemented as vl_float32_t.
Definition vl_msgpack.h:42
@ VL_MSGPACK_INT
MessagePack Signed Integer. Implemented as vl_ilarge_t.
Definition vl_msgpack.h:34
@ VL_MSGPACK_BINARY
MessagePack Binary. An arbitrary sequence of bytes with a well-defined length.
Definition vl_msgpack.h:54
@ VL_MSGPACK_NIL
MessagePack type equivalent to NIL. Has no data associated with it.
Definition vl_msgpack.h:26
vl_msgpack_iter root
Definition vl_msgpack.h:95
vl_msgpack_iter vlMsgPackPrevSibling(vl_msgpack *pack, vl_msgpack_iter iter)
Retrieves the previous sibling of a given node.
Definition vl_msgpack.c:230
vl_msgpack_iter vlMsgPackNextSibling(vl_msgpack *pack, vl_msgpack_iter iter)
Retrieves the next sibling of a given node.
Definition vl_msgpack.c:226
#define VL_MSGPACK_ITER_INVALID
Definition vl_msgpack.h:9
vl_int8_t vlMsgPackExtType(vl_msgpack *pack, vl_msgpack_iter iter)
Retrieves the extension type of a MessagePack EXT node.
Definition vl_msgpack.c:238
#define VL_MSGPACK_EXT_NONE
Definition vl_msgpack.h:10
const vl_transient * vlMsgPackSampleKey(vl_msgpack *pack, vl_msgpack_iter iter, vl_memsize_t *size)
Retrieves the key associated with an element.
Definition vl_msgpack.c:438
vl_transient * vlMsgPackSampleValue(vl_msgpack *pack, vl_msgpack_iter iter, vl_memsize_t *size)
Retrieves the value associated with an element.
Definition vl_msgpack.c:445
vl_arena values
Definition vl_msgpack.h:94
MessagePack Document Object Model.
Definition vl_msgpack.h:92
VL_BOOL_T vl_bool_t
Definition vl_numtypes.h:128
VL_STRUCTURE_INDEX_T vl_dsidx_t
Index type for data structures.
Definition vl_numtypes.h:13
VL_F32_T vl_float32_t
32-bit floating point number type.
Definition vl_numtypes.h:111
VL_ULARGE_T vl_ularge_t
Largest available unsigned integer type.
Definition vl_numtypes.h:74
VL_ILARGE_T vl_ilarge_t
Largest available signed integer type.
Definition vl_numtypes.h:78