maliput
drake_throw.h File Reference

Detailed Description

Provides a convenient wrapper to throw an exception when a condition is unmet. This is similar to an assertion, but uses exceptions instead of ::abort(), and cannot be disabled.

#include <type_traits>
#include "maliput/drake/common/drake_assert.h"
Include dependency graph for drake_throw.h:
This graph shows which files directly or indirectly include this file:

Namespaces

 maliput
 Code in this file is inspired by: https://github.com/RobotLocomotion/drake/blob/master/common/text_logging.h.
 
 maliput::drake
 
 maliput::drake::internal
 

Macros

#define MALIPUT_DRAKE_THROW_UNLESS(condition)
 Evaluates condition and iff the value is false will throw an exception with a message showing at least the condition text, function name, file, and line. More...
 

Functions

void Throw (const char *condition, const char *func, const char *file, int line)
 
template<bool >
constexpr void DrakeThrowUnlessWasUsedWithRawPointer ()
 
template<>
constexpr void DrakeThrowUnlessWasUsedWithRawPointer< true > ()
 

Macro Definition Documentation

◆ MALIPUT_DRAKE_THROW_UNLESS

#define MALIPUT_DRAKE_THROW_UNLESS (   condition)
Value:
do { \
typedef ::maliput::drake::assert::ConditionTraits<typename std::remove_cv_t<decltype(condition)>> Trait; \
static_assert(Trait::is_valid, "Condition should be bool-convertible."); \
::maliput::drake::internal::DrakeThrowUnlessWasUsedWithRawPointer<std::is_pointer_v<decltype(condition)>>(); \
if (!Trait::Evaluate(condition)) { \
::maliput::drake::internal::Throw(#condition, __func__, __FILE__, __LINE__); \
} \
} while (0)

Evaluates condition and iff the value is false will throw an exception with a message showing at least the condition text, function name, file, and line.

The condition must not be a pointer, where we'd implicitly rely on its nullness. Instead, always write out "!= nullptr" to be precise.

Correct: MALIPUT_DRAKE_THROW_UNLESS(foo != nullptr); Incorrect: MALIPUT_DRAKE_THROW_UNLESS(foo);

Because this macro is intended to provide a useful exception message to users, we should err on the side of extra detail about the failure. The meaning of "foo" isolated within error message text does not make it clear that a null pointer is the proximate cause of the problem.

maliput::drake::internal::DrakeThrowUnlessWasUsedWithRawPointer
constexpr void DrakeThrowUnlessWasUsedWithRawPointer()
Definition: drake_throw.h:18
maliput::common::internal::Throw
void Throw(const char *condition, const char *func, const char *file, int line)
Definition: maliput_abort_and_throw.cc:111