ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/include/ermyth/exception.h
Revision: 1.7
Committed: Sat Sep 22 14:27:26 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +1 -1 lines
State: FILE REMOVED
Log Message:
split up ermyth into ermyth-modules, libermyth (currently just ermyth-util) and ermyth-core

File Contents

# Content
1 /**
2 * exception.h: Ermyth exceptions.
3 *
4 * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
5 * Rights to this code are as documented in doc/pod/gplicense.pod.
6 *
7 * $Id: exception.h,v 1.6 2007-09-16 18:54:42 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 #include <svsconfig.h>
19
20 #define SRCINF ::impl::srcinf (__FILE__, __LINE__, CURFUNC)
21
22 #define EBUFSIZE 1024
23
24 namespace impl
25 {
26 struct srcinf
27 {
28 srcinf (char const *filename, unsigned int linenumber, char const *function)
29 : m_info (info (filename, linenumber, function))
30 {
31 }
32
33 unsigned int line() const throw() { return m_info.line; }
34 char const *file() const throw() { return m_info.file; }
35 char const *func() const throw() { return m_info.func; }
36
37 srcinf &operator = (srcinf const &rhs)
38 {
39 if (this != &rhs)
40 this->m_info = rhs.m_info;
41 return *this;
42 }
43
44 srcinf (srcinf const &rhs)
45 {
46 this->m_info = rhs.m_info;
47 }
48
49 private:
50 struct info
51 {
52 char const *file;
53 unsigned line;
54 char const *func;
55
56 info ()
57 : file (0), line (0), func (0)
58 {
59 }
60
61 info (char const *filename, unsigned linenumber, char const *function)
62 : file (filename ? filename : "<anonymous file>"), line (linenumber), func (function ? function : "<anonymous function> ()")
63 {
64 }
65 };
66
67 info m_info;
68 };
69
70 struct exception : std::exception
71 {
72 virtual const char *what() const throw ()
73 {
74 return m_what ? m_what : "";
75 }
76
77 protected:
78 void what (srcinf const &si, char const *fmt, va_list ap) throw ()
79 {
80 char buf[EBUFSIZE];
81 char *bufptr = buf;
82 int l = snprintf (bufptr, EBUFSIZE - 1, "%s(#%d): %s: ", si.file (), si.line (), si.func ());
83 vsnprintf (bufptr + l, EBUFSIZE - 1, fmt, ap);
84 what (buf);
85 }
86
87 void what (char const *fmt, va_list ap) throw ()
88 {
89 char buf[EBUFSIZE];
90 vsnprintf (buf, EBUFSIZE - 1, fmt, ap);
91 what (buf);
92 }
93
94 void what (char const *w) throw ()
95 {
96 std::copy (w, w + EBUFSIZE, m_what);
97 }
98
99 exception ()
100 {
101 }
102
103 private:
104 char m_what[EBUFSIZE];
105 };
106 } // namespace impl
107
108 #define ADD_EXCEPTION(name, base) \
109 struct name ## _exception : base \
110 { \
111 explicit name ## _exception () \
112 { \
113 what (#name " exception thrown"); \
114 } \
115 \
116 explicit name ## _exception (char const *fmt, ...) \
117 { \
118 va_list ap; \
119 va_start (ap, fmt); \
120 what (fmt, ap); \
121 va_end (ap); \
122 } \
123 \
124 explicit name ## _exception (impl::srcinf const &si, char const *fmt, ...) \
125 { \
126 va_list ap; \
127 va_start (ap, fmt); \
128 what (si, fmt, ap); \
129 va_end (ap); \
130 } \
131 }
132
133 ADD_EXCEPTION (nullpointer, impl::exception);
134 ADD_EXCEPTION (balloc, impl::exception);
135 ADD_EXCEPTION (module, impl::exception);
136 ADD_EXCEPTION (factory, impl::exception);
137
138 #endif // ERMYTH_EXCEPTION_H