WPILibC++ 2023.4.3
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
12namespace drake {
13namespace internal {
14// Throw an error message.
15[[noreturn]] void Throw(const char* condition, const char* func,
16 const char* file, int line);
17} // namespace internal
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///
24/// The condition must not be a pointer, where we'd implicitly rely on its
25/// nullness. Instead, always write out "!= nullptr" to be precise.
26///
27/// Correct: `DRAKE_THROW_UNLESS(foo != nullptr);`
28/// Incorrect: `DRAKE_THROW_UNLESS(foo);`
29///
30/// Because this macro is intended to provide a useful exception message to
31/// users, we should err on the side of extra detail about the failure. The
32/// meaning of "foo" isolated within error message text does not make it
33/// clear that a null pointer is the proximate cause of the problem.
34#define DRAKE_THROW_UNLESS(condition) \
35 do { \
36 typedef ::drake::assert::ConditionTraits< \
37 typename std::remove_cv_t<decltype(condition)>> Trait; \
38 static_assert(Trait::is_valid, "Condition should be bool-convertible."); \
39 static_assert( \
40 !std::is_pointer_v<decltype(condition)>, \
41 "When using DRAKE_THROW_UNLESS on a raw pointer, always write out " \
42 "DRAKE_THROW_UNLESS(foo != nullptr), do not write DRAKE_THROW_UNLESS" \
43 "(foo) and rely on implicit pointer-to-bool conversion."); \
44 if (!Trait::Evaluate(condition)) { \
45 ::drake::internal::Throw(#condition, __func__, __FILE__, __LINE__); \
46 } \
47 } while (0)
then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file
Definition: ThirdPartyNotices.txt:195
Provides Drake's assertion implementation.
void Throw(const char *condition, const char *func, const char *file, int line)
Definition: drake_assertion_error.h:6
Definition: Eigen_Colamd.h:50