_macro.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include <jvalidate/_config.h>
  3. #define JVALIDATE_CONCAT2(A, B) A##B
  4. #define JVALIDATE_CONCAT(A, B) JVALIDATE_CONCAT2(A, B)
  5. #define JVALIDATE_IIF0(IF, ELSE) ELSE
  6. #define JVALIDATE_IIF1(IF, ELSE) IF
  7. #define JVALIDATE_IIF(CONDITIONAL, IF, ELSE) JVALIDATE_CONCAT(JVALIDATE_IIF, CONDITIONAL)(IF, ELSE)
  8. /**
  9. * @brief Assert a certain pre/post-condition is true, else return the default
  10. * expression (or void).
  11. *
  12. * @param condition A boolean or boolean-like expression that should be TRUE.
  13. * If the condition is FALSE, then the other params are used to produce errors.
  14. *
  15. * @param ... Zero or One arguments representing the return value if the
  16. * condition is FALSE. Zero arguments is equivalent to `return void();`, which
  17. * doesn't need to be explicitly stated.
  18. */
  19. #define JVALIDATE_RETURN_UNLESS(condition, ...) \
  20. if (!(condition)) [[unlikely]] { \
  21. return __VA_ARGS__; \
  22. }
  23. /**
  24. * @brief Assert a certain pre/post-condition is false, else return the default
  25. * expression (or void).
  26. *
  27. * @param condition A boolean or boolean-like expression that should be FALSE.
  28. * If the condition is TRUE, then the other params are used to produce errors.
  29. *
  30. * @param ... Zero or One arguments representing the return value if the
  31. * condition is TRUE. Zero arguments is equivalent to `return void();`, which
  32. * doesn't need to be explicitly stated.
  33. */
  34. #define JVALIDATE_RETURN_IF(condition, ...) \
  35. if (condition) [[unlikely]] { \
  36. return __VA_ARGS__; \
  37. }