macros.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (c) 2018-2019 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #pragma once
  23. KS_BEGIN_EXTERN_C
  24. /* CREATE - Constructs the type with default values. Makes a method
  25. * called function_name_##CREATE. Useful for generating the source
  26. * sturctures to later be fed into MARSHAL. */
  27. #define SWCLT_JSON_ALLOC_BEG(function_name, target_type) \
  28. static inline ks_status_t function_name##_ALLOC(ks_pool_t *pool, \
  29. target_type **result) \
  30. { \
  31. ks_status_t status = KS_STATUS_SUCCESS;\
  32. void (*release_cb)(target_type **) = function_name##_DESTROY; \
  33. target_type *target = (target_type *)ks_pool_alloc(pool, sizeof(target_type)); \
  34. \
  35. if (!target) {status = KS_STATUS_NO_MEM; goto end;}
  36. #define SWCLT_JSON_ALLOC_CUSTOM(function_name, key) \
  37. { \
  38. if ((status = function_name##_ALLOC(pool, &target->key))) { \
  39. goto end;\
  40. } \
  41. }
  42. #define SWCLT_JSON_ALLOC_DEFAULT(key, value) \
  43. target->key = value;
  44. #define SWCLT_JSON_ALLOC_END() \
  45. end:\
  46. if (status) { \
  47. if (target) release_cb(&target); \
  48. return status;\
  49. }\
  50. *result = target; \
  51. return KS_STATUS_SUCCESS; \
  52. }
  53. /* PARSE - Populate a structure with distinct copies of all attributes from json.
  54. * Meant to be used in methods returning ks_status_t's.
  55. * Creates a function name function_name_ALLOC, expects a function_name_DESTROY function
  56. * to also exist. */
  57. #define SWCLT_JSON_PARSE_BEG(function_name, target_type) \
  58. static inline ks_status_t function_name##_PARSE(ks_pool_t *pool, \
  59. ks_json_t *object, target_type **result) \
  60. { \
  61. void (*release_cb)(target_type **) = (void(*)(target_type **))function_name##_DESTROY; \
  62. target_type *target = (target_type *)ks_pool_alloc(pool, sizeof(target_type)); \
  63. \
  64. (void)(object); /* suppress unused warnings */ \
  65. (void)(release_cb); /* suppress unused warnings */ \
  66. if (!target) return KS_STATUS_NO_MEM;
  67. #define SWCLT_JSON_PARSE_CUSTOM_OPT(function_name, key) \
  68. { \
  69. ks_json_t *item = ks_json_get_object_item(object, #key); \
  70. ks_status_t status; \
  71. \
  72. if (item) { \
  73. if (!ks_json_type_is_object(item)) { \
  74. release_cb(&target); \
  75. return KS_STATUS_INVALID_ARGUMENT; \
  76. } \
  77. if ((status = function_name##_PARSE(pool, object, &target->key))) { \
  78. release_cb(&target); \
  79. return status; \
  80. } \
  81. } \
  82. }
  83. #define SWCLT_JSON_PARSE_UUID_OPT(key) \
  84. { \
  85. ks_json_t *item = ks_json_get_object_item(object, #key); \
  86. if (item) { \
  87. if (!ks_json_type_is_string(item)) { \
  88. release_cb(&target); \
  89. return KS_STATUS_INVALID_ARGUMENT; \
  90. } \
  91. const char *str = ""; \
  92. ks_json_value_string(item, &str); \
  93. target->key = ks_uuid_from_str(str); \
  94. } \
  95. }
  96. #define SWCLT_JSON_PARSE_BOOL_OPT(key) \
  97. { \
  98. ks_json_t *item = ks_json_get_object_item(object, #key); \
  99. if (item) { \
  100. if (!ks_json_type_is_bool(item)) { \
  101. release_cb(&target); \
  102. return KS_STATUS_INVALID_ARGUMENT; \
  103. } \
  104. ks_json_value_bool(item, &target->key); \
  105. } \
  106. }
  107. #define SWCLT_JSON_PARSE_INT_OPT(key) \
  108. { \
  109. ks_json_t *item = ks_json_get_object_item(object, #key); \
  110. if (item) { \
  111. if (!ks_json_type_is_number(item)) { \
  112. release_cb(&target); \
  113. return KS_STATUS_INVALID_ARGUMENT; \
  114. } \
  115. target->key = ks_json_get_number_int(item, 0); \
  116. } \
  117. }
  118. #define SWCLT_JSON_PARSE_INT_OPT_DEF(key, def) \
  119. { \
  120. ks_json_t *item = ks_json_get_object_item(object, #key); \
  121. if (item) { \
  122. if (!ks_json_type_is_number(item)) { \
  123. release_cb(&target); \
  124. return KS_STATUS_INVALID_ARGUMENT; \
  125. } \
  126. target->key = ks_json_get_number_int(item, 0); \
  127. } else target->key = def; \
  128. }
  129. #define SWCLT_JSON_PARSE_DOUBLE_OPT(key) \
  130. { \
  131. ks_json_t *item = ks_json_get_object_item(object, #key); \
  132. if (item) { \
  133. if (!ks_json_type_is_number(item)) { \
  134. release_cb(&target); \
  135. return KS_STATUS_INVALID_ARGUMENT; \
  136. } \
  137. target->key = ks_json_get_number_double(item, 0); \
  138. } \
  139. }
  140. #define SWCLT_JSON_PARSE_DOUBLE_OPT_DEF(key, def) \
  141. { \
  142. ks_json_t *item = ks_json_get_object_item(object, #key); \
  143. if (item) { \
  144. if (!ks_json_type_is_number(item)) { \
  145. release_cb(&target); \
  146. return KS_STATUS_INVALID_ARGUMENT; \
  147. } \
  148. target->key = ks_json_get_number_double(item, 0); \
  149. } else target->key = def; \
  150. }
  151. #define SWCLT_JSON_PARSE_STRING_OPT(key) \
  152. { \
  153. const char *str = ks_json_get_object_string(object, #key, NULL); \
  154. if (str) { \
  155. if (!(target->key = ks_pstrdup(pool, str))) { \
  156. release_cb(&target); \
  157. return KS_STATUS_NO_MEM; \
  158. } \
  159. } \
  160. }
  161. #define SWCLT_JSON_PARSE_ITEM_OPT(key) \
  162. { \
  163. ks_json_t *item = ks_json_get_object_item(object, #key); \
  164. if (item) { \
  165. if (!(target->key = ks_json_duplicate(item, KS_TRUE))) { \
  166. release_cb(&target); \
  167. return KS_STATUS_NO_MEM; \
  168. } \
  169. } \
  170. }
  171. #define SWCLT_JSON_PARSE_CUSTOM(function_name, key) \
  172. { \
  173. ks_json_t *item = ks_json_get_object_item(object, #key); \
  174. ks_status_t status; \
  175. \
  176. if (!item || !ks_json_type_is_object(item)) { \
  177. release_cb(&target); \
  178. return KS_STATUS_INVALID_ARGUMENT; \
  179. } \
  180. if ((status = function_name##_PARSE(pool, object, &target->key))) { \
  181. release_cb(&target); \
  182. return status; \
  183. } \
  184. }
  185. #define SWCLT_JSON_PARSE_UUID(key) \
  186. { \
  187. ks_json_t *item = ks_json_get_object_item(object, #key); \
  188. if (!item || !ks_json_type_is_string(item)) { \
  189. release_cb(&target); \
  190. return KS_STATUS_INVALID_ARGUMENT; \
  191. } \
  192. const char *str = ""; \
  193. ks_json_value_string(item, &str); \
  194. target->key = ks_uuid_from_str(str); \
  195. }
  196. #define SWCLT_JSON_PARSE_BOOL(key) \
  197. { \
  198. ks_json_t *item = ks_json_get_object_item(object, #key); \
  199. if (!item || !ks_json_type_is_bool(item)) { \
  200. release_cb(&target); \
  201. return KS_STATUS_INVALID_ARGUMENT; \
  202. } \
  203. ks_json_value_bool(item, &target->key); \
  204. }
  205. #define SWCLT_JSON_PARSE_DOUBLE(key) \
  206. { \
  207. ks_json_t *item = ks_json_get_object_item(object, #key); \
  208. if (!item || !ks_json_type_is_number(item)) { \
  209. release_cb(&target); \
  210. return KS_STATUS_INVALID_ARGUMENT; \
  211. } \
  212. ks_json_value_number_double(item, (double *)&target->key); \
  213. }
  214. #define SWCLT_JSON_PARSE_INT_EX(source_key, target_key) \
  215. { \
  216. ks_json_t *item = ks_json_get_object_item(object, #target_key); \
  217. if (!item || !ks_json_type_is_number(item)) { \
  218. release_cb(&target); \
  219. return KS_STATUS_INVALID_ARGUMENT; \
  220. } \
  221. ks_json_value_number_int(item, (int *)&target->source_key); \
  222. }
  223. #define SWCLT_JSON_PARSE_INT(key) \
  224. { \
  225. ks_json_t *item = ks_json_get_object_item(object, #key); \
  226. if (!item || !ks_json_type_is_number(item)) { \
  227. release_cb(&target); \
  228. return KS_STATUS_INVALID_ARGUMENT; \
  229. } \
  230. ks_json_value_number_int(item, (int *)&target->key); \
  231. }
  232. #define SWCLT_JSON_PARSE_STRING(key) \
  233. { \
  234. const char *str = ks_json_get_object_string(object, #key, NULL); \
  235. if (!str) { \
  236. release_cb(&target); \
  237. return KS_STATUS_INVALID_ARGUMENT; \
  238. } \
  239. if (!(target->key = ks_pstrdup(pool, str))) { \
  240. release_cb(&target); \
  241. return KS_STATUS_NO_MEM; \
  242. } \
  243. }
  244. #define SWCLT_JSON_PARSE_ITEM(key) \
  245. { \
  246. ks_json_t *item = ks_json_get_object_item(object, #key); \
  247. if (!item) { \
  248. release_cb(&target); \
  249. return KS_STATUS_INVALID_ARGUMENT; \
  250. } \
  251. if (!(target->key = ks_json_duplicate(item, KS_TRUE))) { \
  252. release_cb(&target); \
  253. return KS_STATUS_NO_MEM; \
  254. } \
  255. }
  256. #define SWCLT_JSON_PARSE_END() \
  257. *result = (void *)target; \
  258. return KS_STATUS_SUCCESS; \
  259. }
  260. /* DESTROY - Opposite of alloc, cleanly tears down and sets items to null.
  261. * Creates a method called function_name_DESTROY. */
  262. #define SWCLT_JSON_DESTROY_BEG(function_name, type) \
  263. static inline void function_name##_DESTROY(type **__free_target) \
  264. { \
  265. type *target = NULL; \
  266. if (!__free_target || !*__free_target) \
  267. return; \
  268. target = *__free_target; \
  269. (void)(target); /* suppress unused warnings */
  270. #define SWCLT_JSON_DESTROY_STRING(key) ks_pool_free(&target->key);
  271. #define SWCLT_JSON_DESTROY_ITEM(key) ks_json_delete((ks_json_t **)&target->key);
  272. #define SWCLT_JSON_DESTROY_UUID(key) /* uuids are by value, null op */
  273. #define SWCLT_JSON_DESTROY_BOOL(key) /* bools are by value, null op */
  274. #define SWCLT_JSON_DESTROY_INT(key) /* ints are by value, null op */
  275. #define SWCLT_JSON_DESTROY_INT_EX(source_key, target_key) /* ints are by value, null op */
  276. #define SWCLT_JSON_DESTROY_DOUBLE(key) /* doubles are by value, null op */
  277. #define SWCLT_JSON_DESTROY_CUSTOM(function_name, key) \
  278. function_name##_DESTROY(&target->key);
  279. #define SWCLT_JSON_DESTROY_END() \
  280. ks_pool_free(__free_target); \
  281. }
  282. /* MARSHAL - Converts the type to a json object, opposite of alloc/parse.
  283. * Creates an inline function called function_name_MARSHAL. */
  284. #define SWCLT_JSON_MARSHAL_BEG(function_name, target_type) \
  285. static inline ks_json_t * function_name##_MARSHAL(target_type *source) \
  286. { \
  287. if (!source) \
  288. return NULL; \
  289. ks_json_t *target = ks_json_create_object(); \
  290. if (!target) \
  291. return NULL;
  292. #define SWCLT_JSON_MARSHAL_UUID(key) \
  293. { \
  294. char *str = ks_uuid_str(NULL, &source->key); \
  295. ks_json_add_string_to_object(target, #key, str); \
  296. ks_pool_free(&str); \
  297. }
  298. #define SWCLT_JSON_MARSHAL_BOOL(key) \
  299. ks_json_add_bool_to_object(target, #key, source->key);
  300. #define SWCLT_JSON_MARSHAL_DOUBLE(key) \
  301. ks_json_add_number_to_object(target, #key, (double)source->key);
  302. #define SWCLT_JSON_MARSHAL_INT_EX(source_key, target_key) \
  303. ks_json_add_number_to_object(target, #target_key, (int)source->source_key);
  304. #define SWCLT_JSON_MARSHAL_INT(key) \
  305. ks_json_add_number_to_object(target, #key, (int)source->key);
  306. #define SWCLT_JSON_MARSHAL_CUSTOM(function_name, key) \
  307. { \
  308. ks_json_t *__custom_obj; \
  309. if (!(__custom_obj = function_name##_MARSHAL(source->key))) { \
  310. ks_json_delete(&target); \
  311. return NULL; \
  312. } \
  313. ks_json_add_item_to_object(target, #key, __custom_obj); \
  314. }
  315. #define SWCLT_JSON_MARSHAL_STRING_OPT(key) \
  316. if (source->key) { \
  317. ks_json_add_string_to_object(target, #key, source->key); \
  318. source->key = NULL; \
  319. }
  320. #define SWCLT_JSON_MARSHAL_STRING(key) \
  321. ks_json_add_string_to_object(target, #key, source->key);
  322. #define SWCLT_JSON_MARSHAL_ITEM_OPT(key) \
  323. if (source->key) { \
  324. ks_json_add_item_to_object(target, #key, source->key); \
  325. source->key = NULL; \
  326. }
  327. #define SWCLT_JSON_MARSHAL_ITEM(key) \
  328. ks_json_add_item_to_object(target, #key, source->key); \
  329. source->key = NULL;
  330. #define SWCLT_JSON_MARSHAL_END() \
  331. return target; \
  332. }
  333. KS_END_EXTERN_C