Drake
drake_throw.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <type_traits>
4 
6 
7 /// @file
8 /// Provides a convenient wrapper to throw an exception when a condition is
9 /// unmet. This is similar to an assertion, but uses exceptions instead of
10 /// ::abort(), and cannot be disabled.
11 
12 namespace drake {
13 namespace detail {
14 // Throw an error message.
15 __attribute__((noreturn)) /* gcc is ok with [[noreturn]]; clang is not. */
16 void Throw(const char* condition, const char* func, const char* file, int line);
17 } // namespace detail
18 } // namespace drake
19 
20 /// Evaluates @p condition and iff the value is false will throw an exception
21 /// with a message showing at least the condition text, function name, file,
22 /// and line.
23 #define DRAKE_THROW_UNLESS(condition) \
24  do { \
25  typedef ::drake::assert::ConditionTraits< \
26  typename std::remove_cv<decltype(condition)>::type> Trait; \
27  static_assert(Trait::is_valid, "Condition should be bool-convertible."); \
28  if (!Trait::Evaluate(condition)) { \
29  ::drake::detail::Throw(#condition, __func__, __FILE__, __LINE__); \
30  } \
31  } while (0)
void Throw(const char *condition, const char *func, const char *file, int line)
Definition: drake_assert_and_throw.cc:53
const char const char int line
Definition: drake_throw.h:16
Definition: automotive_demo.cc:88
Provides Drake&#39;s assertion implementation.
__attribute__((noreturn)) void Throw(const char *condition
const char const char * file
Definition: drake_throw.h:16
const char * func
Definition: drake_throw.h:16