errormacro.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_errormacro_hh
22 #define mia_core_errormacro_hh
23 
24 
25 #include <iostream>
26 #include <sstream>
27 #include <cassert>
28 #include <stdexcept>
29 #include <mia/core/defines.hh>
30 
31 
33 
34 #if defined(HAVE_CXXABI_H) && defined(HAVE_EXEINFO_H)
35 void append_stacktrace(ostream& os);
36 #endif
37 
38 template <typename V>
39 void __append_message(std::ostream& os, const V& v)
40 {
41  os << v;
42 }
43 
44 inline void __append_message(std::ostream& MIA_PARAM_UNUSED(os))
45 {
46 }
47 
48 template <typename V, typename... T>
49 void __append_message(std::ostream& os, const V& v, T ...t)
50 {
51  os << v;
52  ::mia::__append_message(os, t...);
53 }
54 
55 template <typename... T>
56 const std::string __create_message(T ...t)
57 {
58  std::stringstream msg;
59  ::mia::__append_message(msg, t...);
60 #if defined(HAVE_CXXABI_H) && defined(HAVE_EXEINFO_H)
61  append_stacktrace(msg);
62 #endif
63  return msg.str();
64 }
65 
79 template <typename E, typename... T>
80 E create_exception( T ...t )
81 {
82  return E(::mia::__create_message(t...));
83 }
84 
85 #ifdef NDEBUG
86 
94 #define DEBUG_ASSERT_RELEASE_THROW(cond, msg... ) \
95  if (!(cond)) { \
96  throw ::mia::create_exception<std::logic_error>(msg); \
97  }
98 #else
99 #define DEBUG_ASSERT_RELEASE_THROW(cond, msg... ) \
100  if (!(cond)) { \
101  std::cerr << "Assertion failed: \'" #cond << "\' " << \
102  __FILE__ << ":" << __LINE__ << " " \
103  << ::mia::__create_message(msg) << "\n"; \
104  abort(); \
105  }
106 #endif
107 
109 
110 #endif
E create_exception(T ...t)
helper template to create exceptions with complex messages
Definition: errormacro.hh:80
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
const std::string __create_message(T ...t)
Definition: errormacro.hh:56
void __append_message(std::ostream &os, const V &v)
Definition: errormacro.hh:39
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36