Drake
drake_assert.h File Reference

Provides Drake's assertion implementation. More...

#include <type_traits>
#include "drake/common/drake_compat.h"
Include dependency graph for drake_assert.h:

Go to the source code of this file.

Macros

#define DRAKE_ASSERT(condition)
 DRAKE_ASSERT(condition) is similar to the built-in assert(condition) from the C++ system header <cassert>. More...
 
#define DRAKE_ASSERT_VOID(expression)
 Like DRAKE_ASSERT, except that the expression must be void-valued; this allows for guarding expensive assertion-checking subroutines using the same macros as stand-alone assertions. More...
 
#define DRAKE_DEMAND(condition)
 Evaluates condition and iff the value is false will trigger an assertion failure with a message showing at least the condition text, function name, file, and line. More...
 
#define DRAKE_ABORT()
 Aborts the program (via ::abort) with a message showing at least the function name, file, and line. More...
 
#define DRAKE_ABORT_MSG(message)
 Aborts the program (via ::abort) with a message showing at least the given message (macro argument), function name, file, and line. More...
 

Detailed Description

Provides Drake's assertion implementation.

This is intended to be used both within Drake and by other software. Drake's asserts can be armed and disarmed independently from the system-wide asserts.

Macro Definition Documentation

#define DRAKE_ABORT ( )

Aborts the program (via ::abort) with a message showing at least the function name, file, and line.

#define DRAKE_ABORT_MSG (   message)

Aborts the program (via ::abort) with a message showing at least the given message (macro argument), function name, file, and line.

#define DRAKE_ASSERT (   condition)

DRAKE_ASSERT(condition) is similar to the built-in assert(condition) from the C++ system header <cassert>.

Unless Drake's assertions are disarmed by the pre-processor definitions listed below, DRAKE_ASSERT will evaluate condition and iff the value is false will trigger an assertion failure with a message showing at least the condition text, function name, file, and line.

By default, assertion failures will :abort() the program. However, when using the pydrake python bindings, assertion failures will instead throw a C++ exception that causes a python SystemExit exception.

Assertions are enabled or disabled using the following pre-processor macros:

  • If DRAKE_ENABLE_ASSERTS is defined, then DRAKE_ASSERT is armed.
  • If DRAKE_DISABLE_ASSERTS is defined, then DRAKE_ASSERT is disarmed.
  • If both macros are defined, then it is a compile-time error.
  • If neither are defined, then NDEBUG governs assertions as usual.

This header will define exactly one of either DRAKE_ASSERT_IS_ARMED or DRAKE_ASSERT_IS_DISARMED to indicate whether DRAKE_ASSERT is armed.

One difference versus the standard assert(condition) is that the condition within DRAKE_ASSERT is always syntax-checked, even if Drake's assertions are disarmed.

Treat DRAKE_ASSERT like a statement – it must always be used in block scope, and must always be followed by a semicolon.

#define DRAKE_ASSERT_VOID (   expression)

Like DRAKE_ASSERT, except that the expression must be void-valued; this allows for guarding expensive assertion-checking subroutines using the same macros as stand-alone assertions.

#define DRAKE_DEMAND (   condition)

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