expect.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <iostream> // IWYU pragma: keep
  3. #include <sstream> // IWYU pragma: keep
  4. #if defined(JVALIDATE_USE_EXCEPTIONS)
  5. /**
  6. * @brief Throw an exception after construcing the error message.
  7. *
  8. * @param extype A subtype of std::exception that can be constructed using a
  9. * std::string.
  10. *
  11. * @param message The error "message" to be emit - in the form of an iostream
  12. * output chain (e.g. `"unsupported index " << i << ", valid items " << items`).
  13. */
  14. #define JVALIDATE_THROW(extype, message) \
  15. do { \
  16. std::stringstream ss; \
  17. ss << message; \
  18. throw extype(ss.str()); \
  19. } while (false)
  20. #else
  21. /**
  22. * @brief Print an error message and then terminate execution
  23. *
  24. * @param extype[ignored]
  25. *
  26. * @param message The error "message" to be emit - in the form of an iostream
  27. * output chain (e.g. `"unsupported index " << i << ", valid items " << items`).
  28. */
  29. #define JVALIDATE_THROW(extype, message) \
  30. do { \
  31. std::cerr << message << std::endl; \
  32. std::terminate(); \
  33. } while (false)
  34. #endif
  35. /**
  36. * @brief Assert a certain pre/post-condition is true, else emit an error of a
  37. * specified type and message.
  38. *
  39. * @param condition A boolean or boolean-like expression that should be TRUE.
  40. * If the condition is FALSE, then the other params are used to produce errors.
  41. *
  42. * @param extype A subtype of std::exception that can be constructed using a
  43. * std::string. If exceptions are enabled, and condition is FALSE - then this
  44. * is the type that will be thrown.
  45. *
  46. * @param message The error "message" to be emit - in the form of an iostream
  47. * output chain (e.g. `"unsupported index " << i << ", valid items " << items`).
  48. */
  49. #define EXPECT_T(condition, extype, message) \
  50. if (!(condition)) [[unlikely]] { \
  51. JVALIDATE_THROW(extype, message); \
  52. }
  53. /**
  54. * @brief Assert a certain pre/post-condition is true, else emit an error of a
  55. * specified message.
  56. *
  57. * @param condition A boolean or boolean-like expression that should be TRUE.
  58. * If the condition is FALSE, then the other params are used to produce errors.
  59. *
  60. * @param message The error "message" to be emit - in the form of an iostream
  61. * output chain (e.g. `"unsupported index " << i << ", valid items " << items`).
  62. */
  63. #define EXPECT_M(condition, message) EXPECT_T(condition, std::runtime_error, message)
  64. /**
  65. * @brief Assert a certain pre/post-condition is true, else emit a generic error.
  66. *
  67. * @param condition A boolean or boolean-like expression that should be TRUE.
  68. * If the condition is FALSE, then the other params are used to produce errors.
  69. */
  70. #define EXPECT(condition) EXPECT_M(condition, #condition " at " __FILE__ ":" << __LINE__)