SALOME documentation central

src/DSC/DSC_User/DSC_Exception.hxx

00001 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 //  This library is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU Lesser General Public
00008 //  License as published by the Free Software Foundation; either
00009 //  version 2.1 of the License.
00010 //
00011 //  This library is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 //  Lesser General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU Lesser General Public
00017 //  License along with this library; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : DSC_Exception.hxx
00023 //  Author : Eric Fayolle (EDF)
00024 //  Module : KERNEL
00025 //
00026 #ifndef DSC_EXCEPTION_HXX
00027 #define DSC_EXCEPTION_HXX
00028 
00029 #include "Utils_SALOME_Exception.hxx"
00030 #include <string>
00031 #include <iostream>
00032 #include <sstream>
00033 #include <memory>
00034 
00035 #include "utilities.h"
00036 
00037 #ifndef WIN32
00038 extern "C"
00039 {
00040 #endif
00041 #include <string.h>
00042 #ifndef WIN32
00043 }
00044 #endif
00045 
00046 
00047 #if defined(_DEBUG_) || defined(_DEBUG)
00048 # ifdef __GNUC__
00049 #  define LOC(message) (message), __FILE__ , __LINE__ , __FUNCTION__
00050 # else
00051 #  define LOC(message) (message), __FILE__, __LINE__
00052 # endif
00053 #else
00054 # define LOC(message)  (message)
00055 #endif
00056 
00057 
00058 #ifndef SWIG
00059 
00063 class OSS
00064 {
00065 private:
00066   std::ostringstream oss_;
00067 
00068 public:
00069   explicit OSS() : oss_() {}
00070 
00071   template <class T>
00072   OSS & operator<<(T obj)
00073   {
00074     oss_ << obj;
00075     return *this;
00076   }
00077 
00078   operator std::string()
00079   {
00080     return oss_.str();
00081   }
00082 
00083   // Surtout ne pas écrire le code suivant:
00084   // car oss_.str() renvoie une string temporaire
00085   //   operator const char*()
00086   //   {
00087   //     return oss_.str().c_str();
00088   //   }
00089 
00090 }; /* end class OSS */
00091 #endif
00092 
00093 
00094 // Cette fonction provient de Utils_SALOME_Exception
00095 // Solution pas très élégante mais contrainte par les manques de la classe SALOME_Exception
00096 const char *makeText( const char *text, const char *fileName, const unsigned int lineNumber);
00097 
00098 struct DSC_Exception : public SALOME_Exception {
00099 
00100   // Attention, en cas de modification des paramètres par défaut
00101   // il est necessaire de les repporter dans la macro DSC_EXCEPTION ci-dessous
00102   // Le constructeur de la SALOME_Exception demande une chaine non vide
00103   // Du coup on est obliger de la désallouer avant d'y mettre la notre
00104   // car le what n'est pas virtuel donc il faut que le contenu de SALOME_Exception::_text
00105   // soit utilisable.
00106   // Ne pas mettre lineNumber=0 à cause du calcul log dans la SALOME_Exception si fileName est défini
00107   DSC_Exception( const std::string & text, 
00108            const char *fileName="", 
00109            const unsigned int lineNumber=0, 
00110            const char *funcName="" ):
00111     SALOME_Exception(text.c_str()) ,
00112     _dscText(text),
00113     _filefuncName(setFileFuncName(fileName?fileName:"",funcName?funcName:"")),
00114     _lineNumber(lineNumber),
00115     _exceptionName("DSC_Exception")
00116   {
00117     // Mise en cohérence avec l'exception SALOME (à revoir)
00118     delete [] ((char*)SALOME_Exception::_text);
00119     if (! _filefuncName.empty() )
00120       SALOME_Exception::_text = makeText(text.c_str(),_filefuncName.c_str(),lineNumber) ;
00121     else
00122       SALOME_Exception::_text = makeText(text.c_str(),0,lineNumber) ;
00123     
00124     OSS oss ;
00125     oss << _exceptionName ;
00126     if (!_filefuncName.empty() ) oss << " in " << _filefuncName;
00127     if (_lineNumber) oss << " [" << _lineNumber << "]";
00128     oss << " : " << _dscText;
00129     _what = oss;
00130   }
00131 
00132   virtual const char* what( void ) const throw ()
00133   {
00134     return _what.c_str()  ;
00135   }
00136 
00137   // L'opérateur = de SALOME_Exception n'est pas défini
00138   // problème potentiel concernant la recopie de son pointeur _text
00139     
00140   // Le destructeur de la SALOME_Exception devrait être virtuel
00141   // sinon pb avec nos attributs de type pointeur.
00142   virtual ~DSC_Exception(void) throw() {};
00143 
00144   virtual const std::string & getExceptionName() const {return _exceptionName;};
00145 
00146 private:
00147 
00148   std::string setFileFuncName(const char * fileName, const char * funcName) {
00149     ASSERT(fileName);
00150     ASSERT(funcName);
00151     OSS oss;
00152     if ( strcmp(fileName,"") )
00153       oss << fileName << "##" << funcName;
00154   
00155     return oss;
00156   };
00157 
00158   //DSC_Exception(void) {};
00159 protected:
00160   std::string  _dscText;
00161   std::string  _filefuncName;
00162   std::string  _exceptionName;
00163   int          _lineNumber;
00164   std::string  _what;
00165 };
00166 
00167 #define DSC_EXCEPTION(Derived) struct Derived : public DSC_Exception { \
00168   Derived ( const std::string & text, const char *fileName="", const unsigned int lineNumber=0, const char *funcName="" \
00169          ) : DSC_Exception(text,fileName,lineNumber,funcName) {  \
00170     _exceptionName = #Derived; \
00171   } \
00172     virtual ~Derived(void) throw();\
00173 };\
00174 
00175 //Sert à eviter le problème d'identification RTTI des exceptions
00176 //Crée un unique typeInfo pour tous les bibliothèques composants SALOME
00177 //dans un fichier cxx
00178 #define DSC_EXCEPTION_CXX(NameSpace,Derived) NameSpace::Derived::~Derived(void) throw() {};
00179 
00180 #endif /* DSC_EXCEPTION_HXX */