00001 #pragma ident "$Id: Exception.hpp 1461 2008-11-18 19:46:55Z ocibu $"
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
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 std::string 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;
00339 };
00340
00341
00342 }
00343
00344
00355 #if defined ( __FUNCTION__ )
00356 #define FILE_LOCATION gpstk::ExceptionLocation(__FILE__, __FUNCTION__, __LINE__)
00357 #else
00358 #define FILE_LOCATION gpstk::ExceptionLocation(__FILE__, "", __LINE__)
00359 #endif
00360
00361
00362 #if defined (NO_EXCEPTIONS_SUPPORT)
00365 #define GPSTK_THROW(exc) { exc.addLocation(FILE_LOCATION); exc.terminate(); }
00368 #define GPSTK_RETHROW(exc) { exc.addLocation(FILE_LOCATION); exc.terminate(); }
00369 #else
00372 #define GPSTK_THROW(exc) { exc.addLocation(FILE_LOCATION); throw exc; }
00375 #define GPSTK_RETHROW(exc) { exc.addLocation(FILE_LOCATION); throw; }
00376 #endif
00377
00378
00379
00389 #define NEW_EXCEPTION_CLASS(child, parent) \
00390 class child : public parent \
00391 { \
00392 public: \
00393 \
00394 child() throw() : parent() {} \
00395 \
00396 child(const child& a) throw() : parent(a) {} \
00397 \
00398 child(const gpstk::Exception& a) throw() : parent(a) {}; \
00399 \
00405 child(std::string a, unsigned long b = 0,\
00406 gpstk::Exception::Severity c = gpstk::Exception::unrecoverable) \
00407 throw() \
00408 : parent(a, b, c) \
00409 {};\
00410 \
00411 ~child() throw() {} \
00412 \
00413 std::string getName() const throw() {return ( # child);} \
00414 \
00415 child& operator=(const child& kid) \
00416 { parent::operator=(kid); return *this; } \
00417 \
00418 friend std::ostream& operator<<(std::ostream& s, const child& c) throw() \
00419 { c.dump(s); return s; } \
00420 }
00421
00422 namespace gpstk
00423 {
00426 NEW_EXCEPTION_CLASS(InvalidParameter, Exception);
00427
00430 NEW_EXCEPTION_CLASS(InvalidRequest, Exception);
00431
00434 NEW_EXCEPTION_CLASS(AssertionFailure, Exception);
00435
00438 NEW_EXCEPTION_CLASS(AccessError, Exception);
00439
00442 NEW_EXCEPTION_CLASS(IndexOutOfBoundsException, Exception);
00443
00446 NEW_EXCEPTION_CLASS(InvalidArgumentException, Exception);
00447
00450 NEW_EXCEPTION_CLASS(ConfigurationException, Exception);
00451
00454 NEW_EXCEPTION_CLASS(FileMissingException, Exception);
00455
00458 NEW_EXCEPTION_CLASS(SystemSemaphoreException, Exception);
00459
00462 NEW_EXCEPTION_CLASS(SystemPipeException, Exception);
00463
00466 NEW_EXCEPTION_CLASS(SystemQueueException, Exception);
00467
00470 NEW_EXCEPTION_CLASS(OutOfMemory, Exception);
00471
00474 NEW_EXCEPTION_CLASS(ObjectNotFound, AccessError);
00475
00476 }
00477 #endif
00478
00479
00480