-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgpackutil.h
51 lines (41 loc) · 1.06 KB
/
msgpackutil.h
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
/**
* msgpackutil.h - msgpack helper functions
*/
#ifndef __MSGPACKUTIL_H__
#define __MSGPACKUTIL_H__
#include <msgpack.h>
#include <string.h>
/**
* A yak. Yaks carry things. Like messagepack objects. That you serialize.
*/
struct yak {
msgpack_sbuffer buf;
msgpack_packer pack;
};
#define yak_pack(yak, type, data) msgpack_pack_##type(&(yak)->pack, data)
#define yak_array(yak, len) msgpack_pack_array(&(yak)->pack, len)
static inline void
yak_init(struct yak *yak, size_t nelem)
{
msgpack_sbuffer_init(&yak->buf);
msgpack_packer_init(&yak->pack, &yak->buf, msgpack_sbuffer_write);
yak_array(yak, nelem);
}
static inline void
yak_destroy(struct yak *yak)
{
msgpack_sbuffer_destroy(&yak->buf);
}
static inline void
yak_string(struct yak *yak, const char *data, size_t size)
{
msgpack_pack_raw(&yak->pack, size);
msgpack_pack_raw_body(&yak->pack, data, size);
}
static inline void
yak_cstring(struct yak *yak, const char *str)
{
yak_string(yak, str, strlen(str));
}
#define UNYAK(yak) (yak)->buf.data, (yak)->buf.size
#endif // __MSGPACKUTIL_H__