00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef ERROR_HPP
00044 #define ERROR_HPP 1
00045
00046
00047 #include <string>
00048 #include <cstring>
00049 #include <stdint.h>
00050 #include <sstream>
00051 #include <errno.h>
00052
00053
00054 template <class T>
00055 inline std::string to_string( const T& t )
00056 {
00057 std::stringstream ss;
00058 ss << t;
00059 return( ss.str() );
00060 }
00061
00062
00065 #define ERROR_LOCATION ErrorLocation( __FILE__, __LINE__, __func__ )
00066
00067
00074 struct ErrorLocation {
00075 ErrorLocation() {}
00076 ErrorLocation( const char *file, int line, const char *func ) :
00077 _file(file), _line(line), _func(func) {}
00078 const char *_file;
00079 int _line;
00080 const char *_func;
00081 };
00082
00083
00086 struct Error {
00087 ErrorLocation _loc;
00088 std::string _error_str;
00089 Error() {}
00090 Error( ErrorLocation loc, const std::string &str )
00091 : _loc(loc), _error_str(str) {}
00092 };
00093
00094
00097 struct ErrorNoMem : public Error {
00098 ErrorNoMem( ErrorLocation loc )
00099 : Error( loc, "memory allocation error" ) {}
00100 };
00101
00102
00105 struct ErrorErrno : public Error {
00106 int _ierrno;
00107 ErrorErrno( ErrorLocation loc ) : _ierrno(errno) {
00108 _loc = loc;
00109 #if defined(WIN32) || defined(__MINGW32__)
00110 _error_str = "errno " + to_string(_ierrno);
00111 #else
00112 char buf[1024];
00113 strerror_r( _ierrno, buf, 1024 );
00114 _error_str = buf;
00115 #endif
00116 }
00117 };
00118
00119
00122 struct ErrorUnimplemented : public Error {
00123 ErrorUnimplemented( ErrorLocation loc )
00124 : Error( loc, "feature unimplemented" ) {}
00125 ErrorUnimplemented( ErrorLocation loc, const std::string &str )
00126 : Error( loc, str ) {}
00127 };
00128
00129
00132 struct ErrorDim : public Error {
00133 ErrorDim( ErrorLocation loc )
00134 : Error( loc, "dimension mismatch" ) {}
00135 ErrorDim( ErrorLocation loc, const std::string &str )
00136 : Error( loc, str ) {}
00137 };
00138
00139
00142 struct ErrorRange : public Error {
00143 ErrorRange( ErrorLocation loc, uint32_t i, uint32_t n, uint32_t j, uint32_t m ) {
00144 std::ostringstream ss;
00145 ss << "index out of range ( " << i << " >= " << n << " || ";
00146 ss << j << " >= " << m << " )";
00147 _error_str = ss.str();
00148 _loc = loc;
00149 }
00150 ErrorRange( ErrorLocation loc, uint32_t i, uint32_t n ) {
00151 std::ostringstream ss;
00152 ss << "index out of range ( " << i << " >= " << n << " )";
00153 _error_str = ss.str();
00154 _loc = loc;
00155 }
00156 };
00157
00158
00159 #endif
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178