1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : DSC_Exception.hxx
24 // Author : Eric Fayolle (EDF)
27 #ifndef DSC_EXCEPTION_HXX
28 #define DSC_EXCEPTION_HXX
30 #include "Utils_SALOME_Exception.hxx"
36 #include "utilities.h"
48 #if defined(_DEBUG_) || defined(_DEBUG)
50 # define LOC(message) (message), __FILE__ , __LINE__ , __FUNCTION__
52 # define LOC(message) (message), __FILE__, __LINE__
55 # define LOC(message) (message)
61 * Class OSS is useful when streaming data through a function
62 * that expect a string as parameter
67 std::ostringstream oss_;
70 explicit OSS() : oss_() {}
73 OSS & operator<<(T obj)
79 operator std::string()
84 // Surtout ne pas écrire le code suivant:
85 // car oss_.str() renvoie une string temporaire
86 // operator const char*()
88 // return oss_.str().c_str();
91 }; /* end class OSS */
95 // Cette fonction provient de Utils_SALOME_Exception
96 // Solution pas très élégante mais contrainte par les manques de la classe SALOME_Exception
97 const char *makeText( const char *text, const char *fileName, const unsigned int lineNumber);
99 struct DSC_Exception : public SALOME_Exception {
101 // Attention, en cas de modification des paramètres par défaut
102 // il est necessaire de les repporter dans la macro DSC_EXCEPTION ci-dessous
103 // Le constructeur de la SALOME_Exception demande une chaine non vide
104 // Du coup on est obliger de la désallouer avant d'y mettre la notre
105 // car le what n'est pas virtuel donc il faut que le contenu de SALOME_Exception::_text
107 // Ne pas mettre lineNumber=0 à cause du calcul log dans la SALOME_Exception si fileName est défini
108 DSC_Exception( const std::string & text,
109 const char *fileName="",
110 const unsigned int lineNumber=0,
111 const char *funcName="" ):
112 SALOME_Exception(text.c_str()) ,
114 _filefuncName(setFileFuncName(fileName?fileName:"",funcName?funcName:"")),
115 _lineNumber(lineNumber),
116 _exceptionName("DSC_Exception")
118 // Mise en cohérence avec l'exception SALOME (à revoir)
119 delete [] ((char*)SALOME_Exception::_text);
120 if (! _filefuncName.empty() )
121 SALOME_Exception::_text = makeText(text.c_str(),_filefuncName.c_str(),lineNumber) ;
123 SALOME_Exception::_text = makeText(text.c_str(),0,lineNumber) ;
126 oss << _exceptionName ;
127 if (!_filefuncName.empty() ) oss << " in " << _filefuncName;
128 if (_lineNumber) oss << " [" << _lineNumber << "]";
129 oss << " : " << _dscText;
133 virtual const char* what( void ) const throw ()
135 return _what.c_str() ;
138 // L'opérateur = de SALOME_Exception n'est pas défini
139 // problème potentiel concernant la recopie de son pointeur _text
141 // Le destructeur de la SALOME_Exception devrait être virtuel
142 // sinon pb avec nos attributs de type pointeur.
143 virtual ~DSC_Exception(void) throw() {};
145 virtual const std::string & getExceptionName() const {return _exceptionName;};
149 std::string setFileFuncName(const char * fileName, const char * funcName) {
153 if ( strcmp(fileName,"") )
154 oss << fileName << "##" << funcName;
159 //DSC_Exception(void) {};
161 std::string _dscText;
162 std::string _filefuncName;
164 std::string _exceptionName;
168 #define DSC_EXCEPTION(Derived) struct Derived : public DSC_Exception { \
169 Derived ( const std::string & text, const char *fileName="", const unsigned int lineNumber=0, const char *funcName="" \
170 ) : DSC_Exception(text,fileName,lineNumber,funcName) { \
171 _exceptionName = #Derived; \
173 virtual ~Derived(void) throw();\
176 //Sert à eviter le problème d'identification RTTI des exceptions
177 //Crée un unique typeInfo pour tous les bibliothèques composants SALOME
178 //dans un fichier cxx
179 #define DSC_EXCEPTION_CXX(NameSpace,Derived) NameSpace::Derived::~Derived(void) throw() {};
181 #endif /* DSC_EXCEPTION_HXX */