Back to index

salome-gui  6.5.0
CASCatch_ErrorHandler.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : CASCatch_ErrorHandler.cxx
00024 // Author : Sergey RUIN, Open CASCADE S.A.S (sergey.ruin@opencascade.com)
00025 //
00026 #ifdef NO_CXX_EXCEPTION
00027 
00028 #include "CASCatch_ErrorHandler.hxx"
00029 
00030 // During setjmp()/longjmp() K_SETJMP_CASCatch is non zero (try)
00031 // So if there is an abort request and if K_SETJMP_CASCatch is non zero, the abort
00032 // request will be ignored. If the abort request do a raise during a setjmp
00033 // or a longjmp, there will be a "terminating SEGV" impossible to handle.
00034 
00035 
00036 Standard_EXPORT int K_SETJMP_CASCatch = 0 ; 
00037 
00038 static Handle(CASCatch_Failure) GlbError;  //Last caught Error, Null if there is no error
00039 
00040 static CASCatch_ErrorHandler*   Top = 0;   //The top of the Errors Stack
00041 
00042 //=======================================================================
00043 //function : CASCatch_ErrorHandler
00044 //purpose  : Constructor
00045 //=======================================================================
00046 CASCatch_ErrorHandler::CASCatch_ErrorHandler ()
00047 {
00048   Previous = Top;
00049   Top      = this;
00050   CaughtError.Nullify();
00051   GlbError.Nullify();
00052 }
00053 
00054 //=======================================================================
00055 //function : ~CASCatch_ErrorHandler
00056 //purpose  : Destructor : Delete the ErrorHandler and Abort if there is a 'Error'.
00057 //=======================================================================
00058 CASCatch_ErrorHandler::~CASCatch_ErrorHandler()
00059 {
00060   Top = Top->Previous;
00061   if( !GlbError.IsNull() ) Abort();
00062 }
00063 
00064 //=======================================================================
00065 //function : Abort: make a longjmp to the saved Context.
00066 //purpose  : Abort if there is a non null 'Error'
00067 //=======================================================================
00068 void CASCatch_ErrorHandler::Abort ()
00069 {
00070   //==== Check if can do the "longjmp" =======================================
00071   if(Top == NULL || Top->Label == NULL) {
00072     cout << "*** Abort *** an exception was raised, but no catch was found." << endl;
00073     cout << "\t... The exception is:" << GlbError;
00074     exit(1);
00075   }
00076 
00077 #ifdef DO_ABORT
00078   if ( K_SETJMP_CASCatch )
00079     cout << "Recursive abort ===> Terminating SEGV ..." << endl ;
00080   K_SETJMP_CASCatch = 1 ;
00081 #endif
00082 
00083   longjmp(Top->Label, Standard_True);
00084 }
00085 
00086 //=======================================================================
00087 //function : Catches
00088 //purpose  : If there is a 'Error', and it is in good type 
00089 //           returns True and clean 'Error', else returns False.
00090 //=======================================================================
00091 Standard_Boolean CASCatch_ErrorHandler::Catches 
00092   (const Handle(Standard_Type)& AType) 
00093 {
00094 #ifdef DO_ABORT
00095   K_SETJMP_CASCatch = 0 ;
00096 #endif
00097   if(GlbError.IsNull())
00098     return Standard_False;
00099 
00100   if(GlbError->IsKind(AType)){
00101     CaughtError = GlbError;
00102     GlbError.Nullify();
00103     return Standard_True;
00104   } else {
00105     return Standard_False;
00106   }
00107 }
00108 
00109 //=======================================================================
00110 //function : LastCaughtError
00111 //purpose  : 
00112 //=======================================================================
00113 Handle(CASCatch_Failure) CASCatch_ErrorHandler::LastCaughtError()
00114 {
00115   return Top->CaughtError;
00116 }
00117 
00118 //=======================================================================
00119 //function : Error
00120 //purpose  : 
00121 //=======================================================================
00122 void CASCatch_ErrorHandler::Error(const Handle(CASCatch_Failure)& aError)
00123 {
00124   GlbError = aError;
00125 }
00126 
00127 #endif