1 |
/* |
2 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
3 |
* Rights to this code are as documented in doc/pod/gplicense.pod. |
4 |
* |
5 |
* Ermyth exceptions. |
6 |
* |
7 |
* $Id: exception.h,v 1.2 2007-07-25 01:05:17 pippijn Exp $ |
8 |
*/ |
9 |
|
10 |
#ifndef ERMYTH_EXCEPTION_H |
11 |
#define ERMYTH_EXCEPTION_H |
12 |
|
13 |
#include <cstdarg> |
14 |
#include <cstdio> |
15 |
#include <algorithm> |
16 |
#include <exception> |
17 |
|
18 |
#ifdef __GNUC__ |
19 |
# define CURFUNC __PRETTY_FUNCTION__ |
20 |
#elif defined(__BORLANDC__) |
21 |
# define CURFUNC __FUNC__ |
22 |
#else |
23 |
# define CURFUNC __FUNCTION__ |
24 |
#endif |
25 |
|
26 |
#define SRCINF srcinf (__FILE__, __LINE__, CURFUNC) |
27 |
|
28 |
#define EBUFSIZE 1024 |
29 |
|
30 |
struct srcinf |
31 |
{ |
32 |
srcinf (char const *file, unsigned int line, char const *func) |
33 |
: m_info (info (file, line, func)) |
34 |
{ |
35 |
} |
36 |
|
37 |
unsigned int line() const throw() { return m_info.line; } |
38 |
char const *file() const throw() { return m_info.file; } |
39 |
char const *func() const throw() { return m_info.func; } |
40 |
|
41 |
srcinf &operator = (srcinf const &rhs) |
42 |
{ |
43 |
if (this != &rhs) |
44 |
this->m_info = rhs.m_info; |
45 |
return *this; |
46 |
} |
47 |
|
48 |
srcinf (srcinf const &rhs) |
49 |
{ |
50 |
this->m_info = rhs.m_info; |
51 |
} |
52 |
|
53 |
private: |
54 |
struct info |
55 |
{ |
56 |
char const *file; |
57 |
unsigned line; |
58 |
char const *func; |
59 |
|
60 |
info () |
61 |
: file (0), line (0), func (0) |
62 |
{ |
63 |
} |
64 |
|
65 |
info (char const *file, unsigned line, char const *func) |
66 |
: file (file ? file : "<anonymous file>"), line (line), func (func ? func : "<anonymous function> ()") |
67 |
{ |
68 |
} |
69 |
}; |
70 |
|
71 |
info m_info; |
72 |
}; |
73 |
|
74 |
struct exception : std::exception |
75 |
{ |
76 |
virtual const char *what() const throw () |
77 |
{ |
78 |
return m_what ? m_what : ""; |
79 |
} |
80 |
|
81 |
protected: |
82 |
explicit exception (const char *fmt, ...) |
83 |
{ |
84 |
va_list ap; |
85 |
va_start (ap, fmt); |
86 |
vsnprintf (m_what, EBUFSIZE - 1, fmt, ap); |
87 |
va_end (ap); |
88 |
} |
89 |
|
90 |
explicit exception (srcinf const &si, char const *fmt, ...) |
91 |
{ |
92 |
va_list ap; |
93 |
va_start (ap, fmt); |
94 |
what (si, fmt, ap); |
95 |
va_end (ap); |
96 |
} |
97 |
|
98 |
void what (srcinf const &si, char const *fmt, va_list ap) throw () |
99 |
{ |
100 |
char buf[EBUFSIZE]; |
101 |
char *bufptr = buf; |
102 |
int l = snprintf (bufptr, EBUFSIZE - 1, "%s(#%d): %s: ", si.file (), si.line (), si.func ()); |
103 |
vsnprintf (bufptr + l, EBUFSIZE - 1, fmt, ap); |
104 |
what (buf); |
105 |
} |
106 |
|
107 |
void what (char const *fmt, va_list ap) throw () |
108 |
{ |
109 |
char buf[EBUFSIZE]; |
110 |
vsnprintf (buf, EBUFSIZE - 1, fmt, ap); |
111 |
what (buf); |
112 |
} |
113 |
|
114 |
void what (char const *w) throw () |
115 |
{ |
116 |
std::copy (w, w + EBUFSIZE, m_what); |
117 |
} |
118 |
|
119 |
exception () |
120 |
{ |
121 |
} |
122 |
|
123 |
private: |
124 |
char m_what[EBUFSIZE]; |
125 |
}; |
126 |
|
127 |
struct nullpointer_exception : exception |
128 |
{ |
129 |
explicit nullpointer_exception () |
130 |
{ |
131 |
} |
132 |
|
133 |
explicit nullpointer_exception (char const *fmt, ...) |
134 |
{ |
135 |
va_list ap; |
136 |
va_start (ap, fmt); |
137 |
what (fmt, ap); |
138 |
va_end (ap); |
139 |
} |
140 |
|
141 |
explicit nullpointer_exception (srcinf const &si, char const *fmt, ...) |
142 |
{ |
143 |
va_list ap; |
144 |
va_start (ap, fmt); |
145 |
what (si, fmt, ap); |
146 |
va_end (ap); |
147 |
} |
148 |
}; |
149 |
|
150 |
#endif // ERMYTH_EXCEPTION_H |