Drake
drake_assert.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
6 
11 
12 #ifdef DRAKE_DOXYGEN_CXX
13 #define DRAKE_ASSERT(condition)
40 #define DRAKE_ASSERT_VOID(expression)
44 #define DRAKE_DEMAND(condition)
48 #define DRAKE_ABORT()
51 #define DRAKE_ABORT_MSG(message)
54 #else // DRAKE_DOXYGEN_CXX
55 
56 // Users should NOT set these; only this header should set them.
57 #ifdef DRAKE_ASSERT_IS_ARMED
58 # error Unexpected DRAKE_ASSERT_IS_ARMED defined.
59 #endif
60 #ifdef DRAKE_ASSERT_IS_DISARMED
61 # error Unexpected DRAKE_ASSERT_IS_DISARMED defined.
62 #endif
63 
64 // Decide whether Drake assertions are enabled.
65 #if defined(DRAKE_ENABLE_ASSERTS) && defined(DRAKE_DISABLE_ASSERTS)
66 # error Conflicting assertion toggles.
67 #elif defined(DRAKE_ENABLE_ASSERTS)
68 # define DRAKE_ASSERT_IS_ARMED
69 #elif defined(DRAKE_DISABLE_ASSERTS) || defined(NDEBUG)
70 # define DRAKE_ASSERT_IS_DISARMED
71 #else
72 # define DRAKE_ASSERT_IS_ARMED
73 #endif
74 
75 namespace drake {
76 namespace detail {
77 // Abort the program with an error message.
78 __attribute__((noreturn)) /* gcc is ok with [[noreturn]]; clang is not. */
79 void Abort(const char* condition, const char* func, const char* file, int line);
80 // Report an assertion failure; will either Abort(...) or throw.
81 __attribute__((noreturn)) /* gcc is ok with [[noreturn]]; clang is not. */
82 void AssertionFailed(
83  const char* condition, const char* func, const char* file, int line);
84 } // namespace detail
85 namespace assert {
86 // Allows for specialization of how to bool-convert Conditions used in
87 // assertions, in case they are not intrinsically convertible. See
88 // symbolic_formula.h for an example use. This is a public interface to
89 // extend; it is intended to be specialized by unusual Scalar types that
90 // require special handling.
91 template <typename Condition>
92 struct ConditionTraits {
93  static constexpr bool is_valid = std::is_convertible<Condition, bool>::value;
94  static bool Evaluate(const Condition& value) {
95  return value;
96  }
97 };
98 } // namespace assert
99 } // namespace drake
100 
101 #define DRAKE_ABORT() \
102  ::drake::detail::Abort(nullptr, __func__, __FILE__, __LINE__)
103 
104 #define DRAKE_DEMAND(condition) \
105  do { \
106  typedef ::drake::assert::ConditionTraits< \
107  typename std::remove_cv<decltype(condition)>::type> Trait; \
108  static_assert(Trait::is_valid, "Condition should be bool-convertible."); \
109  if (!Trait::Evaluate(condition)) { \
110  ::drake::detail::AssertionFailed( \
111  #condition, __func__, __FILE__, __LINE__); \
112  } \
113  } while (0)
114 
115 #define DRAKE_ABORT_MSG(msg) \
116  ::drake::detail::Abort(msg, __func__, __FILE__, __LINE__)
117 
118 #ifdef DRAKE_ASSERT_IS_ARMED
119 // Assertions are enabled.
120 # define DRAKE_ASSERT(condition) DRAKE_DEMAND(condition)
121 # define DRAKE_ASSERT_VOID(expression) do { \
122  static_assert( \
123  std::is_convertible<decltype(expression), void>::value, \
124  "Expression should be void."); \
125  expression; \
126  } while (0)
127 #else
128 // Assertions are disabled, so just typecheck the expression.
129 # define DRAKE_ASSERT(condition) static_assert( \
130  ::drake::assert::ConditionTraits< \
131  typename std::remove_cv<decltype(condition)>::type>::is_valid, \
132  "Condition should be bool-convertible.");
133 # define DRAKE_ASSERT_VOID(expression) static_assert( \
134  std::is_convertible<decltype(expression), void>::value, \
135  "Expression should be void.")
136 #endif
137 
138 #endif // DRAKE_DOXYGEN_CXX
const char const char int line
Definition: drake_throw.h:16
Definition: automotive_demo.cc:88
This header provides a std::make_unique implementation to be used when the compiler does not support ...
void AssertionFailed(const char *condition, const char *func, const char *file, int line)
Definition: drake_assert_and_throw.cc:61
int value
Definition: copyable_unique_ptr_test.cc:61
__attribute__((noreturn)) void Throw(const char *condition
const char const char * file
Definition: drake_throw.h:16
void Abort(const char *condition, const char *func, const char *file, int line)
Definition: drake_assert_and_throw.cc:44
const char * func
Definition: drake_throw.h:16