Tomographer  v5.2
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 
49 
50 namespace Tomographer {
51 namespace Tools {
52 
61 class TOMOGRAPHER_EXPORT EigenAssertException : public std::exception
62 {
63  std::string _msg;
64 public:
65  EigenAssertException(const std::string& msg, const std::string& file, const std::size_t line)
66  : _msg("eigen_assert() failed: `" + msg + "' at " + file + ", line " + std::to_string(line))
67  {
68  }
69  virtual ~EigenAssertException() noexcept {}
70 
71  virtual const char * what() const noexcept { return _msg.c_str(); }
72 };
73 
74 } // namespace Tools
75 } // namespace Tomographer
76 
77 
78 
79 // define some Eigen internal magic (undocumented, hope they don't change this in the
80 // future) in case there is a eigen_assert() in a destructor (e.g. CommaInitializer)
81 #define VERIFY_RAISES_ASSERT 1
82 namespace Eigen {
83 typedef Tomographer::Tools::EigenAssertException eigen_assert_exception;
84 }
85 
86 
93 #define eigen_assert_throw_exception(x) \
94  if (!(x)) { \
95  tomographer_eigen_assert_failure_cleanup(); \
96  throw (::Tomographer::Tools::EigenAssertException(#x, __FILE__, __LINE__)); \
97  }
98 
127 
128 
129 
130 #ifdef TOMOGRAPHER_EIGEN_ASSERT_EXCEPTION
131 // override Eigen's eigen_assert() macro
132 #undef eigen_assert
133 #define eigen_assert(x) eigen_assert_throw_exception(x)
134 #endif
135 
136 
137 #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.
Basic definitions, which may have to be defined before any Eigen headers or further utilities are inc...