Salome HOME
updated copyright message
[modules/gui.git] / src / CASCatch / CASCatch_ErrorHandler.cxx
1 // Copyright (C) 2007-2023  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   : CASCatch_ErrorHandler.cxx
24 // Author : Sergey RUIN, Open CASCADE S.A.S (sergey.ruin@opencascade.com)
25 //
26 #ifdef NO_CXX_EXCEPTION
27
28 #include "CASCatch_ErrorHandler.hxx"
29
30 // During setjmp()/longjmp() K_SETJMP_CASCatch is non zero (try)
31 // So if there is an abort request and if K_SETJMP_CASCatch is non zero, the abort
32 // request will be ignored. If the abort request do a raise during a setjmp
33 // or a longjmp, there will be a "terminating SEGV" impossible to handle.
34
35
36 Standard_EXPORT int K_SETJMP_CASCatch = 0 ; 
37
38 static Handle(CASCatch_Failure) GlbError;  //Last caught Error, Null if there is no error
39
40 static CASCatch_ErrorHandler*   Top = 0;   //The top of the Errors Stack
41
42 //=======================================================================
43 //function : CASCatch_ErrorHandler
44 //purpose  : Constructor
45 //=======================================================================
46 CASCatch_ErrorHandler::CASCatch_ErrorHandler ()
47 {
48   Previous = Top;
49   Top      = this;
50   CaughtError.Nullify();
51   GlbError.Nullify();
52 }
53
54 //=======================================================================
55 //function : ~CASCatch_ErrorHandler
56 //purpose  : Destructor : Delete the ErrorHandler and Abort if there is a 'Error'.
57 //=======================================================================
58 CASCatch_ErrorHandler::~CASCatch_ErrorHandler()
59 {
60   Top = Top->Previous;
61   if( !GlbError.IsNull() ) Abort();
62 }
63
64 //=======================================================================
65 //function : Abort: make a longjmp to the saved Context.
66 //purpose  : Abort if there is a non null 'Error'
67 //=======================================================================
68 void CASCatch_ErrorHandler::Abort ()
69 {
70   //==== Check if can do the "longjmp" =======================================
71   if(Top == NULL || Top->Label == NULL) {
72     cout << "*** Abort *** an exception was raised, but no catch was found." << endl;
73     cout << "\t... The exception is:" << GlbError;
74     exit(1);
75   }
76
77 #ifdef DO_ABORT
78   if ( K_SETJMP_CASCatch )
79     cout << "Recursive abort ===> Terminating SEGV ..." << endl ;
80   K_SETJMP_CASCatch = 1 ;
81 #endif
82
83   longjmp(Top->Label, Standard_True);
84 }
85
86 //=======================================================================
87 //function : Catches
88 //purpose  : If there is a 'Error', and it is in good type 
89 //           returns True and clean 'Error', else returns False.
90 //=======================================================================
91 Standard_Boolean CASCatch_ErrorHandler::Catches 
92   (const Handle(Standard_Type)& AType) 
93 {
94 #ifdef DO_ABORT
95   K_SETJMP_CASCatch = 0 ;
96 #endif
97   if(GlbError.IsNull())
98     return Standard_False;
99
100   if(GlbError->IsKind(AType)){
101     CaughtError = GlbError;
102     GlbError.Nullify();
103     return Standard_True;
104   } else {
105     return Standard_False;
106   }
107 }
108
109 //=======================================================================
110 //function : LastCaughtError
111 //purpose  : 
112 //=======================================================================
113 Handle(CASCatch_Failure) CASCatch_ErrorHandler::LastCaughtError()
114 {
115   return Top->CaughtError;
116 }
117
118 //=======================================================================
119 //function : Error
120 //purpose  : 
121 //=======================================================================
122 void CASCatch_ErrorHandler::Error(const Handle(CASCatch_Failure)& aError)
123 {
124   GlbError = aError;
125 }
126
127 #endif