ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/include/exception.h
Revision: 1.2
Committed: Tue Aug 28 17:12:24 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
State: FILE REMOVED
Log Message:
removed old files

File Contents

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