expect.h 3.8 KB

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