Arbutils/src/Assert.hpp

39 lines
2.0 KiB
C++
Raw Normal View History

2020-09-24 18:40:21 +00:00
/** @file */
#include "Exception.hpp"
2020-03-22 10:14:47 +00:00
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
2020-09-24 18:40:21 +00:00
/// \defgroup Asserts Asserts
2020-09-24 19:02:22 +00:00
/// \brief A set of assertion macros.
2020-09-24 18:40:21 +00:00
2020-03-22 10:14:47 +00:00
#ifndef NO_ASSERT
2020-09-24 18:40:21 +00:00
/// @brief Asserts an expression is true. Throws an exception if the assertion fails.
/// @ingroup Asserts
2020-03-22 10:14:47 +00:00
#define Assert(expr) \
2020-03-22 11:20:39 +00:00
if (!(expr)) { \
std::stringstream ss; \
ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \
ss << #expr << "\""; \
throw ArbUt::Exception(ss.str()); \
2020-03-22 11:02:02 +00:00
}
2020-09-24 18:40:21 +00:00
/// @brief Asserts an expression is true for a range. The value in the range can be retrieved using ``item``.
/// @ingroup Asserts
2020-04-28 14:55:00 +00:00
#define AssertForEach(iterator, assertion) \
{ \
2020-09-24 18:40:21 +00:00
for (auto item : (iterator)) \
2020-04-28 14:55:00 +00:00
Assert(assertion) \
}
2020-03-22 10:14:47 +00:00
#else
// Assert is empty if NO_ASSERT is defined.
2020-03-22 11:20:39 +00:00
#define Assert(expr) ;
#define AssertForEach(iterator, assertion) ;
2020-03-22 10:14:47 +00:00
#endif
2020-09-24 18:40:21 +00:00
/// @brief Asserts a pointer is not null.
/// @ingroup Asserts
#define AssertNotNull(value) Assert((value) != nullptr)
/// @brief Asserts a range is not null.
/// @ingroup Asserts
#define AssertAllNotNull(iterator) AssertForEach(iterator, item != nullptr)