Arbutils/src/Assert.hpp

29 lines
1.6 KiB
C++

#include <sstream>
#include <stdexcept>
#include <string.h>
#include "Exception.hpp"
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#ifndef NO_ASSERT
#define Assert(expr) \
if (!(expr)) { \
std::stringstream ss; \
ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \
ss << #expr << "\""; \
throw ArbUt::Exception(ss.str()); \
}
#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
#define AssertNotNull(value) Assert(value != nullptr)
#define AssertAllNotNull(iterator) AssertForEach(iterator, item != nullptr)