khelper.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, nested) \
  71. bool_t k##obj##_add_##nested(k##obj##_t *inst, k##nested##_t *subobj)
  72. #define KADD_NESTED(obj, nested) \
  73. _KADD_NESTED(obj, 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##s, 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. C_DECL_BEGIN
  114. bool_t attr2ctext(char **dst, const char *field, const char *value, int level);
  115. C_DECL_END
  116. #endif // _khelper_h