Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:21:46

0001 // Created on: 1991-09-05
0002 // Created by: Philippe COICADAN
0003 // Copyright (c) 1991-1999 Matra Datavision
0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _Standard_Failure_HeaderFile
0018 #define _Standard_Failure_HeaderFile
0019 
0020 #include <Standard_Type.hxx>
0021 
0022 #include <Standard_CString.hxx>
0023 #include <Standard_Transient.hxx>
0024 #include <Standard_OStream.hxx>
0025 #include <Standard_SStream.hxx>
0026 
0027 DEFINE_STANDARD_HANDLE(Standard_Failure, Standard_Transient)
0028 
0029 //! Forms the root of the entire exception hierarchy.
0030 class Standard_Failure : public Standard_Transient
0031 {
0032 public:
0033 
0034   //! Creates a status object of type "Failure".
0035   Standard_EXPORT Standard_Failure();
0036 
0037   //! Copy constructor
0038   Standard_EXPORT Standard_Failure (const Standard_Failure& f);
0039 
0040   //! Creates a status object of type "Failure".
0041   //! @param theDesc [in] exception description
0042   Standard_EXPORT Standard_Failure (const Standard_CString theDesc);
0043 
0044   //! Creates a status object of type "Failure" with stack trace.
0045   //! @param theDesc [in] exception description
0046   //! @param theStackTrace [in] associated stack trace
0047   Standard_EXPORT Standard_Failure (const Standard_CString theDesc,
0048                                     const Standard_CString theStackTrace);
0049 
0050   //! Assignment operator
0051   Standard_EXPORT Standard_Failure& operator= (const Standard_Failure& f);
0052 
0053   //! Destructor
0054   Standard_EXPORT ~Standard_Failure();
0055 
0056   //! Prints on the stream @p theStream the exception name followed by the error message.
0057   //!
0058   //! Note: there is a short-cut @c operator<< (Standard_OStream&, Handle(Standard_Failure)&)
0059   Standard_EXPORT void Print (Standard_OStream& theStream) const;
0060   
0061   //! Returns error message
0062   Standard_EXPORT virtual Standard_CString GetMessageString() const;
0063   
0064   //! Sets error message
0065   Standard_EXPORT virtual void SetMessageString (const Standard_CString theMessage);
0066 
0067   //! Returns the stack trace string
0068   Standard_EXPORT virtual Standard_CString GetStackString() const;
0069 
0070   //! Sets the stack trace string
0071   Standard_EXPORT virtual void SetStackString (const Standard_CString theStack);
0072 
0073   Standard_EXPORT void Reraise();
0074   
0075   Standard_EXPORT void Reraise (const Standard_CString aMessage);
0076   
0077   //! Reraises a caught exception and changes its error message.
0078   Standard_EXPORT void Reraise (const Standard_SStream& aReason);
0079 
0080 public:
0081 
0082   //! Raises an exception of type "Failure" and associates
0083   //! an error message to it. The message can be printed
0084   //! in an exception handler.
0085   Standard_EXPORT static void Raise (const Standard_CString aMessage = "");
0086   
0087   //! Raises an exception of type "Failure" and associates
0088   //! an error message to it. The message can be constructed
0089   //! at run-time.
0090   Standard_EXPORT static void Raise (const Standard_SStream& aReason);
0091   
0092   //! Used to construct an instance of the exception object as a handle.
0093   //! Shall be used to protect against possible construction of exception object in C stack,
0094   //! which is dangerous since some of methods require that object was allocated dynamically.
0095   Standard_EXPORT static Handle(Standard_Failure) NewInstance (Standard_CString theMessage);
0096 
0097   //! Used to construct an instance of the exception object as a handle.
0098   Standard_EXPORT static Handle(Standard_Failure) NewInstance (Standard_CString theMessage,
0099                                                                Standard_CString theStackTrace);
0100 
0101   //! Returns the default length of stack trace to be captured by Standard_Failure constructor;
0102   //! 0 by default meaning no stack trace.
0103   Standard_EXPORT static Standard_Integer DefaultStackTraceLength();
0104 
0105   //! Sets default length of stack trace to be captured by Standard_Failure constructor.
0106   Standard_EXPORT static void SetDefaultStackTraceLength (Standard_Integer theNbStackTraces);
0107 
0108 public:
0109 
0110   //! Used to throw CASCADE exception from C signal handler.
0111   //! On platforms that do not allow throwing C++ exceptions
0112   //! from this handler (e.g. Linux), uses longjump to get to
0113   //! the current active signal handler, and only then is
0114   //! converted to C++ exception.
0115   Standard_EXPORT void Jump();
0116 
0117   DEFINE_STANDARD_RTTIEXT(Standard_Failure,Standard_Transient)
0118 
0119 protected:
0120 
0121   //! Used only if standard C++ exceptions are used.
0122   //! Throws exception of the same type as this by C++ throw,
0123   //! and stores current object as last thrown exception,
0124   //! to be accessible by method Caught()
0125   Standard_EXPORT virtual void Throw() const;
0126 
0127 private:
0128 
0129   //! Reference-counted string,
0130   //! Memory block is allocated with an extra 4-byte header (int representing number of references)
0131   //! using low-level malloc() to avoid exceptions.
0132   struct StringRef
0133   {
0134     Standard_Integer   Counter;
0135     Standard_Character Message[1];
0136 
0137     //! Return message string.
0138     Standard_CString GetMessage() const { return (Standard_CString )&Message[0]; }
0139 
0140     //! Allocate reference-counted message string.
0141     static StringRef* allocate_message (Standard_CString theString);
0142 
0143     //! Copy reference-counted message string.
0144     static StringRef* copy_message (StringRef* theString);
0145 
0146     //! Release reference-counted message string.
0147     static void deallocate_message (StringRef* theString);
0148   };
0149 
0150 private:
0151 
0152   StringRef* myMessage;
0153   StringRef* myStackTrace;
0154 
0155 };
0156 
0157 // =======================================================================
0158 // function : operator<<
0159 // purpose  :
0160 // =======================================================================
0161 inline Standard_OStream& operator<< (Standard_OStream& theStream,
0162                                      const Handle(Standard_Failure)& theFailure)
0163 {
0164   theFailure->Print (theStream);
0165   return theStream;
0166 }
0167 
0168 // =======================================================================
0169 // function : operator<<
0170 // purpose  :
0171 // =======================================================================
0172 inline Standard_OStream& operator<< (Standard_OStream& theStream,
0173                                      const Standard_Failure& theFailure)
0174 {
0175   theFailure.Print (theStream);
0176   return theStream;
0177 }
0178 
0179 #endif // _Standard_Failure_HeaderFile