Zero  0.1.0
kits_exception.h
Go to the documentation of this file.
1 // -*- mode:C++; c-basic-offset:4 -*-
2 #ifndef __KITS_EXCEPTION_H
3 #define __KITS_EXCEPTION_H
4 
5 #include <exception>
6 #include <cstring>
7 #include <cerrno>
8 #include <cassert>
9 #include <cstdlib>
10 #include <cstdio>
11 #include <sstream>
12 
13 
17 #define EXCEPTION(type) \
18  type(__FILE__, __LINE__, __PRETTY_FUNCTION__)
19 #define EXCEPTION1(type, arg) \
20  type(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(arg))
21 #define EXCEPTION2(type, arg1, arg2) \
22  type(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(arg1, arg2))
23 
24 #define THROW(type) \
25  do { \
26  assert(false); \
27  throw type(__FILE__, __LINE__, __PRETTY_FUNCTION__, ""); \
28  } while (false)
29 #define THROW1(type, arg) \
30  do { \
31  fprintf(stderr, arg.c_str()); \
32  fflush(stderr); \
33  assert(false); \
34  throw type(__FILE__, __LINE__, __PRETTY_FUNCTION__, arg.c_str()); \
35  } while (false)
36 
37 #define THROW2(type, arg1, arg2) \
38  do { \
39  fprintf(stderr, arg1, arg2); \
40  fflush(stderr); \
41  assert(false); \
42  throw type(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(arg1, arg2)); \
43  } while (false)
44 
45 // these two only used by cpu_set.cpp
46 #define THROW3(type, arg1, arg2, arg3) \
47  do { \
48  fprintf(stderr, arg1, arg2, arg3); \
49  fflush(stderr); \
50  assert(false); \
51  throw type(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(arg1, arg2, arg3)); \
52  } while (false)
53 
54 #define THROW4(type, arg1, arg2, arg3, arg4) \
55  do { \
56  fprintf(stderr, arg1, arg2, arg3, arg4); \
57  fflush(stderr); \
58  assert(false); \
59  throw type(__FILE__, __LINE__, __PRETTY_FUNCTION__, std::string(arg1, arg2, arg3, arg4)); \
60  } while (false)
61 
62 // tests an error code and throws the specified exception if nonzero
63 #define THROW_IF(Exception, err) \
64  do { \
65  assert(!err); \
66  if(err) { \
67  THROW1(Exception, errno_to_str(err)); \
68  } \
69  } while (false)
70 
71 class ZappsException : public std::exception {
72 
73 private:
74 
75  std::string _message;
76 
77 public:
78  ZappsException(const char* filename, int line_num, const char* function_name,
79  std::string const& m) : exception() {
80  std::stringstream ss;
81  ss << filename << ":" << line_num
82  << "(" << function_name << "): "
83  << m;
84  _message = ss.str();
85  }
86 
87  virtual const char* what() const throw() {
88  return _message.data();
89  }
90 };
91 
93 public:
94  QPipeException(const char* filename, int line_num, const char* function_name,
95  std::string const& m) : ZappsException(filename, line_num, function_name, m) {}
96 };
97 
99 public:
100  ThreadException(const char* filename, int line_num, const char* function_name,
101  std::string const& m) : ZappsException(filename, line_num, function_name, m) {}
102 };
103 
104 #define DEFINE_EXCEPTION(Name) \
105  class Name : public ZappsException { \
106  public: \
107  Name(const char* filename, int line_num, const char* function_name, \
108  const char* m) \
109  : ZappsException(filename, line_num, function_name, m) \
110  { \
111  } \
112  }
113 
114 inline std::string errno_to_str(int err = errno) {
115  return strerror(err);
116 }
117 
118 DEFINE_EXCEPTION(Unreachable);
119 
120 DEFINE_EXCEPTION(BadAlloc);
121 
122 DEFINE_EXCEPTION(OutOfRange);
123 
124 DEFINE_EXCEPTION(FileException);
125 
126 DEFINE_EXCEPTION(BdbException);
127 
128 #ifdef __GCC
129 inline void unreachable() ATTRIBUTE(noreturn);
130 inline void unreachable() {
131  assert(false);
132  exit(-1);
133 }
134 #else
135 #define unreachable() THROW(Unreachable)
136 #endif
137 
138 #endif // __KITS_EXCEPTION_H
#define unreachable()
Definition: kits_exception.h:135
std::string _message
Definition: kits_exception.h:75
std::string errno_to_str(int err=errno)
Definition: kits_exception.h:114
Definition: kits_exception.h:71
Definition: kits_exception.h:92
#define ATTRIBUTE(x)
Definition: compat.h:33
ZappsException(const char *filename, int line_num, const char *function_name, std::string const &m)
Definition: kits_exception.h:78
QPipeException(const char *filename, int line_num, const char *function_name, std::string const &m)
Definition: kits_exception.h:94
virtual const char * what() const
Definition: kits_exception.h:87
ThreadException(const char *filename, int line_num, const char *function_name, std::string const &m)
Definition: kits_exception.h:100
Definition: kits_exception.h:98
#define DEFINE_EXCEPTION(Name)
Definition: kits_exception.h:104