b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2exception.H
Go to the documentation of this file.
1//------------------------------------------------------------------------
2// b2exception.H --
3//
4//
5// written by Mathias Doreille
6//
7// Copyright (c) 2004-2012 SMR Engineering & Development SA
8// 2502 Bienne, Switzerland
9//
10// All Rights Reserved. Proprietary source code. The contents of
11// this file may not be disclosed to third parties, copied or
12// duplicated in any form, in whole or in part, without the prior
13// written permission of SMR.
14//----------------------------------------------------------------------*/
15
16#ifndef __B2EXCEPTION_H__
17#define __B2EXCEPTION_H__
18
108#include <exception>
109#include <iostream>
110#include <sstream>
111#include <string>
112
113#include "b2ppconfig.h"
114#include "utils/b2util.H"
115
116#ifdef HAVE_BACKTRACE
117#include <execinfo.h>
118
119#include <vector>
120#endif
121
122extern "C" void b2throw_callback();
123
124namespace b2000 {
125
131class Exception : public std::exception {
132public:
143 Exception(const std::string& msg_ = std::string(), const char* file_ = nullptr, int line_ = -1)
144 : msg(msg_), file(file_), line(line_) {
145#ifdef HAVE_BACKTRACE
146 void* bt[128];
147 int bt_size = ::backtrace(bt, 128);
148 backtrace.insert(backtrace.begin(), bt, bt + bt_size);
149#endif
150 b2throw_callback();
151 }
152
154 const char* name() const noexcept { return typeid(Exception).name(); }
155
156 virtual const char* get_name() const noexcept { return "Exception"; }
157
159 const char* get_file() const { return file ? file : "??"; }
160
162 int get_line() const { return line; }
163
165 const std::string& get_msg() const { return msg; }
166
168 const std::string& get_backtrace(int start_level = 1) const;
169
170 ~Exception() noexcept override {}
171
174 struct Throw {
175 Throw(const char* file_, int line_) : file(file_), line(line_) {}
176
177 const char* file;
178 int line;
179 };
180
183 struct Warning {
184 Warning(const char* file_, int line_) : file(file_), line(line_) {}
185
186 const char* file;
187 int line;
188 };
189
198#define THROW Exception::Throw(__FILE__, __LINE__)
199#define WARNING Exception::Warning(__FILE__, __LINE__)
200
202 template <typename T>
203 Exception& operator<<(const T& t) {
204 std::ostringstream o;
205 o << t;
206 msg += o.str();
207 return *this;
208 }
209
211 template <typename T1, typename T2>
212 Exception& operator<<(const std::pair<T1, T2>& t) {
213 std::ostringstream o;
214 o << "(" << t.first << "," << t.second << ")";
215 msg += o.str();
216 return *this;
217 }
218
221 void operator<<(const Throw& t) {
222 if (indicate_location) { set_location(t); }
223 throw *this;
224 }
225
227 void operator<<(const Warning& w) {
228 std::cerr << "Warning: " << msg;
229 if (indicate_location) { std::cerr << " File " << w.file << ":" << w.line; }
230 std::cerr << std::endl;
231 }
232
233#ifdef HAVE_LIB_TBB
235 const char* what() const noexcept override { return msg.c_str(); }
236
238 Exception* move() noexcept {
239 void* e = ::operator new(sizeof(Exception));
240 return ::new (e) Exception(*this);
241 }
242
244 void destroy() noexcept { delete this; }
245
247 void throw_self() { throw *this; }
248#endif
249
250protected:
251 void set_msg(const std::string& msg_) { msg = msg_; }
252
253 void set_location(const Throw& t) {
254 file = t.file;
255 line = t.line;
256 }
257
258 static bool indicate_location;
259
260private:
261 std::string msg;
262 const char* file;
263 int line;
264#ifdef HAVE_BACKTRACE
265 std::vector<void*> backtrace;
266#endif
267};
268
269inline std::ostream& operator<<(std::ostream& out, const Exception& exception) {
270 out << exception.get_name() << ": " << exception.get_msg();
271 return out;
272}
273
275template <const char* NAME>
277public:
279 const std::string& msg_ = std::string(), const char* file_ = nullptr, int line_ = -1)
280 : Exception(msg_, file_, line_) {}
281
282 const char* name() const noexcept { return typeid(GenericException).name(); }
283
284 const char* get_name() const noexcept override { return NAME; }
285
286 template <typename T>
287 GenericException& operator<<(const T& t) {
289 return *this;
290 }
291
292 GenericException& operator<<(const Throw& t) {
293 set_location(t);
294 throw *this;
295 return *this;
296 }
297
298#ifdef HAVE_LIB_TBB
299 GenericException* move() noexcept {
300 void* e = ::operator new(sizeof(GenericException));
301 return ::new (e) GenericException(*this);
302 }
303
304 void destroy() noexcept { delete this; }
305
306 void throw_self() { throw *this; }
307#endif
308};
309
310extern const char UnimplementedError_name[];
311
315
316extern const char KeyError_name[];
317
321
322extern const char TypeError_name[];
323
326
327extern const char ValueError_name[];
328
331
332extern const char IOError_name[];
333
336
337extern const char DBError_name[];
338
341
342extern const char SizeError_name[];
343
347
348extern const char ConvergenceError_name[];
350
351extern const char OutOfMemoryError_name[];
353
354extern const char KeyboardInterrupt_name[];
356
357extern const char SystemError_name[];
359
360extern const char LoggingError_name[];
361
364
365extern const char PythonError_name[];
366
372
373#ifndef NDEBUG
374template <typename R, typename T>
375inline R static_cast_check(T t, std::string msg = std::string()) {
376 R r = dynamic_cast<R>(t);
377 if (r == 0) { TypeError() << msg << THROW; }
378 return r;
379}
380#else
381template <typename R, typename T>
382inline R static_cast_check(T t, std::string mgs = std::string()) {
383 return static_cast<R>(t);
384}
385#endif
386
387} // namespace b2000
388
389#endif /* __B2EXCEPTION_H__ */
#define THROW
Definition b2exception.H:198
Definition b2exception.H:131
Exception & operator<<(const T &t)
Definition b2exception.H:203
const char * name() const noexcept
Definition b2exception.H:154
const std::string & get_backtrace(int start_level=1) const
Definition b2exception.C:47
int get_line() const
Definition b2exception.H:162
const std::string & get_msg() const
Definition b2exception.H:165
Exception(const std::string &msg_=std::string(), const char *file_=nullptr, int line_=-1)
Definition b2exception.H:143
Exception & operator<<(const std::pair< T1, T2 > &t)
Definition b2exception.H:212
const char * get_file() const
Definition b2exception.H:159
void operator<<(const Throw &t)
Definition b2exception.H:221
void operator<<(const Warning &w)
Definition b2exception.H:227
Definition b2exception.H:276
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32
GenericException< IOError_name > IOError
Definition b2exception.H:335
GenericException< ValueError_name > ValueError
Definition b2exception.H:330
GenericException< KeyError_name > KeyError
Definition b2exception.H:320
GenericException< SizeError_name > SizeError
Definition b2exception.H:346
GenericException< PythonError_name > PythonError
Definition b2exception.H:371
GenericException< TypeError_name > TypeError
Definition b2exception.H:325
GenericException< SystemError_name > LoggingError
Definition b2exception.H:363
GenericException< UnimplementedError_name > UnimplementedError
Definition b2exception.H:314
GenericException< DBError_name > DBError
Definition b2exception.H:340
Definition b2exception.H:174
Definition b2exception.H:183