00001 #pragma ident "$Id: Exception.hpp 2937 2011-10-23 19:23:33Z yanweignss $"
00002
00003
00004
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
00044
00045
00046 #ifndef GPSTK_EXCEPTION_HPP
00047 #define GPSTK_EXCEPTION_HPP
00048
00049 #include <cstdlib>
00050 #include <iostream>
00051 #include <vector>
00052 #include <string>
00053
00054 namespace gpstk
00055 {
00067
00068
00069 class ExceptionLocation
00070 {
00071 public:
00078 ExceptionLocation(const std::string& filename = std::string(),
00079 const std::string& funcName = std::string(),
00080 const unsigned long& lineNum = 0)
00081 throw()
00082 : fileName(filename), functionName(funcName),
00083 lineNumber(lineNum)
00084 { }
00085
00089 ~ExceptionLocation() throw() {}
00090
00092 std::string getFileName() const
00093 throw()
00094 { return fileName; }
00096 std::string getFunctionName() const
00097 throw()
00098 { return functionName; }
00100 unsigned long getLineNumber() const
00101 throw()
00102 { return lineNumber; }
00103
00108 void dump(std::ostream& s) const throw();
00109
00111 std::string what() const throw();
00112
00122 friend std::ostream& operator<<( std::ostream& s,
00123 const ExceptionLocation& e )
00124 throw();
00125
00126 private:
00128 std::string fileName;
00130 std::string functionName;
00132 unsigned long lineNumber;
00133 };
00134
00155 class Exception : public std::exception
00156 {
00157 public:
00159 enum Severity
00160 {
00161 unrecoverable,
00162 recoverable
00163 };
00164
00169 Exception() throw();
00170
00178 Exception(const std::string& errorText,
00179 const unsigned long& errorId = 0,
00180 const Severity& severity = unrecoverable)
00181 throw();
00183 Exception(const Exception& exception)
00184 throw();
00186 ~Exception()
00187 throw()
00188 {};
00189
00191 Exception& operator=(const Exception& e)
00192 throw();
00193
00201 void terminate()
00202 throw()
00203 { ::exit(1); };
00204
00206 unsigned long getErrorId() const
00207 throw()
00208 { return errorId; };
00209
00215 Exception& setErrorId(const unsigned long& errId)
00216 throw()
00217 { errorId = errId; return *this; };
00218
00231 Exception& addLocation(const ExceptionLocation& location)
00232 throw();
00233
00240 const ExceptionLocation getLocation(const size_t& index=0) const
00241 throw();
00242
00245 size_t getLocationCount() const
00246 throw();
00247
00253 bool isRecoverable() const
00254 throw()
00255 { return (severity == recoverable); }
00256
00262 Exception& setSeverity(const Severity& sever)
00263 throw()
00264 { severity = sever; return *this; };
00265
00271 Exception& addText(const std::string& errorText)
00272 throw();
00273
00282 std::string getText(const size_t& index=0) const
00283 throw();
00284
00286 size_t getTextCount() const
00287 throw();
00288
00290 std::string getName() const
00291 throw()
00292 { return "Exception"; };
00293
00298 void dump(std::ostream& s) const
00299 throw();
00300
00302 virtual const char* what() const throw();
00303
00312 friend std::ostream& operator<<( std::ostream& s,
00313 const Exception& e )
00314 throw();
00315
00316 protected:
00318 unsigned long errorId;
00320 std::vector<ExceptionLocation> locations;
00322 Severity severity;
00324 std::vector<std::string> text;
00325
00334 int overflow(int c);
00335
00336 private:
00338 std::string streamBuffer;
00340 mutable std::string whatBuffer;
00341 };
00342
00343
00344 }
00345
00346
00357 #if defined ( __FUNCTION__ )
00358 #define FILE_LOCATION gpstk::ExceptionLocation(__FILE__, __FUNCTION__, __LINE__)
00359 #else
00360 #define FILE_LOCATION gpstk::ExceptionLocation(__FILE__, "", __LINE__)
00361 #endif
00362
00363
00364 #if defined (NO_EXCEPTIONS_SUPPORT)
00367 #define GPSTK_THROW(exc) { exc.addLocation(FILE_LOCATION); exc.terminate(); }
00370 #define GPSTK_RETHROW(exc) { exc.addLocation(FILE_LOCATION); exc.terminate(); }
00371 #else
00374 #define GPSTK_THROW(exc) { exc.addLocation(FILE_LOCATION); throw exc; }
00377 #define GPSTK_RETHROW(exc) { exc.addLocation(FILE_LOCATION); throw; }
00378 #endif
00379
00380
00381
00391 #define NEW_EXCEPTION_CLASS(child, parent) \
00392 class child : public parent \
00393 { \
00394 public: \
00395 \
00396 child() throw() : parent() {} \
00397 \
00398 child(const child& a) throw() : parent(a) {} \
00399 \
00400 child(const gpstk::Exception& a) throw() : parent(a) {}; \
00401 \
00407 child(std::string a, unsigned long b = 0,\
00408 gpstk::Exception::Severity c = gpstk::Exception::unrecoverable) \
00409 throw() \
00410 : parent(a, b, c) \
00411 {};\
00412 \
00413 ~child() throw() {} \
00414 \
00415 std::string getName() const throw() {return ( # child);} \
00416 \
00417 child& operator=(const child& kid) \
00418 { parent::operator=(kid); return *this; } \
00419 \
00420 friend std::ostream& operator<<(std::ostream& s, const child& c) throw() \
00421 { c.dump(s); return s; } \
00422 }
00423
00424 namespace gpstk
00425 {
00428 NEW_EXCEPTION_CLASS(InvalidParameter, Exception);
00429
00432 NEW_EXCEPTION_CLASS(InvalidRequest, Exception);
00433
00436 NEW_EXCEPTION_CLASS(AssertionFailure, Exception);
00437
00440 NEW_EXCEPTION_CLASS(AccessError, Exception);
00441
00444 NEW_EXCEPTION_CLASS(IndexOutOfBoundsException, Exception);
00445
00448 NEW_EXCEPTION_CLASS(InvalidArgumentException, Exception);
00449
00452 NEW_EXCEPTION_CLASS(ConfigurationException, Exception);
00453
00456 NEW_EXCEPTION_CLASS(FileMissingException, Exception);
00457
00460 NEW_EXCEPTION_CLASS(SystemSemaphoreException, Exception);
00461
00464 NEW_EXCEPTION_CLASS(SystemPipeException, Exception);
00465
00468 NEW_EXCEPTION_CLASS(SystemQueueException, Exception);
00469
00472 NEW_EXCEPTION_CLASS(OutOfMemory, Exception);
00473
00476 NEW_EXCEPTION_CLASS(ObjectNotFound, AccessError);
00477
00480 NEW_EXCEPTION_CLASS(NullPointerException, Exception);
00481
00482
00483 }
00484 #endif
00485
00486
00487