EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
Classes | Namespaces | Macros | Functions
EBGeometry_Macros.hpp File Reference

Utility macros for EBGeometry. More...

#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include "EBGeometry_GPU.hpp"
Include dependency graph for EBGeometry_Macros.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  EBGeometry::RequireError
 What a failed EBGEOMETRY_REQUIRE throws on the host inside a ThrowOnRequire scope: its message, as it would have been printed. More...
 
class  EBGeometry::ThrowOnRequire
 While an object of this class lives, a failed EBGEOMETRY_REQUIRE on this thread throws RequireError instead of aborting: for a program that builds objects from input it does not control (the .ebg reader), so that a bad value is an error it reports, not the end of the program. More...
 

Namespaces

namespace  EBGeometry
 Namespace containing all of EBGeometry's functionality.
 

Macros

#define EBGEOMETRY_REQUIRE(cond, ...)
 Always-on check of user input and one-time host setup.
 
#define EBGEOMETRY_EXPECT(cond)   ((void)0)
 Runtime precondition assertion for EBGeometry.
 

Functions

int & EBGeometry::MacrosDetail::throwingScopes () noexcept
 How many ThrowOnRequire scopes are open on this thread.
 
void EBGeometry::MacrosDetail::requireFailed (const char *a_cond, const char *a_file, const int a_line, const char *a_format,...)
 A failed EBGEOMETRY_REQUIRE on the host: throws RequireError inside a ThrowOnRequire scope, prints and aborts otherwise.
 

Detailed Description

Utility macros for EBGeometry.

Author
Robert Marskar

Macro Definition Documentation

◆ EBGEOMETRY_EXPECT

#define EBGEOMETRY_EXPECT (   cond)    ((void)0)

Runtime precondition assertion for EBGeometry.

When EBGEOMETRY_ENABLE_ASSERTIONS is defined (e.g. via -DEBGEOMETRY_ENABLE_ASSERTIONS on the compiler command line, in CXXFLAGS for GNU Make, or via CMake's target_compile_definitions), this macro evaluates cond and, if it is false, prints a diagnostic message to stderr and calls std::abort().

When EBGEOMETRY_ENABLE_ASSERTIONS is not defined the macro expands to ((void)0): cond is not evaluated at all, so an assertion costs exactly nothing in release. A variable computed solely to be asserted is then unused and must be marked [[maybe_unused]] at its declaration. On a device (CUDA/HIP) compilation pass with assertions enabled the macro prints the failing expression with the device printf and traps, instead of std::fprintf / std::abort. Unlike assert(), this does not depend on NDEBUG, which Release configurations define.

Enabling assertions
# CMake
target_compile_definitions(MyTarget PRIVATE EBGEOMETRY_ENABLE_ASSERTIONS)
# GNU Make
CXXFLAGS += -DEBGEOMETRY_ENABLE_ASSERTIONS
# Command line
g++ -DEBGEOMETRY_ENABLE_ASSERTIONS ...
Parameters
condBoolean-convertible expression to test.

◆ EBGEOMETRY_REQUIRE

#define EBGEOMETRY_REQUIRE (   cond,
  ... 
)
Value:
do { \
if (!(cond)) { \
::EBGeometry::MacrosDetail::requireFailed(#cond, __FILE__, __LINE__, "EBGeometry::" __VA_ARGS__); \
} \
} while (0)

Always-on check of user input and one-time host setup.

Evaluates cond in every build, whether or not EBGEOMETRY_ENABLE_ASSERTIONS is defined. If it is false, prints "EBGeometry::" followed by the formatted message, the failed condition, the file and the line to stderr, and calls std::abort(); inside a ThrowOnRequire scope, it throws RequireError with the message instead (host only).

The library uses two kinds of check:

  • EBGEOMETRY_EXPECT for internal invariants, including on hot paths such as a signed-distance query. It compiles to nothing unless assertions are enabled.
  • EBGEOMETRY_REQUIRE for what a caller controls – constructor arguments, sizes and counts, memory resources – checked once, outside any query loop, where a Release build would otherwise continue into a wrong answer or undefined behavior. Its cost is a few comparisons per object built.

The file readers do not abort on a bad file: they throw EBGeometry::Parser::ParseError.

The message is a printf format string literal, starting with the class or function that checks, followed by its arguments:

EBGEOMETRY_REQUIRE(a_radius > T(0), "SphereSDF: the radius must be positive (%g)", double(a_radius));
#define EBGEOMETRY_REQUIRE(cond,...)
Always-on check of user input and one-time host setup.
Definition EBGeometry_Macros.hpp:200

On a device compilation pass (CUDA/HIP) the macro prints only the failed condition, with the device printf, and traps.

Parameters
condBoolean-convertible expression to test. Always evaluated.
...A printf format string literal, then its arguments.

Function Documentation

◆ requireFailed()

void EBGeometry::MacrosDetail::requireFailed ( const char *  a_cond,
const char *  a_file,
const int  a_line,
const char *  a_format,
  ... 
)
inline

A failed EBGEOMETRY_REQUIRE on the host: throws RequireError inside a ThrowOnRequire scope, prints and aborts otherwise.

One call at each check, out of line, so that a check adds no more code to the function it is in than a call (the message is formatted here, on failure only).

Parameters
[in]a_condThe failed condition.
[in]a_fileThe file.
[in]a_lineThe line.
[in]a_formatThe message, a printf format.
[in]...Its arguments.

◆ throwingScopes()

int & EBGeometry::MacrosDetail::throwingScopes ( )
inlinenoexcept

How many ThrowOnRequire scopes are open on this thread.

Returns
The count.