_macro.h 1.8 KB

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