Commit 476b1c97 authored by David Frank's avatar David Frank
Browse files

Add custom assertions macros

parent d2a9be89
......@@ -3,6 +3,7 @@ set(MODULE_HEADERS
elsaDefines.h
Backtrace.h
Cloneable.h
Utilities/Assertions.h
Utilities/Badge.hpp
Utilities/CartesianIndices.h
Utilities/DataContainerFormatter.hpp
......@@ -37,6 +38,7 @@ set(MODULE_SOURCES
elsaDefines.cpp
Backtrace.cpp
Utilities/CartesianIndices.cpp
Utilities/Assertions.cpp
Descriptors/DataDescriptor.cpp
Descriptors/VolumeDescriptor.cpp
Descriptors/PlanarDetectorDescriptor.cpp
......
#include "Assertions.h"
namespace elsa::detail
{
void assert_nomsg(const char* expr_str, bool expr, const char* file, int line)
{
if (!expr) {
std::cerr << "Assert failed:\n"
<< "Expected:\t" << expr_str << "\n"
<< "Source:\t\t" << file << ", line " << line << "\n";
abort();
}
}
void assert_msg(const char* expr_str, bool expr, const char* file, int line, const char* msg)
{
if (!expr) {
std::cerr << "Assert failed:\t" << msg << "\n"
<< "Expected:\t" << expr_str << "\n"
<< "Source:\t\t" << file << ", line " << line << "\n";
abort();
}
}
} // namespace elsa::detail
#pragma once
#include <iostream>
// Based on the standard paper P0627r0 (see
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0627r0.pdf)
#if defined(_MSC_VER)
#define ELSA_UNREACHABLE() __assume(false);
#elif defined(__GNUC__) or defined(__clang__)
// All gcc/clang compilers supporting c++17 have __builtin_unreachable
#define ELSA_UNREACHABLE() __builtin_unreachable()
#else
#include <exception>
#define ELSA_UNREACHABLE() std::terminate()
#endif
namespace elsa::detail
{
void assert_nomsg(const char* expr_str, bool expr, const char* file, int line);
void assert_msg(const char* expr_str, bool expr, const char* file, int line, const char* msg);
} // namespace elsa::detail
#define GET_MACRO(_1, _2, NAME, ...) NAME
#define ELSA_VERIFY_IMPL2(Expr, Msg) \
::elsa::detail::assert_msg(#Expr, Expr, __FILE__, __LINE__, Msg)
#define ELSA_VERIFY_IMPL1(Expr) ::elsa::detail::assert_nomsg(#Expr, Expr, __FILE__, __LINE__)
#define ELSA_VERIFY(...) GET_MACRO(__VA_ARGS__, ELSA_VERIFY_IMPL2, ELSA_VERIFY_IMPL1)(__VA_ARGS__)
#define ELSA_TODO() ELSA_VERIFY(false, "TODO")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment