00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #ifndef ERROR_H
00028 #define ERROR_H
00029
00065 #include <cstdio>
00066 #include <cstdarg>
00067 #include <cstring>
00068
00069 #if _WIN32
00070 # pragma warning(disable:4996)
00071 # ifndef snprintf
00072 # define snprintf _snprintf
00073 # endif
00074 #endif
00075
00076
00079 struct Error {
00083 Error(const char* msg, ...)
00084 {
00085 va_list arg;
00086 va_start(arg, msg);
00087 vsnprintf(this->msg, 1024, msg, arg);
00088 va_end(arg);
00089 }
00090
00094 void tag(const char* msg, ...)
00095 {
00096 char newline[1024];
00097 va_list arg;
00098 va_start(arg, msg);
00099 vsnprintf(newline, 1024, msg, arg);
00100 va_end(arg);
00101 char newmsg[1024];
00102 snprintf(newmsg, 1024, "%s\n%s", this->msg, newline);
00103 strncpy(this->msg, newmsg, 1024);
00104 }
00105
00107 void print() const
00108 {
00109 printf("[error message]\n%s\n", msg);
00110 }
00111
00112 char msg[1024];
00113 };
00114
00115
00116 #if _WIN32
00117 # pragma warning(default:4996)
00118 #endif
00119
00120
00122 struct RangeError : public Error {
00123 RangeError(const char* msg, float val, float lowerbound, float upperbound)
00124 : Error("RangeError: %s, got: %f (bound: %f:%f)", msg, val, lowerbound, upperbound) {}
00125 };
00126
00128 #define CHECK(msg,testvalid) { if(testvalid==0) throw LOCATE(Error(msg)); }
00129
00131 #define CHECKRANGE(msg,val,low,up) { if(val<low || val>=up) throw LOCATE(RangeError(msg,(float)val,(float)low,(float)up)); }
00132
00134 #define CHECKMIN(msg,val,min) { if(val<min) throw LOCATE(RangeError(msg,(float)val,(float)min,0)); }
00135
00137 #define CHECKMAX(msg,val,max) { if(val>=max) throw LOCATE(RangeError(msg,(float)val,0,(float)max)); }
00138
00140 #define LOCATE(Error) LocateError(Error, __FILE__, __LINE__)
00141
00143 struct LocateError {
00144 template <typename Error>
00145 LocateError(Error e, const char* file, int line) { e.tag("\t%s:%d", file, line); throw e; }
00146 };
00147
00148
00149 #ifdef GL_VERSION
00150
00151 # ifndef GLU_VERSION
00152 # ifdef __APPLE__
00153 # include <OpenGL/glu.h>
00154 # else
00155 # include <GL/glu.h>
00156 # endif
00157 # endif
00158
00160 struct OpenGLError : public Error {
00161 OpenGLError(GLenum err)
00162 : Error("OpenGL error 0x%x: %s\n", err, gluErrorString(err)) {}
00163 };
00164
00166 #define glCheckError() \
00167 {\
00168 GLenum err = glGetError();\
00169 if(err != GL_NO_ERROR)\
00170 throw LOCATE(OpenGLError(err));\
00171 }
00172
00173 #else
00174
00175
00176 # pragma message(__FILE__ " : warning: OpenGL errors not handled, <GL/gl.h> is not included before <error.h>")
00177
00178 #endif
00179
00180
00181 #endif