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