object.h (2717B)
1 #ifndef MPACK_OBJECT_H 2 #define MPACK_OBJECT_H 3 4 #include "mpack_core.h" 5 #include "conv.h" 6 7 #ifndef MPACK_MAX_OBJECT_DEPTH 8 # define MPACK_MAX_OBJECT_DEPTH 32 9 #endif 10 11 #define MPACK_PARENT_NODE(n) (((n) - 1)->pos == (size_t)-1 ? NULL : (n) - 1) 12 13 #define MPACK_THROW(parser) \ 14 do { \ 15 parser->status = MPACK_EXCEPTION; \ 16 return; \ 17 } while (0) 18 19 enum { 20 MPACK_EXCEPTION = -1, 21 MPACK_NOMEM = MPACK_ERROR + 1 22 }; 23 24 /* Storing integer in pointers in undefined behavior according to the C 25 * standard. Define a union type to accommodate arbitrary user data associated 26 * with nodes(and with requests in rpc.h). */ 27 typedef union { 28 void *p; 29 mpack_uintmax_t u; 30 mpack_sintmax_t i; 31 double d; 32 } mpack_data_t; 33 34 typedef struct mpack_node_s { 35 mpack_token_t tok; 36 size_t pos; 37 /* flag to determine if the key was visited when traversing a map */ 38 int key_visited; 39 /* allow 2 instances mpack_data_t per node. the reason is that when 40 * serializing, the user may need to keep track of traversal state besides the 41 * parent node reference */ 42 mpack_data_t data[2]; 43 } mpack_node_t; 44 45 #define MPACK_PARSER_STRUCT(c) \ 46 struct { \ 47 mpack_data_t data; \ 48 mpack_uint32_t size, capacity; \ 49 int status; \ 50 int exiting; \ 51 mpack_tokbuf_t tokbuf; \ 52 mpack_node_t items[c + 1]; \ 53 } 54 55 /* Some compilers warn against anonymous structs: 56 * https://github.com/libmpack/libmpack/issues/6 */ 57 typedef MPACK_PARSER_STRUCT(0) mpack_one_parser_t; 58 59 #define MPACK_PARSER_STRUCT_SIZE(c) \ 60 (sizeof(mpack_node_t) * c + \ 61 sizeof(mpack_one_parser_t)) 62 63 typedef MPACK_PARSER_STRUCT(MPACK_MAX_OBJECT_DEPTH) mpack_parser_t; 64 typedef void(*mpack_walk_cb)(mpack_parser_t *w, mpack_node_t *n); 65 66 MPACK_API void mpack_parser_init(mpack_parser_t *p, mpack_uint32_t c) 67 FUNUSED FNONULL; 68 69 MPACK_API int mpack_parse_tok(mpack_parser_t *walker, mpack_token_t tok, 70 mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) 71 FUNUSED FNONULL_ARG((1,3,4)); 72 MPACK_API int mpack_unparse_tok(mpack_parser_t *walker, mpack_token_t *tok, 73 mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) 74 FUNUSED FNONULL_ARG((1,2,3,4)); 75 76 MPACK_API int mpack_parse(mpack_parser_t *parser, const char **b, size_t *bl, 77 mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) 78 FUNUSED FNONULL_ARG((1,2,3,4,5)); 79 MPACK_API int mpack_unparse(mpack_parser_t *parser, char **b, size_t *bl, 80 mpack_walk_cb enter_cb, mpack_walk_cb exit_cb) 81 FUNUSED FNONULL_ARG((1,2,3,4,5)); 82 83 MPACK_API void mpack_parser_copy(mpack_parser_t *d, mpack_parser_t *s) 84 FUNUSED FNONULL; 85 86 #endif /* MPACK_OBJECT_H */