Arbutils/src/Assert.hpp

42 lines
2.1 KiB
C++

/** @file */
#include <sstream>
#include <stdexcept>
#include <string.h>
#include "Exception.hpp"
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
/// \defgroup Asserts Asserts
/// \brief A set of assertion macros.
#ifndef NO_ASSERT
/// @brief Asserts an expression is true. Throws an exception if the assertion fails.
/// @ingroup Asserts
#define Assert(expr) \
if (!(expr)) { \
std::stringstream ss; \
ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \
ss << #expr << "\""; \
throw ArbUt::Exception(ss.str()); \
}
/// @brief Asserts an expression is true for a range. The value in the range can be retrieved using ``item``.
/// @ingroup Asserts
#define AssertForEach(iterator, assertion) \
{ \
for (auto item : (iterator)) \
Assert(assertion) \
}
#else
// Assert is empty if NO_ASSERT is defined.
#define Assert(expr) ;
#define AssertForEach(iterator, assertion) ;
#endif
/// @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)