macros.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Macros for simplifying to write subsystem's service functions */
  2. #ifndef _clish_macros_h
  3. #define _clish_macros_h
  4. #include <assert.h>
  5. /* Function to get value from structure by name */
  6. #define _CLISH_GET(obj, type, name) \
  7. type clish_##obj##__get_##name(const clish_##obj##_t *inst)
  8. #define CLISH_GET(obj, type, name) \
  9. _CLISH_GET(obj, type, name) { \
  10. assert(inst); \
  11. return inst->name; \
  12. }
  13. #define _CLISH_GET_STR(obj, name) \
  14. _CLISH_GET(obj, const char *, name)
  15. #define CLISH_GET_STR(obj, name) \
  16. CLISH_GET(obj, const char *, name)
  17. /* Function to set value to structure by name */
  18. #define _CLISH_SET(obj, type, name) \
  19. void clish_##obj##__set_##name(clish_##obj##_t *inst, type val)
  20. #define CLISH_SET(obj, type, name) \
  21. _CLISH_SET(obj, type, name) { \
  22. assert(inst); \
  23. inst->name = val; \
  24. }
  25. #define _CLISH_SET_ONCE(obj, type, name) \
  26. _CLISH_SET(obj, type, name)
  27. #define CLISH_SET_ONCE(obj, type, name) \
  28. _CLISH_SET_ONCE(obj, type, name) { \
  29. assert(inst); \
  30. assert(!inst->name); \
  31. inst->name = val; \
  32. }
  33. #define _CLISH_SET_STR(obj, name) \
  34. _CLISH_SET(obj, const char *, name)
  35. #define CLISH_SET_STR(obj, name) \
  36. _CLISH_SET_STR(obj, name) { \
  37. assert(inst); \
  38. lub_string_free(inst->name); \
  39. inst->name = lub_string_dup(val); \
  40. }
  41. #define _CLISH_SET_STR_ONCE(obj, name) \
  42. _CLISH_SET_STR(obj, name)
  43. #define CLISH_SET_STR_ONCE(obj, name) \
  44. _CLISH_SET_STR_ONCE(obj, name) { \
  45. assert(inst); \
  46. assert(!inst->name); \
  47. inst->name = lub_string_dup(val); \
  48. }
  49. #endif // _clish_macros_h