Lagrange
Assert and errors

Assertions and exceptions. More...

Namespaces

namespace  lagrange
 Main namespace for Lagrange.
 

Classes

struct  Error
 Base exception for errors thrown by Lagrange functions. More...
 
struct  BadCastError
 An exception of this type is thrown when a lagrange::safe_cast<> fails. More...
 

Macros

#define la_runtime_assert(...)
 Runtime assertion check. More...
 
#define la_debug_assert(...)
 Debug assertion check. More...
 

Functions

LA_CORE_API void set_breakpoint_enabled (bool enabled)
 Sets whether to trigger a debugger breakpoint on assert failure. More...
 
LA_CORE_API bool is_breakpoint_enabled ()
 Returns whether to trigger a debugger breakpoint on assert failure. More...
 
LA_CORE_API void trigger_breakpoint ()
 Call to explicitly trigger a debugger breakpoint.
 
LA_CORE_API bool assertion_failed (const char *function, const char *file, unsigned int line, const char *condition, std::string_view message)
 Called in case of an assertion failure. More...
 

Detailed Description

Assertions and exceptions.

A failed assertion will throw an exception lagrange::Error with a specific error message, and can be caught by client code. Lagrange code uses two types of assertions:

Note that assert macros behave as functions, meaning they expand to a void expression and can be used in an expression such as y = (la_debug_assert(true), x).

Finally, our assertion macros can take either 1 or 2 arguments, the second argument being an optional error message. To conveniently format assertion messages with a printf-like syntax, use fmt::format:

#include <lagrange/utils/assert.h>
#include <spdlog/fmt/fmt.h>
la_debug_assert(x == 3, "Error message");
la_debug_assert(x == 3, fmt::format("Incorrect value of x: {}", x));
#define la_debug_assert(...)
Debug assertion check.
Definition: assert.h:189

Macro Definition Documentation

◆ la_runtime_assert

#define la_runtime_assert (   ...)

#include <lagrange/utils/assert.h>

Runtime assertion check.

This check is executed for both Debug and Release configurations, and should be used, e.g., to check the validity of user-given inputs.

Parameters
conditionCondition to check at runtime.
messageOptional message argument.
Returns
Void expression.

◆ la_debug_assert

#define la_debug_assert (   ...)

#include <lagrange/utils/assert.h>

Debug assertion check.

This check is executed only for Debug configurations, and should be used as a sanity check for situations that should never arise in the program's normal execution (e.g., a doubled linked list is malformed).

Parameters
conditionCondition to check at runtime.
messageOptional message argument.
Returns
Void expression.

Function Documentation

◆ set_breakpoint_enabled()

void set_breakpoint_enabled ( bool  enabled)

#include <lagrange/utils/assert.h>

Sets whether to trigger a debugger breakpoint on assert failure.

Use this function in unit test, around lines that are supposed to throw, to avoid spurious breakpoints when running unit tests.

Parameters
[in]enabledTrue to enable breakpoint debugging, false to disable.

◆ is_breakpoint_enabled()

bool is_breakpoint_enabled ( )

#include <lagrange/utils/assert.h>

Returns whether to trigger a debugger breakpoint on assert failure.

Use this function in a unit test to restore previous behavior when explicitly disabling triggering debugger breakpoint.

Returns
True if assert failure triggers a debugger breakpoint, False otherwise.

◆ assertion_failed()

LA_IGNORE_NONVOID_NORETURN_WARNING_BEGIN bool assertion_failed ( const char *  function,
const char *  file,
unsigned int  line,
const char *  condition,
std::string_view  message 
)

#include <lagrange/utils/assert.h>

Called in case of an assertion failure.

Parameters
[in]functionFunction in which the assertion failed.
[in]fileFile in which the assertion failed.
[in]lineLine where the assertion failed.
[in]conditionAssert condition that failed.
[in]messageOptional error message associated with the assertion.
Returns
Boolean type returned by this function. While this function never returns, the return boolean type allows it to be called in an expression such as foo && assertion_failed(...)