libutil++  1.9.3
 All Classes Functions Variables
Exception.h
1 /*
2 ** libutil++
3 ** $Id: Exception.h 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__Exception_H__
10 #define __libutilxx__sella__Exception_H__
11 
12 #include "../common.h"
13 
14 #include <stdarg.h>
15 #include <typeinfo>
16 
17 #include <string>
18 #include <exception>
19 
26 #define NO_EXCEPTIONS throw ()
27 
34 #define THROWS(e, ...) throw (e, ##__VA_ARGS__)
35 
36 #define THROW(exception, format, ...) throw exception(false, __func__, __FILE__, __LINE__, 0, format, ##__VA_ARGS__)
37 #define THROW_CODE(exception, code, format, ...) throw exception(false, __func__, __FILE__, __LINE__, code, format, ##__VA_ARGS__)
38 #define THROW2(exception, format, ...) throw exception(true, __func__, __FILE__, __LINE__, 0, format, ##__VA_ARGS__)
39 #define THROW2_CODE(exception, code, format, ...) throw exception(true, __func__, __FILE__, __LINE__, code, format, ##__VA_ARGS__)
40 #define RETHROW(exception, e) throw exception(false, __func__, __FILE__, __LINE__, e.getCode(), e.what())
41 #define RETHROW_CODE(exception, code, e) throw exception(false, __func__, __FILE__, __LINE__, code, e.what())
42 #define RETHROW2(exception, e, format, ...) throw exception(false, __func__, __FILE__, __LINE__, 0, e, format, ##__VA_ARGS__)
43 #define RETHROW2_CODE(exception, code, e, format, ...) throw exception(false, __func__, __FILE__, __LINE__, code, e, format, ##__VA_ARGS__)
44 
45 #define EXCEPTION_BUILDMESSAGE_CODE \
46  do { \
47  va_list list; \
48  va_start(list, format); \
49  buildMessage(sysmsg, format, list); \
50  va_end(list); \
51  } while (false);
52 
53 /* NOTE: Inclusion of what() and getCode() allows SWIGPERL exceptions to work. */
54 #define BUILD_EXCEPTION(T) \
55  class T : public sella::Exception { \
56  public: \
57  T(bool sysmsg, const char *function, const char *file, int line, int code, const char *format, ...) throw () : \
58  Exception(function, file, line, code) { EXCEPTION_BUILDMESSAGE_CODE }; \
59  T(bool sysmsg, const char *function, const char *file, int line, int code, sella::Exception &e, const char *format, ...) throw () : \
60  Exception(function, file, line, code) { EXCEPTION_BUILDMESSAGE_CODE; message.append(" Error: ").append(e.getMessage()); }; \
61  virtual const char* what() const throw () { return Exception::what(); } \
62  virtual int getCode() const throw () { return Exception::getCode(); } \
63  virtual const std::string getName() const throw () { return demangle(typeid(T).name()); } \
64  };
65 
66 namespace sella {
67  class Exception;
68 }
69 
70 class sella::Exception : public std::exception {
71  public:
72  Exception(const char *function, const char *file, int line, int code) throw ();
73  Exception(bool sysmsg, const char *function, const char *file, int line, int code, const char *format, ...) throw ();
74 
75  Exception(const Exception &other) throw ();
76 #ifndef SWIG
77  Exception(const Exception &&other) throw ();
78 #endif
79 
80  virtual ~Exception() throw ();
81 
82  public:
83  Exception& operator=(const Exception &other) throw ();
84 #ifndef SWIG
85  Exception& operator=(const Exception &&other) throw ();
86 #endif
87 
88  public:
89  virtual const std::string& getFunction() const throw ();
90  virtual const std::string& getFile() const throw ();
91  virtual int getLine() const throw ();
92  virtual int getCode() const throw ();
93  virtual const std::string& getMessage() const throw ();
94  virtual const char* what() const throw ();
95  virtual const std::string& getStackTrace() const throw ();
96  virtual const std::string getName() const throw ();
97 
98  virtual const std::string& str() const throw ();
99  virtual const char* c_str() const throw ();
100 
101  static void enableStackTrace(bool on = true) throw (); /* Will slow down exceptions. */
102 
103  protected:
104  std::string function;
105  std::string file;
106  int line;
107  int code;
108  std::string message;
109  std::string stack;
110  mutable std::string cache;
111 
112  void buildMessage(bool sysmsg, const char *format, va_list list) throw ();
113  const std::string demangle(const char *name) const throw ();
114 };
115 
116 #endif
117 
118 /*
119 ** vim: noet ts=3 sw=3
120 */