00001 #ifndef LOPER_EXCEPTIONS
00002 #define LOPER_EXCEPTIONS
00003
00014 #include <stdarg.h>
00015 #include <stdexcept>
00016 #include <exception>
00017 #include <stdio.h>
00018
00019
00020
00025 template< typename Super >
00026 class ExceptionTemplate : public Super
00027 {
00028 public:
00029 ExceptionTemplate (const char *format, ...);
00030 ExceptionTemplate(FILE *fp, const char *format, ...);
00031 virtual const char *what() const;
00032 private:
00033 std::string str;
00034 };
00035
00036
00037
00038
00045 typedef ExceptionTemplate< std::exception > Error;
00046 typedef ExceptionTemplate< std::logic_error > LogicError;
00047 typedef ExceptionTemplate< std::invalid_argument > ArgError;
00048 typedef ExceptionTemplate< std::runtime_error > RuntimeError;
00049
00050
00051
00052
00053
00054
00055
00056
00057 template< typename Super >
00058 ExceptionTemplate< Super >::ExceptionTemplate (const char *format, ...) : Super("")
00059 {
00060 va_list args;
00061 char buffer[512];
00062 va_start(args,format);
00063 _vsnprintf(buffer,sizeof(buffer)-1,format,args);
00064 va_end(args);
00065 str = buffer;
00066 }
00067
00068
00069
00070 template< typename Super >
00071 ExceptionTemplate< Super >::ExceptionTemplate< Super >(FILE *fp, const char *format, ...) : Super("")
00072 {
00073 va_list args;
00074 char buffer[512];
00075 va_start(args,format);
00076 _vsnprintf(buffer,sizeof(buffer)-1,format,args);
00077 va_end(args);
00078
00079 str = buffer;
00080 if (!fp) str += ": file i/o error (null file pointer)";
00081 else if (feof(fp)) str += ": reached end of file.";
00082 else if (ferror(fp)) str += ": error reading file.";
00083 else str += ": unknown error";
00084 }
00085
00086
00087
00088 template< typename Super >
00089 const char *ExceptionTemplate< Super >::what() const
00090 {
00091 return str.c_str();
00092 }
00093
00094
00095 #endif // LOPER_EXCEPTIONS