MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
exceptions.hpp
Go to the documentation of this file.
1 /*
2 * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3 * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef __mast__exceptions__
21 #define __mast__exceptions__
22 
23 #include <iostream>
24 #include <string>
25 
26 namespace MAST {
27 namespace Exceptions {
28 
30 
31 public:
32  ExceptionBase(const std::string& cond,
33  const std::string& val_msg,
34  const std::string& msg,
35  const std::string& file,
36  const int line):
37  _cond (cond),
38  _val_msg (val_msg),
39  _msg (msg),
40  _file (file),
41  _line (line)
42  { }
43 
44  virtual ~ExceptionBase() { }
45 
46  void throw_error() {
47 
48  std::cerr
49  << "Condition Violated: "
50  << _cond << std::endl
51  << _val_msg << std::endl
52  << _msg << std::endl
53  << _file << " : " << _line << std::endl;
54 
55  throw 1;
56  }
57 
58 protected:
59 
60  const std::string _cond;
61  const std::string _val_msg;
62  const std::string _msg;
63  const std::string _file;
64  const int _line;
65 };
66 
67 
68 template <typename Val1Type>
69 class Exception1:
71 
72 public:
73 
74  Exception1(const Val1Type v1,
75  const std::string& cond,
76  const std::string& msg,
77  const std::string& file,
78  const int line):
79  MAST::Exceptions::ExceptionBase(cond,
80  "Val 1: " + std::to_string(v1),
81  msg,
82  file,
83  line),
84  _val1 (v1)
85  {}
86 
87  virtual ~Exception1() { }
88 
89 protected:
90 
91  const Val1Type _val1;
92 };
93 
94 
95 template <typename Val1Type, typename Val2Type>
96 class Exception2:
98 
99 public:
100 
101  Exception2(const Val1Type v1,
102  const Val2Type v2,
103  const std::string& cond,
104  const std::string& msg,
105  const std::string& file,
106  const int line):
107  MAST::Exceptions::ExceptionBase(cond,
108  "Val 1: " + std::to_string(v1) + " \nVal 2: " + std::to_string(v2),
109  msg,
110  file,
111  line),
112  _val1 (v1),
113  _val2 (v2)
114  { }
115 
116 protected:
117 
118  Val1Type _val1;
119  Val2Type _val2;
120 };
121 
122 } // namespace Exceptions
123 } // namespace MAST
124 
125 
126 #ifdef NDEBUG
127 
128 #define Assert0(cond, msg) { }
129 #define Assert1(cond, v1, msg) { }
130 #define Assert2(cond, v1, v2, msg) { }
131 
132 #else
133 
134 #define Assert0(cond, msg) \
135  if (!(cond)) \
136  MAST::Exceptions::ExceptionBase \
137  (#cond, \
138  " ", \
139  msg, \
140  __FILE__, \
141  __LINE__).throw_error() \
142 
143 #define Assert1(cond, v1, msg) \
144  if (!(cond)) \
145  MAST::Exceptions::Exception1<decltype(v1)> \
146  (v1, \
147  #cond, \
148  msg, \
149  __FILE__, \
150  __LINE__).throw_error() \
151 
152 #define Assert2(cond, v1, v2, msg) \
153  if (!(cond)) \
154  MAST::Exceptions::Exception2<decltype(v1), decltype(v2)> \
155  (v1, \
156  v2, \
157  #cond, \
158  msg, \
159  __FILE__, \
160  __LINE__).throw_error() \
161 
162 #endif // DEBUG
163 
164 
165 
166 #define Error(cond, msg) \
167  if (!(cond)) \
168  MAST::Exceptions::ExceptionBase( \
169  #cond, \
170  " ", \
171  msg, \
172  __FILE__, \
173  __LINE__).throw_error() \
174 
175 #endif // __mast__exceptions__
ExceptionBase(const std::string &cond, const std::string &val_msg, const std::string &msg, const std::string &file, const int line)
Definition: exceptions.hpp:32
Exception2(const Val1Type v1, const Val2Type v2, const std::string &cond, const std::string &msg, const std::string &file, const int line)
Definition: exceptions.hpp:101
Exception1(const Val1Type v1, const std::string &cond, const std::string &msg, const std::string &file, const int line)
Definition: exceptions.hpp:74