SPH
Assert.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Globals.h"
9 #include <exception>
10 #include <sstream>
11 
13 
14 struct Assert {
15  static bool isTest;
16 
18  static bool throwAssertException;
19 
20  typedef bool (*Handler)(const std::string& message);
21 
26  static Handler handler;
27 
28  class Exception : public std::exception {
29  private:
30  const std::string message;
31 
32  public:
33  Exception(const std::string& message)
34  : message(message) {}
35 
36  virtual const char* what() const noexcept override {
37  return message.c_str();
38  }
39  };
40 
42  const bool originalValue;
43 
46  throwAssertException = true;
47  }
50  }
51  };
52 
53  template <typename T0, typename... TArgs>
54  static void stringify(std::stringstream& ss, T0&& t0, TArgs&&... rest) {
55  ss << t0;
56  if (sizeof...(TArgs) > 0) {
57  ss << " ,";
58  }
59  stringify(ss, std::forward<TArgs>(rest)...);
60  }
61 
62  static void stringify(std::stringstream& UNUSED(ss)) {}
63 
64  template <typename... TArgs>
65  static void fire(const char* message,
66  const char* file,
67  const char* func,
68  const int line,
69  TArgs&&... args) {
70  std::stringstream ss;
71  stringify(ss, std::forward<TArgs>(args)...);
72  fireParams(message, file, func, line, ss.str().c_str());
73  }
74 
75  static void fireParams(const char* message,
76  const char* file,
77  const char* func,
78  const int line,
79  const char* params = "");
80 
81  static void todo(const char* message, const char* func, const int line);
82 };
83 
84 #define TODO(x) Assert::todo(x, __FUNCTION__, __LINE__);
85 
86 #ifdef SPH_DEBUG
87 #define SPH_ASSERT(x, ...) \
88  if (!bool(x)) { \
89  Assert::fire(#x, __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
90  }
91 #define CONSTEXPR_SPH_ASSERT(x) assert(x)
92 #define SPH_ASSERT_UNEVAL(x, ...) SPH_ASSERT(x, ##__VA_ARGS__)
93 #else
94 #define SPH_ASSERT(x, ...) MARK_USED(x)
95 #define CONSTEXPR_SPH_ASSERT(x) MARK_USED(x)
96 #define SPH_ASSERT_UNEVAL(x, ...)
97 #endif
98 
100 #define NOT_IMPLEMENTED \
101  SPH_ASSERT(false, "not implemented"); \
102  throw Assert::Exception(std::string("Functionality not implemented in function ") + __PRETTY_FUNCTION__);
103 
106 #define STOP \
107  SPH_ASSERT(false, "stop"); \
108  throw std::exception();
109 
112 template <typename TDerived, typename TBase>
113 INLINE TDerived assert_cast(TBase* value) {
114  static_assert(std::is_pointer<TDerived>::value, "Must be a pointer type");
115  SPH_ASSERT(!value || dynamic_cast<TDerived>(value) != nullptr, value);
116  return static_cast<TDerived>(value);
117 }
118 
119 #define SPH_STR(x) SPH_XSTR(x)
120 #define SPH_XSTR(x) #x
121 
INLINE TDerived assert_cast(TBase *value)
Definition: Assert.h:113
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Global parameters of the code.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Exception(const std::string &message)
Definition: Assert.h:33
virtual const char * what() const noexcept override
Definition: Assert.h:36
Definition: Assert.h:14
static bool isTest
Definition: Assert.h:15
bool(* Handler)(const std::string &message)
Definition: Assert.h:20
static void fireParams(const char *message, const char *file, const char *func, const int line, const char *params="")
Definition: Assert.cpp:36
static void fire(const char *message, const char *file, const char *func, const int line, TArgs &&... args)
Definition: Assert.h:65
static void stringify(std::stringstream &UNUSED(ss))
Definition: Assert.h:62
static bool throwAssertException
If true, assert throws an exception.
Definition: Assert.h:18
static Handler handler
Custom assert handler.
Definition: Assert.h:26
static void todo(const char *message, const char *func, const int line)
Definition: Assert.cpp:94
static void stringify(std::stringstream &ss, T0 &&t0, TArgs &&... rest)
Definition: Assert.h:54