Drake
drake_assert.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
5 #include "drake/drakeCommon_export.h"
6 
11 
12 #ifdef DRAKE_DOXYGEN_CXX
13 #define DRAKE_ASSERT(condition)
36 #define DRAKE_ABORT_UNLESS(condition)
40 #define DRAKE_ABORT()
43 #else // DRAKE_DOXYGEN_CXX
44 
45 // Users should NOT set these; only this header should set them.
46 #ifdef DRAKE_ASSERT_IS_ARMED
47 # error Unexpected DRAKE_ASSERT_IS_ARMED defined.
48 #endif
49 #ifdef DRAKE_ASSERT_IS_DISARMED
50 # error Unexpected DRAKE_ASSERT_IS_DISARMED defined.
51 #endif
52 
53 // Decide whether Drake assertions are enabled.
54 #if defined(DRAKE_ENABLE_ASSERTS) && defined(DRAKE_DISABLE_ASSERTS)
55 # error Conflicting assertion toggles.
56 #elif defined(DRAKE_ENABLE_ASSERTS)
57 # define DRAKE_ASSERT_IS_ARMED
58 #elif defined(DRAKE_DISABLE_ASSERTS) || defined(NDEBUG)
59 # define DRAKE_ASSERT_IS_DISARMED
60 #else
61 # define DRAKE_ASSERT_IS_ARMED
62 #endif
63 
64 namespace drake {
65 namespace detail {
66 // Abort the program with an error message.
67 DRAKECOMMON_EXPORT
68 void Abort(const char* condition, const char* func, const char* file, int line);
69 }
70 }
71 
72 #define DRAKE_ABORT() \
73  ::drake::detail::Abort(nullptr, __func__, __FILE__, __LINE__)
74 
75 #define DRAKE_ABORT_UNLESS(condition) \
76  do { \
77  if (!(condition)) { \
78  ::drake::detail::Abort(#condition, __func__, __FILE__, __LINE__); \
79  } \
80  } while (0)
81 
82 #ifdef DRAKE_ASSERT_IS_ARMED
83 // Assertions are enabled.
84 # define DRAKE_ASSERT(condition) DRAKE_ABORT_UNLESS(condition)
85 #else
86 // Assertions are disabled, so just typecheck the expression.
87 # define DRAKE_ASSERT(condition) static_assert( \
88  std::is_convertible<decltype(condition), bool>::value, \
89  "Assertion condition should be bool-convertible.")
90 #endif
91 
92 #endif // DRAKE_DOXYGEN_CXX
Definition: constants.h:3
void Abort(const char *condition, const char *func, const char *file, int line)
Definition: drake_assert.cc:9