khelper.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /** @file khelper.h
  2. * @brief Macros for simplifying to write subsystem's service functions.
  3. */
  4. #ifndef _khelper_h
  5. #define _khelper_h
  6. #include <faux/faux.h>
  7. #include <faux/str.h>
  8. // Function to get value from structure by name
  9. #define _KGET(obj, type, name) \
  10. type k##obj##_##name(const k##obj##_t *inst)
  11. #define KGET(obj, type, name) \
  12. _KGET(obj, type, name) { \
  13. assert(inst); \
  14. return inst->name; \
  15. }
  16. #define _KGET_STR(obj, name) \
  17. _KGET(obj, const char *, name)
  18. #define KGET_STR(obj, name) \
  19. KGET(obj, const char *, name)
  20. #define _KGET_BOOL(obj, name) \
  21. _KGET(obj, bool_t, name)
  22. #define KGET_BOOL(obj, name) \
  23. KGET(obj, bool_t, name)
  24. // Function to set value to structure by name
  25. #define _KSET(obj, type, name) \
  26. bool_t k##obj##_set_##name(k##obj##_t *inst, type val)
  27. #define KSET(obj, type, name) \
  28. _KSET(obj, type, name) { \
  29. assert(inst); \
  30. inst->name = val; \
  31. return BOOL_TRUE; \
  32. }
  33. #define _KSET_ONCE(obj, type, name) \
  34. _KSET(obj, type, name)
  35. #define KSET_ONCE(obj, type, name) \
  36. _KSET_ONCE(obj, type, name) { \
  37. assert(inst); \
  38. assert(!inst->name); \
  39. inst->name = val; \
  40. }
  41. #define _KSET_STR(obj, name) \
  42. _KSET(obj, const char *, name)
  43. #define KSET_STR(obj, name) \
  44. _KSET_STR(obj, name) { \
  45. assert(inst); \
  46. faux_str_free(inst->name); \
  47. inst->name = faux_str_dup(val); \
  48. return BOOL_TRUE; \
  49. }
  50. #define _KSET_STR_ONCE(obj, name) \
  51. _KSET_STR(obj, name)
  52. #define KSET_STR_ONCE(obj, name) \
  53. _KSET_STR_ONCE(obj, name) { \
  54. assert(inst); \
  55. if (inst->name) { \
  56. if (NULL == val) \
  57. return BOOL_FALSE; \
  58. if (strcmp(inst->name, val) == 0) \
  59. return BOOL_TRUE; \
  60. return BOOL_FALSE; \
  61. } \
  62. inst->name = faux_str_dup(val); \
  63. return BOOL_TRUE; \
  64. }
  65. #define _KSET_BOOL(obj, name) \
  66. _KSET(obj, bool_t, name)
  67. #define KSET_BOOL(obj, name) \
  68. KSET(obj, bool_t, name)
  69. // Function to add object to list
  70. #define _KADD_NESTED(obj, type, nested) \
  71. bool_t k##obj##_add_##nested(k##obj##_t *inst, type subobj)
  72. #define KADD_NESTED(obj, type, nested) \
  73. _KADD_NESTED(obj, type, nested) { \
  74. assert(inst); \
  75. if (!inst) \
  76. return BOOL_FALSE; \
  77. assert(subobj); \
  78. if (!subobj) \
  79. return BOOL_FALSE; \
  80. if (!faux_list_add(inst->nested, (void *)subobj)) \
  81. return BOOL_FALSE; \
  82. return BOOL_TRUE; \
  83. }
  84. #define _KFIND_NESTED(obj, nested) \
  85. k##nested##_t *k##obj##_find_##nested(const k##obj##_t *inst, const char *str_key)
  86. #define KFIND_NESTED(obj, nested) \
  87. _KFIND_NESTED(obj, nested) { \
  88. assert(inst); \
  89. if (!inst) \
  90. return NULL; \
  91. assert(str_key); \
  92. if (!str_key) \
  93. return NULL; \
  94. return (k##nested##_t *)faux_list_kfind(inst->nested##s, str_key); \
  95. }
  96. // Compare functions. For lists
  97. #define _KCMP_NESTED(obj, nested, field) \
  98. int k##obj##_##nested##_compare(const void *first, const void *second)
  99. #define KCMP_NESTED(obj, nested, field) \
  100. _KCMP_NESTED(obj, nested, field) { \
  101. const k##nested##_t *f = (const k##nested##_t *)first; \
  102. const k##nested##_t *s = (const k##nested##_t *)second; \
  103. return strcmp(k##nested##_##field(f), k##nested##_##field(s)); \
  104. }
  105. #define _KCMP_NESTED_BY_KEY(obj, nested, field) \
  106. int k##obj##_##nested##_kcompare(const void *key, const void *list_item)
  107. #define KCMP_NESTED_BY_KEY(obj, nested, field) \
  108. _KCMP_NESTED_BY_KEY(obj, nested, field) { \
  109. const char *f = (const char *)key; \
  110. const k##nested##_t *s = (const k##nested##_t *)list_item; \
  111. return strcmp(f, k##nested##_##field(s)); \
  112. }
  113. #define _KNESTED_LEN(obj, nested) \
  114. ssize_t k##obj##_##nested##_len(const k##obj##_t *inst)
  115. #define KNESTED_LEN(obj, nested) \
  116. _KNESTED_LEN(obj, nested) { \
  117. assert(inst); \
  118. if (!inst) \
  119. return -1; \
  120. return faux_list_len(inst->nested); \
  121. }
  122. #define _KNESTED_IS_EMPTY(obj, nested) \
  123. bool_t k##obj##_##nested##_is_empty(const k##obj##_t *inst)
  124. #define KNESTED_IS_EMPTY(obj, nested) \
  125. _KNESTED_IS_EMPTY(obj, nested) { \
  126. assert(inst); \
  127. if (!inst) \
  128. return -1; \
  129. return faux_list_is_empty(inst->nested); \
  130. }
  131. #define _KNESTED_ITER(obj, nested) \
  132. k##obj##_##nested##_node_t *k##obj##_##nested##_iter(const k##obj##_t *inst)
  133. #define KNESTED_ITER(obj, nested) \
  134. _KNESTED_ITER(obj, nested) { \
  135. assert(inst); \
  136. if (!inst) \
  137. return NULL; \
  138. return (k##obj##_##nested##_node_t *)faux_list_head(inst->nested); \
  139. }
  140. #define _KNESTED_ITERR(obj, nested) \
  141. k##obj##_##nested##_node_t *k##obj##_##nested##_iterr(const k##obj##_t *inst)
  142. #define KNESTED_ITERR(obj, nested) \
  143. _KNESTED_ITERR(obj, nested) { \
  144. assert(inst); \
  145. if (!inst) \
  146. return NULL; \
  147. return (k##obj##_##nested##_node_t *)faux_list_tail(inst->nested); \
  148. }
  149. #define _KNESTED_EACH(obj, type, nested) \
  150. type k##obj##_##nested##_each(k##obj##_##nested##_node_t **iter)
  151. #define KNESTED_EACH(obj, type, nested) \
  152. _KNESTED_EACH(obj, type, nested) { \
  153. return (type)faux_list_each((faux_list_node_t **)iter); \
  154. }
  155. #define _KNESTED_EACHR(obj, type, nested) \
  156. type k##obj##_##nested##_eachr(k##obj##_##nested##_node_t **iter)
  157. #define KNESTED_EACHR(obj, type, nested) \
  158. _KNESTED_EACHR(obj, type, nested) { \
  159. return (type)faux_list_eachr((faux_list_node_t **)iter); \
  160. }
  161. C_DECL_BEGIN
  162. bool_t attr2ctext(char **dst, const char *field, const char *value, int level);
  163. C_DECL_END
  164. #endif // _khelper_h