svZeroDSolver
Loading...
Searching...
No Matches
Assertions.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the
2// University of California, and others. SPDX-License-Identifier: BSD-3-Clause
3/**
4 * @file Assertions.h
5 * @brief eigen_assert override for builds configured with ENABLE_ASSERTIONS
6 */
7#ifndef SVZERODSOLVER_ASSERTIONS_H_
8#define SVZERODSOLVER_ASSERTIONS_H_
9
10#include <stdexcept>
11#include <string>
12
13/**
14 * @brief Report a violated Eigen assertion
15 *
16 * Eigen asserts via assert(), which aborts the process. Inside the svzerod
17 * extension module that would tear down the Python interpreter, so throw
18 * instead: pybind11 surfaces the exception and only the current call fails.
19 *
20 * @param condition Stringified condition that was violated
21 * @param file Source file containing the assertion
22 * @param line Line number of the assertion
23 */
24[[noreturn]] inline void svzerod_eigen_assertion_failed(const char* condition,
25 const char* file,
26 int line) {
27 throw std::runtime_error("Eigen assertion failed: " + std::string(condition) +
28 " at " + std::string(file) + ":" +
29 std::to_string(line));
30}
31
32/**
33 * @brief Eigen assertion that throws instead of aborting
34 *
35 * An expression, not a statement, because Eigen also uses eigen_assert inside
36 * larger expressions.
37 *
38 * @param condition Condition that must hold
39 */
40#define eigen_assert(condition) \
41 ((condition) \
42 ? static_cast<void>(0) \
43 : svzerod_eigen_assertion_failed(#condition, __FILE__, __LINE__))
44
45#endif // SVZERODSOLVER_ASSERTIONS_H_
void svzerod_eigen_assertion_failed(const char *condition, const char *file, int line)
Report a violated Eigen assertion.
Definition Assertions.h:24