Version: 8.3.0
DSC_Exception.hxx
Go to the documentation of this file.
1 // Copyright (C) 2007-2016 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, 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 
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 
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 
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);
98 
100 
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
106  // soit utilisable.
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()) ,
113  _dscText(text),
114  _filefuncName(setFileFuncName(fileName?fileName:"",funcName?funcName:"")),
115  _lineNumber(lineNumber),
116  _exceptionName("DSC_Exception")
117  {
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) ;
122  else
123  SALOME_Exception::_text = makeText(text.c_str(),0,lineNumber) ;
124 
125  OSS oss ;
126  oss << _exceptionName ;
127  if (!_filefuncName.empty() ) oss << " in " << _filefuncName;
128  if (_lineNumber) oss << " [" << _lineNumber << "]";
129  oss << " : " << _dscText;
130  _what = oss;
131  }
132 
133  virtual const char* what( void ) const throw ()
134  {
135  return _what.c_str() ;
136  }
137 
138  // L'opérateur = de SALOME_Exception n'est pas défini
139  // problème potentiel concernant la recopie de son pointeur _text
140 
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() {};
144 
145  virtual const std::string & getExceptionName() const {return _exceptionName;};
146 
147 private:
148 
149  std::string setFileFuncName(const char * fileName, const char * funcName) {
150  ASSERT(fileName);
151  ASSERT(funcName);
152  OSS oss;
153  if ( strcmp(fileName,"") )
154  oss << fileName << "##" << funcName;
155 
156  return oss;
157  };
158 
159  //DSC_Exception(void) {};
160 protected:
161  std::string _dscText;
162  std::string _filefuncName;
164  std::string _exceptionName;
165  std::string _what;
166 };
167 
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; \
172  } \
173  virtual ~Derived(void) throw();\
174 };\
175 
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() {};
180 
181 #endif /* DSC_EXCEPTION_HXX */