Debian Codegraph @

Oodle/src/exceptions.h

raw
1 #ifndef _EXCEPTIONS_
2 #define _EXCEPTIONS_
3
4 #include <stdexcept>
5 #include <ostream>
6 #include <string>
7 #include <queue>
8 #include "SDL.h"
9
10
11
12 using namespace std;
13
14 //! Class to hold stack tace info for each method that handles an exception
15 class CTraceInfo
16 {
17 public:
18 CTraceInfo(const string &c, const string &method, const string &params)
19 : c(c), method(method), params(params)
20 {}
21
22 string getClass() const { return c; }
23 string getMethod() const { return method; }
24 string getParams() const { return params; }
25 private:
26 string c, method, params;
27 };
28
29 //! Generic exception base class
30 class ex_Generic : public std::runtime_error {
31 public:
32 ex_Generic(const std::string& name, const CTraceInfo &t)
33 : std::runtime_error(name), desc("")
34 {
35 addTraceInfo(t);
36 }
37
38 void setDesc(const string& s)
39 {
40 desc = s;
41 }
42
43 void addTraceInfo(const CTraceInfo &t)
44 {
45 trace.push(t); // only problem here is if this throws an exception. if that happens, the CRT will terminate() the program. fix this eventually, maybe
46 }
47
48 void stackTrace(std::ostream &o)
49 {
50 if (trace.size() > 0)
51 {
52 const CTraceInfo t = trace.front(); trace.pop();
53 o << "Exception in " << t.getClass() << "::" << t.getMethod() << "(" << t.getParams() << "): " << what();
54 if (desc != "")
55 {
56 o << " (Detailed: " << desc << ")";
57 }
58 o << endl;
59 }
60 else
61 {
62 o << "Ugh! Something is really fubared. Cannot print stack trace." << endl; // haha this should never happen. if this happens, something is fubared
63 }
64
65 while (trace.size() > 1)
66 {
67 CTraceInfo t = trace.front(); trace.pop();
68 o << "\t" << "rethrown at " << t.getClass() << "::" << t.getMethod() << "(" << t.getParams() << ")" << endl;
69 }
70
71 if (trace.size() > 0)
72 {
73 CTraceInfo t = trace.front(); trace.pop();
74 o << "\t" << "**caught at " << t.getClass() << "::" << t.getMethod() << "(" << t.getParams() << ")" << endl;
75 }
76 }
77
78 # ifdef __GNUC__
79 ~ex_Generic() throw () {} // GCC has an error otherwise -- looser throw specifier http://www.agapow.net/index.php?/agapow/content/pdf/248
80 # endif
81
82 private:
83 #ifdef __GNUC__
84 queue< CTraceInfo > trace; // GCC's libstdc++ apparently won't deal with const objects
85 #else
86 queue< const CTraceInfo > trace;
87 #endif
88 string desc;
89 };
90
91 //! Illegal arguments exception
92 /*! Thrown when there is some error with arguments passed to method
93 */
94 class ex_illegalArguments : public ex_Generic {
95 public:
96 ex_illegalArguments(const std::string& name, const CTraceInfo &t)
97 : ex_Generic(name, t)
98 { }
99 };
100
101 //! Illegal state exception
102 /*! Thrown when there the object is not in a legal state for requested action
103 */
104 class ex_illegalState : public ex_Generic {
105 public:
106 ex_illegalState(const std::string& name, const CTraceInfo &t)
107 : ex_Generic(name, t)
108 { }
109 };
110
111 //! Bad Conversion Exception
112 /*! Thrown when there is some error converting some value
113 */
114 class ex_badConversion : public ex_Generic {
115 public:
116 ex_badConversion(const std::string& name, const CTraceInfo &t)
117 : ex_Generic(name, t)
118 { }
119 };
120
121 //! Generic SDL exception base class
122 /*! Thrown when there is some error with SDL
123 */
124 class ex_sdl : public ex_Generic {
125 public:
126 ex_sdl(const std::string& name, const CTraceInfo &t)
127 : ex_Generic(name, t)
128 {
129 const char* sdlerr = SDL_GetError();
130 if (sdlerr)
131 {
132 setDesc(sdlerr);
133 }
134 }
135 };
136
137 //! Generic SDL_Image exception base class
138 /*! Thrown when there is some error with SDL_Image
139 */
140 class ex_sdlImage : public ex_sdl {
141 public:
142 ex_sdlImage(const std::string& name, const CTraceInfo &t)
143 : ex_sdl(name, t)
144 { }
145 };
146
147 //! SDL Init Failed exception
148 /*! Thrown when there is some error initializing SDL
149 */
150 class ex_sdlInitFailed : public ex_sdl {
151 public:
152 ex_sdlInitFailed(const std::string& name, const CTraceInfo &t)
153 : ex_sdl(name, t)
154 { }
155 };
156
157 //! SDL Surface creation failure
158 /*! Thrown when there is some error creating SDL surface
159 */
160 class ex_sdlSurfaceCreationFailed : public ex_sdl {
161 public:
162 ex_sdlSurfaceCreationFailed(const std::string& name, const CTraceInfo &t)
163 : ex_sdl(name, t)
164 { }
165 };
166
167 //! SDL Surface conversion failure
168 /*! Thrown when there is some error converting SDL surface
169 */
170 class ex_sdlSurfaceConversionFailed : public ex_sdl {
171 public:
172 ex_sdlSurfaceConversionFailed(const std::string& name, const CTraceInfo &t)
173 : ex_sdl(name, t)
174 { }
175 };
176
177 //! SDL Image Load Failed exception
178 /*! Thrown when there is some error loading an image
179 */
180 class ex_sdlImageLoadFailed : public ex_sdlImage {
181 public:
182 ex_sdlImageLoadFailed(const std::string& name, const CTraceInfo &t)
183 : ex_sdlImage(name, t)
184 { }
185 };
186
187
188 //! Macro used when constructing a stack trace object
189 #define _THROWEX(ex, name, c, method, params) { \
190 ostringstream ss[4]; \
191 ss[0] << c;\
192 ss[1] << method;\
193 ss[2] << params;\
194 ss[3] << name << " (in " << __FILE__ << " @ line " << __LINE__ << ")";\
195 throw ex(ss[3].str(), CTraceInfo(ss[0].str(), ss[1].str(), ss[2].str()));\
196 }
197
198 //! Macros used to add trace info to an exception object
199 #define _ADDTRACEINFO(obj, c, method, params) { \
200 ostringstream ss[3]; \
201 ss[0] << c;\
202 ss[1] << method;\
203 ss[2] << params;\
204 obj.addTraceInfo(CTraceInfo(ss[0].str(), ss[1].str(), ss[2].str()));\
205 }
206
207 #endif