Tomographer  v5.4
Tomographer C++ Framework Documentation
eigen_assert_exception.h File Reference

(v5.4)

Define tools for Eigen's eigen_assert() to throw an exception instead of assert'ing. More...

#include <string>
#include <stdexcept>
#include <tomographer/tools/cxxdefs.h>
+ Include dependency graph for eigen_assert_exception.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Tomographer::Tools::EigenAssertException
 Exception that is thrown upon failed eigen_assert. More...
 

Namespaces

 Tomographer
 Base namespace for the Tomographer project.
 
 Tomographer::Tools
 Various useful tools.
 

Macros

#define VERIFY_RAISES_ASSERT   1
 
#define eigen_assert_throw_exception(x)
 Macro like eigen_assert(), but which throws an exception. More...
 

Typedefs

typedef Tomographer::Tools::EigenAssertException Eigen::eigen_assert_exception
 

Functions

void tomographer_eigen_assert_failure_cleanup ()
 Gets called when we use eigen assertion exceptions and when eigen_assert() fails. More...
 

Detailed Description

Define tools for Eigen's eigen_assert() to throw an exception instead of assert'ing.

Note
This header must be included BEFORE any Eigen header!
If you define the preprocessor symbol TOMOGRAPHER_EIGEN_ASSERT_EXCEPTION before including this file, then the macro eigen_assert() is defined such that it throws an exception upon failure. (In this case, eigen_assert() is defined to call eigen_assert_throw_exception().)

Definition in file eigen_assert_exception.h.

Macro Definition Documentation

§ eigen_assert_throw_exception

#define eigen_assert_throw_exception (   x)
Value:
if (!(x)) { \
tomographer_eigen_assert_failure_cleanup(); \
throw (::Tomographer::Tools::EigenAssertException(#x, __FILE__, __LINE__)); \
}
Exception that is thrown upon failed eigen_assert.

Macro like eigen_assert(), but which throws an exception.

You can use this macro in a definition of "eigen_assert()". If the assertion condition fails, then an exception of type Tomographer::Tools::EigenAssertException is thrown.

Definition at line 93 of file eigen_assert_exception.h.

Function Documentation

§ tomographer_eigen_assert_failure_cleanup()

void tomographer_eigen_assert_failure_cleanup ( )
inline

Gets called when we use eigen assertion exceptions and when eigen_assert() fails.

This function does nothing. However, you can override this function depending on the context, by locally defining another function called tomographer_eigen_assert_failure_cleanup(). Because eigen_assert_throw_exception() is a macro which, in its expansion, invokes tomographer_eigen_assert_failure_cleanup(), the closest function context-wise is invoked.

This mechanism should be used in case classes must clean up after an eigen assertion failure. The problem is the following: for instance, suppose a class X has calls to eigen_assert() in its destructor (note that such destructors must be declared noexcept(false) explicitly, as per C++11 standard). Now imaging that there is an other eigen_assert() failure somewhere else in one of X's methods, causing an exception to be thrown. If the instance of X goes out of scope before the exception is caught, it is highly likely that the eigen_assert() in the destructor also fails. In this case, no exception is thrown and std::terminate() is called (with g++ 6 and clang++; not sure what the C++ standard mandates). To solve this, X should provide a private method called tomographer_eigen_assert_failure_cleanup() which sets the class back to a consistent state, after which eigen_assert() tests in the destructor won't fail.

This problem arises in particular for Eigen's Eigen::CommaInitializer class; we use the above mechanism in a patched class in the tests to avoid this problem (see test/test_tomographer.h).

Definition at line 126 of file eigen_assert_exception.h.