ESYS13  Revision_
EsysException.h
Go to the documentation of this file.
00001 
00002 /*******************************************************
00003 *
00004 * Copyright (c) 2003-2012 by University of Queensland
00005 * Earth Systems Science Computational Center (ESSCC)
00006 * http://www.uq.edu.au/esscc
00007 *
00008 * Primary Business: Queensland, Australia
00009 * Licensed under the Open Software License version 3.0
00010 * http://www.opensource.org/licenses/osl-3.0.php
00011 *
00012 *******************************************************/
00013 
00014 
00015 #ifndef ESYSEXCEPTION_H
00016 #define ESYSEXCEPTION_H
00017 #include "system_dep.h"
00018 
00019 #include <string>
00020 #include <exception>
00021 #include <iostream>
00022 
00023 namespace esysUtils
00024 {
00044   class EsysException : public std::exception
00045   {
00046 
00047   protected:
00048 
00049      typedef std::exception Parent;
00050 
00051 
00052   public:
00057     ESYSUTILS_DLL_API
00058     EsysException();
00059 
00066     ESYSUTILS_DLL_API
00067     EsysException(const std::string &exceptionReason);
00068 
00075     ESYSUTILS_DLL_API
00076     EsysException( const char *cStr );
00077 
00084     ESYSUTILS_DLL_API
00085     EsysException(const EsysException &other);
00086 
00088     ESYSUTILS_DLL_API
00089     virtual ~EsysException() THROW(NO_ARG);
00090 
00100     ESYSUTILS_DLL_API
00101     EsysException &
00102     operator=(const EsysException &other) THROW(NO_ARG);
00103 
00111     inline
00112     const std::string & toString() const;
00113 
00121     ESYSUTILS_DLL_API
00122     virtual const std::string & exceptionName() const;
00123 
00130     inline
00131     const std::string& reason() const;
00132 
00139     inline
00140     void setReason(const std::string &new_reason);
00141 
00149     ESYSUTILS_DLL_API
00150     inline
00151     virtual const char* what() const THROW(NO_ARG);
00152 
00153 
00158     inline
00159     void updateMessage();
00160 
00161 
00162   private:
00163     //
00164     // the exception reason
00165     std::string m_reason;
00166 
00167     //
00168     // the full exception message 
00169     std::string m_exceptionMessage;
00170 
00171     //
00172     // the exception name is immutable and class-wide.
00173     // Inheritor note; you need one of these too.
00174     // and an overloaded exceptionName() in your .cpp implementation file. 
00175     static const std::string exceptionNameValue;
00176 
00177   };
00178 
00187   ESYSUTILS_DLL_API
00188   std::ostream &operator<<(std::ostream &output, EsysException &inException);
00189 
00190 
00192 
00193   const std::string & EsysException::reason() const
00194   {
00195     return m_reason;
00196   }
00197   
00198   // return the message as a std::string
00199   const std::string & EsysException::toString() const
00200   {
00201     return m_exceptionMessage;
00202   }
00203 
00204   void EsysException::setReason(const std::string &new_reason)
00205   {
00206     m_reason = new_reason;
00207     updateMessage();
00208   }
00209 
00210   const char*  EsysException::what() const THROW(NO_ARG)
00211   {
00212     return m_exceptionMessage.c_str();
00213   }
00214 
00215   void EsysException::updateMessage()
00216   {
00217     m_exceptionMessage = exceptionName() + ": " + m_reason;
00218   }
00219 
00220 }
00221 
00222 #endif