Exception.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: Exception.hpp 3234 2012-07-31 12:15:03Z yanweignss $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00020 //  
00021 //  Copyright 2004, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 //============================================================================
00026 //
00027 //This software developed by Applied Research Laboratories at the University of
00028 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00029 //Department of Defense. The U.S. Government retains all rights to use,
00030 //duplicate, distribute, disclose, or release this software. 
00031 //
00032 //Pursuant to DoD Directive 523024 
00033 //
00034 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00035 //                           release, distribution is unlimited.
00036 //
00037 //=============================================================================
00038 
00044 // The unusual include macro below is done this way because xerces
00045 // #defines EXCEPTION_HPP in their own exception class header file.
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    }; // class ExceptionLocation
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();
00182 
00183 
00191       Exception(const char* errorText, 
00192                 const unsigned long& errorId = 0,
00193                 const Severity& severity = unrecoverable)
00194          throw();
00195 
00196 
00198       Exception(const Exception& exception) 
00199          throw();
00200 
00202       ~Exception() 
00203          throw() 
00204       {};
00205 
00207       Exception& operator=(const Exception& e)
00208          throw();
00209 
00217       void terminate()
00218          throw()
00219       { exit(1); };
00220 
00222       unsigned long getErrorId() const 
00223          throw()
00224       { return errorId; };
00225 
00231       Exception& setErrorId(const unsigned long& errId)
00232          throw()
00233       { errorId = errId; return *this; };
00234 
00247       Exception& addLocation(const ExceptionLocation& location) 
00248          throw();
00249 
00256       const ExceptionLocation getLocation(const size_t& index=0) const
00257          throw();
00258 
00261       size_t getLocationCount() const
00262          throw();
00263 
00269       bool isRecoverable() const
00270          throw()
00271       { return (severity == recoverable); }
00272 
00278       Exception& setSeverity(const Severity& sever) 
00279          throw()
00280       { severity = sever; return *this; };
00281 
00287       Exception& addText(const std::string& errorText) 
00288          throw();
00289 
00298       std::string getText(const size_t& index=0) const 
00299          throw();
00300 
00302       size_t getTextCount() const
00303          throw();
00304 
00306       std::string getName() const
00307          throw()
00308       { return "Exception"; };
00309 
00314       void dump(std::ostream& s) const 
00315          throw();
00316 
00318       std::string what() const throw();
00319 
00328       friend std::ostream& operator<<( std::ostream& s,
00329                                        const Exception& e )
00330          throw();
00331 
00332    protected:
00334       unsigned long errorId;
00336       std::vector<ExceptionLocation> locations;
00338       Severity severity;
00340       std::vector<std::string> text;
00341 
00350       int overflow(int c);
00351 
00352    private:
00354       std::string streamBuffer;
00355    }; // class Exception
00356 
00357 
00358 }  // namespace gpstk
00359 
00360 
00371 #if defined ( __FUNCTION__ )
00372 #define FILE_LOCATION gpstk::ExceptionLocation(__FILE__, __FUNCTION__, __LINE__)
00373 #else
00374 #define FILE_LOCATION gpstk::ExceptionLocation(__FILE__, "", __LINE__)
00375 #endif
00376 
00377 // For compilers without exceptions, die if you get an exception.
00378 #if defined (NO_EXCEPTIONS_SUPPORT)
00381 #define GPSTK_THROW(exc) { exc.addLocation(FILE_LOCATION); exc.terminate(); }
00384 #define GPSTK_RETHROW(exc) { exc.addLocation(FILE_LOCATION); exc.terminate(); }
00385 #else
00388 #define GPSTK_THROW(exc)   { exc.addLocation(FILE_LOCATION); throw exc; }
00391 #define GPSTK_RETHROW(exc) { exc.addLocation(FILE_LOCATION); throw; }
00392 #endif
00393 
00394 
00395 
00405 #define NEW_EXCEPTION_CLASS(child, parent) \
00406 class child : public parent  \
00407 { \
00408 public: \
00409  \
00410    child() throw()                  : parent() {} \
00411  \
00412    child(const child& a) throw()   : parent(a) {} \
00413  \
00414    child(const gpstk::Exception& a) throw() : parent(a) {}; \
00415  \
00421    child(const std::string& a, unsigned long b = 0,\
00422          gpstk::Exception::Severity c = gpstk::Exception::unrecoverable) \
00423          throw() \
00424          : parent(a, b, c) \
00425    {};\
00426  \
00432    child(const char* a, unsigned long b = 0,\
00433    gpstk::Exception::Severity c = gpstk::Exception::unrecoverable) \
00434    throw() \
00435    : parent(a, b, c) \
00436    {};\
00437  \
00438    ~child() throw() {} \
00439  \
00440    std::string getName() const throw() {return ( # child);} \
00441  \
00442    child& operator=(const child& kid) \
00443       { parent::operator=(kid); return *this; } \
00444  \
00445    friend std::ostream& operator<<(std::ostream& s, const child& c) throw() \
00446       { c.dump(s); return s; } \
00447 }
00448 
00449 namespace gpstk
00450 {
00453    NEW_EXCEPTION_CLASS(InvalidParameter, Exception);
00454 
00457    NEW_EXCEPTION_CLASS(InvalidRequest, Exception);
00458 
00461    NEW_EXCEPTION_CLASS(AssertionFailure, Exception);
00462 
00465    NEW_EXCEPTION_CLASS(AccessError, Exception);
00466 
00469    NEW_EXCEPTION_CLASS(IndexOutOfBoundsException, Exception);
00470 
00473    NEW_EXCEPTION_CLASS(InvalidArgumentException, Exception);
00474 
00477    NEW_EXCEPTION_CLASS(ConfigurationException, Exception);
00478 
00481    NEW_EXCEPTION_CLASS(FileMissingException, Exception);
00482 
00485    NEW_EXCEPTION_CLASS(SystemSemaphoreException, Exception);
00486 
00489    NEW_EXCEPTION_CLASS(SystemPipeException, Exception);
00490 
00493    NEW_EXCEPTION_CLASS(SystemQueueException, Exception);
00494 
00497    NEW_EXCEPTION_CLASS(OutOfMemory, Exception);
00498 
00501    NEW_EXCEPTION_CLASS(ObjectNotFound, AccessError);
00502 
00505    NEW_EXCEPTION_CLASS(NullPointerException, Exception);
00506 
00509    NEW_EXCEPTION_CLASS(UnimplementedException, Exception);
00510 
00511 } // namespace gpstk
00512 #endif
00513 
00514 
00515 

Generated on Wed May 22 03:31:07 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1