00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _EYEDB_EXCEPTION_H
00026 #define _EYEDB_EXCEPTION_H
00027
00028 #include <stdarg.h>
00029
00030 namespace eyedb {
00031
00037 enum SeverityStatus {
00038 _Warning=1,
00039 _Error,
00040 _FatalError
00041 };
00042
00043 class Exception;
00044
00045 typedef const Exception *Status;
00046
00050 class Exception {
00051
00052
00053
00054
00055 public:
00056
00062 Exception(int status = 0, SeverityStatus severity = _Error);
00063
00070 Exception(Status parent, int status = 0,
00071 SeverityStatus severity = _Error);
00072
00077 Exception(const Exception &e);
00078
00084 Exception& operator=(const Exception &e);
00085
00091 Status setSeverity(SeverityStatus severity);
00092
00097 SeverityStatus getSeverity() const;
00098
00103 Status getParent() const;
00104
00109 void setStatus( int status);
00110
00115 int getStatus() const;
00116
00122 Status setString(const char *fmt, ...);
00123
00130 Status setString(SeverityStatus severity, const char *fmt, ...);
00131
00139 Status setString(int status, SeverityStatus severity, const char *fmt, ...);
00140
00145 const char *getString() const;
00146
00151 const char *getDesc() const;
00152
00158 Status setUserData(Any ud);
00159
00164 Any getUserData() const;
00165
00171 static void setHandler(void (*handler)(Status, void *), void *hud = NULL);
00172
00177 static void (*getHandler())(Status, void *);
00178
00183 static void *getHandlerUserData();
00184
00185 void applyHandler() const;
00186
00187 enum Mode {
00188 ExceptionMode = 32,
00189 StatusMode = 64
00190 };
00191
00197 static Exception::Mode setMode(Exception::Mode mode);
00198
00203 static Exception::Mode getMode();
00204
00210 Status print(FILE *fd = stderr, bool newline = true) const;
00211
00218 static Status make(int err, const char *fmt, ...);
00219
00226 static Status make(int err, const std::string &s);
00227
00233 static Status make(const char *fmt, ...);
00234
00240 static Status make(const std::string &s);
00241
00247 static Status make(int err);
00248
00249
00255 static Status make(Status status);
00256
00257 ~Exception();
00258
00259
00260
00261
00262 private:
00263 Exception *parent;
00264 int status;
00265 SeverityStatus severity;
00266 char *string;
00267 Any user_data;
00268 static void* handler_user_data;
00269 static void (*handler)(Status, void *);
00270 static Exception::Mode mode;
00271
00272 static struct _desc {
00273 const char *status_string;
00274 const char *desc;
00275 } statusDesc[IDB_N_ERROR];
00276
00277 static void (*print_method)(Status, FILE*);
00278 void setPrintMethod(void (*)(Status, FILE*));
00279 void (*getPrintMethod(void))(Status, FILE*);
00280
00281 void statusMake(Status, int, SeverityStatus);
00282 void print_realize(FILE*, bool newline) const;
00283
00284
00285
00286
00287 public:
00288 static void init(void);
00289 static void _release(void);
00290 };
00291
00292 std::ostream& operator<<(std::ostream&, const Exception &);
00293 std::ostream& operator<<(std::ostream&, Status);
00294
00295
00296
00297
00298
00299 static const Status Success = (Status)0;
00300
00301 extern Status _status_;
00302
00303 #define eyedb_CHECK(e) \
00304 ((_status_ = (e)) ? \
00305 (void)fprintf(stderr, "file %s, line %d : %s\n<<\n %s\n>>\n", \
00306 __FILE__, __LINE__, _status_->getDesc(), #e), \
00307 _status_ : _status_)
00308
00309 #define eyedb_ASSERT(e) \
00310 if (eyedb_CHECK(e)) \
00311 abort()
00312
00313 #define eyedb_CHECK_RETURN(e) \
00314 if ((_status_ = (e))) return _status_
00315
00320 }
00321
00322 #endif