Salome HOME
Compilation with cmake
[modules/kernel.git] / src / DSC / DSC_User / DSC_Exception.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : DSC_Exception.hxx
23 //  Author : Eric Fayolle (EDF)
24 //  Module : KERNEL
25 //
26 #ifndef DSC_EXCEPTION_HXX
27 #define DSC_EXCEPTION_HXX
28
29 #include "Utils_SALOME_Exception.hxx"
30 #include <string>
31 #include <iostream>
32 #include <sstream>
33 #include <memory>
34
35 #include "utilities.h"
36
37 #ifndef WIN32
38 extern "C"
39 {
40 #endif
41 #include <string.h>
42 #ifndef WIN32
43 }
44 #endif
45
46
47 #if defined(_DEBUG_) || defined(_DEBUG)
48 # ifdef __GNUC__
49 #  define LOC(message) (message), __FILE__ , __LINE__ , __FUNCTION__
50 # else
51 #  define LOC(message) (message), __FILE__, __LINE__
52 # endif
53 #else
54 # define LOC(message)  (message)
55 #endif
56
57
58 #ifndef SWIG
59 /**
60  * Class OSS is useful when streaming data through a function
61  * that expect a string as parameter
62  */
63 class OSS
64 {
65 private:
66   std::ostringstream oss_;
67
68 public:
69   explicit OSS() : oss_() {}
70
71   template <class T>
72   OSS & operator<<(T obj)
73   {
74     oss_ << obj;
75     return *this;
76   }
77
78   operator std::string()
79   {
80     return oss_.str();
81   }
82
83   // Surtout ne pas écrire le code suivant:
84   // car oss_.str() renvoie une string temporaire
85   //   operator const char*()
86   //   {
87   //     return oss_.str().c_str();
88   //   }
89
90 }; /* end class OSS */
91 #endif
92
93
94 // Cette fonction provient de Utils_SALOME_Exception
95 // Solution pas très élégante mais contrainte par les manques de la classe SALOME_Exception
96 const char *makeText( const char *text, const char *fileName, const unsigned int lineNumber);
97
98 struct DSC_Exception : public SALOME_Exception {
99
100   // Attention, en cas de modification des paramètres par défaut
101   // il est necessaire de les repporter dans la macro DSC_EXCEPTION ci-dessous
102   // Le constructeur de la SALOME_Exception demande une chaine non vide
103   // Du coup on est obliger de la désallouer avant d'y mettre la notre
104   // car le what n'est pas virtuel donc il faut que le contenu de SALOME_Exception::_text
105   // soit utilisable.
106   // Ne pas mettre lineNumber=0 à cause du calcul log dans la SALOME_Exception si fileName est défini
107   DSC_Exception( const std::string & text, 
108                  const char *fileName="", 
109                  const unsigned int lineNumber=0, 
110                  const char *funcName="" ):
111     SALOME_Exception(text.c_str()) ,
112     _dscText(text),
113     _filefuncName(setFileFuncName(fileName?fileName:"",funcName?funcName:"")),
114     _lineNumber(lineNumber),
115     _exceptionName("DSC_Exception")
116   {
117     // Mise en cohérence avec l'exception SALOME (à revoir)
118     delete [] ((char*)SALOME_Exception::_text);
119     if (! _filefuncName.empty() )
120       SALOME_Exception::_text = makeText(text.c_str(),_filefuncName.c_str(),lineNumber) ;
121     else
122       SALOME_Exception::_text = makeText(text.c_str(),0,lineNumber) ;
123     
124     OSS oss ;
125     oss << _exceptionName ;
126     if (!_filefuncName.empty() ) oss << " in " << _filefuncName;
127     if (_lineNumber) oss << " [" << _lineNumber << "]";
128     oss << " : " << _dscText;
129     _what = oss;
130   }
131
132   virtual const char* what( void ) const throw ()
133   {
134     return _what.c_str()  ;
135   }
136
137   // L'opérateur = de SALOME_Exception n'est pas défini
138   // problème potentiel concernant la recopie de son pointeur _text
139     
140   // Le destructeur de la SALOME_Exception devrait être virtuel
141   // sinon pb avec nos attributs de type pointeur.
142   virtual ~DSC_Exception(void) throw() {};
143
144   virtual const std::string & getExceptionName() const {return _exceptionName;};
145
146 private:
147
148   std::string setFileFuncName(const char * fileName, const char * funcName) {
149     ASSERT(fileName);
150     ASSERT(funcName);
151     OSS oss;
152     if ( strcmp(fileName,"") )
153       oss << fileName << "##" << funcName;
154   
155     return oss;
156   };
157
158   //DSC_Exception(void) {};
159 protected:
160   std::string  _dscText;
161   std::string  _filefuncName;
162   std::string  _exceptionName;
163   int          _lineNumber;
164   std::string  _what;
165 };
166
167 #define DSC_EXCEPTION(Derived) struct Derived : public DSC_Exception { \
168   Derived ( const std::string & text, const char *fileName="", const unsigned int lineNumber=0, const char *funcName="" \
169             ) : DSC_Exception(text,fileName,lineNumber,funcName) {      \
170     _exceptionName = #Derived; \
171   } \
172     virtual ~Derived(void) throw();\
173 };\
174
175 //Sert à eviter le problème d'identification RTTI des exceptions
176 //Crée un unique typeInfo pour tous les bibliothèques composants SALOME
177 //dans un fichier cxx
178 #define DSC_EXCEPTION_CXX(NameSpace,Derived) NameSpace::Derived::~Derived(void) throw() {};
179
180 #endif /* DSC_EXCEPTION_HXX */