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