Tomographer  v3.0
Tomographer C++ Framework Documentation
eigen_assert_exception.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #ifndef TOMOGRAPHER_TOOLS_EIGEN_ASSERT_EXCEPTION_H
29 #define TOMOGRAPHER_TOOLS_EIGEN_ASSERT_EXCEPTION_H
30 
45 #include <string>
46 #include <stdexcept>
47 
48 namespace Tomographer {
49 namespace Tools {
50 
60 {
61  std::string _msg;
62 public:
63  EigenAssertException(const std::string& msg, const std::string& file, const std::size_t line)
64  : _msg("eigen_assert() failed: `" + msg + "' at " + file + ", line " + std::to_string(line))
65  {
66  }
67  virtual ~EigenAssertException() noexcept {}
68 
69  virtual const char * what() const noexcept { return _msg.c_str(); }
70 };
71 
72 } // namespace Tools
73 } // namespace Tomographer
74 
75 
76 
77 // define some Eigen internal magic (undocumented, hope they don't change this in the
78 // future) in case there is a eigen_assert() in a destructor (e.g. CommaInitializer)
79 #define VERIFY_RAISES_ASSERT 1
80 namespace Eigen {
81 typedef Tomographer::Tools::EigenAssertException eigen_assert_exception;
82 }
83 
84 
91 #define eigen_assert_throw_exception(x) \
92  if (!(x)) { \
93  tomographer_eigen_assert_failure_cleanup(); \
94  throw (::Tomographer::Tools::EigenAssertException(#x, __FILE__, __LINE__)); \
95  }
96 
125 
126 
127 
128 #ifdef TOMOGRAPHER_EIGEN_ASSERT_EXCEPTION
129 // override Eigen's eigen_assert() macro
130 #undef eigen_assert
131 #define eigen_assert(x) eigen_assert_throw_exception(x)
132 #endif
133 
134 
135 #endif
Base namespace for the Tomographer project.
Definition: densellh.h:45
T to_string(T... args)
void tomographer_eigen_assert_failure_cleanup()
Gets called when we use eigen assertion exceptions and when eigen_assert() fails. ...
Exception that is thrown upon failed eigen_assert.
STL class.
STL class.
T c_str(T... args)