Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-04 09:15:44

0001 // Copyright (c) 1992-1999 Matra Datavision
0002 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _OSD_HeaderFile
0016 #define _OSD_HeaderFile
0017 
0018 #include <Standard.hxx>
0019 #include <Standard_PCharacter.hxx>
0020 #include <OSD_SignalMode.hxx>
0021 
0022 //! Set of Operating System Dependent (OSD) tools.
0023 class OSD
0024 {
0025 public:
0026   DEFINE_STANDARD_ALLOC
0027 
0028   //! Sets or removes signal and FPE (floating-point exception) handlers.
0029   //! OCCT signal handlers translate signals raised by C subsystem to C++
0030   //! exceptions inheriting Standard_Failure.
0031   //!
0032   //! ### Windows-specific notes
0033   //!
0034   //! Compiled with MS VC++ sets 3 main handlers:
0035   //! @li Signal handlers (via ::signal() functions) that translate system signals
0036   //! (SIGSEGV, SIGFPE, SIGILL) into C++ exceptions (classes inheriting
0037   //! Standard_Failure). They only be called if function ::raise() is called
0038   //! with one of supported signal type set.
0039   //! @li Exception handler OSD::WntHandler() (via ::SetUnhandledExceptionFilter())
0040   //! that will be used when user's code is compiled with /EHs option.
0041   //! @li Structured exception (SE) translator (via _set_se_translator()) that
0042   //! translates SE exceptions (aka asynchronous exceptions) into the
0043   //! C++ exceptions inheriting Standard_Failure. This translator will be
0044   //! used when user's code is compiled with /EHa option.
0045   //!
0046   //! This approach ensures that regardless of the option the user chooses to
0047   //! compile his code with (/EHs or /EHa), signals (or SE exceptions) will be
0048   //! translated into Open CASCADE C++ exceptions.
0049   //!
0050   //! MinGW should use SEH exception mode for signal handling to work.
0051   //!
0052   //! ### Linux-specific notes
0053   //!
0054   //! OSD::SetSignal() sets handlers (via ::sigaction()) for multiple signals
0055   //! (SIGFPE, SIGSEGV, etc).
0056   //!
0057   //! ### Common notes
0058   //!
0059   //! If @a theFloatingSignal is TRUE then floating point exceptions will
0060   //! generate SIGFPE in accordance with the mask
0061   //! - Windows: _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW,
0062   //!            see _controlfp() system function.
0063   //! - Linux:   FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW,
0064   //!            see feenableexcept() system function.
0065   //!
0066   //! If @a theFloatingSignal is FALSE then floating point calculations will gracefully
0067   //! complete regardless of occurred exceptions (e.g. division by zero).
0068   //! Otherwise the (thread-specific) FPE flags are set to raise signal if one of
0069   //! floating-point exceptions (division by zero, overflow, or invalid operation) occurs.
0070   //!
0071   //! The recommended approach is to call OSD::SetSignal() in the beginning of the
0072   //! execution of the program, in function main() or its equivalent.
0073   //! In multithreaded programs it is advisable to call OSD::SetSignal() or
0074   //! OSD::SetThreadLocalSignal() with the same parameters in other threads where
0075   //! OCCT is used, to ensure consistency of behavior.
0076   //!
0077   //! Note that in order to handle signals as C++ exceptions on Linux and under
0078   //! MinGW on Windows it is necessary to compile both OCCT and application with
0079   //! OCC_CONVERT_SIGNALS macro, and use macro OCC_CATCH_SIGNALS within each try{}
0080   //! block that has to catch this kind of exceptions.
0081   //!
0082   //! Refer to documentation of Standard_ErrorHandler.hxx for details.
0083   Standard_EXPORT static void SetSignal(OSD_SignalMode theSignalMode, bool theFloatingSignal);
0084 
0085   //! Sets signal and FPE handlers.
0086   //! Short-cut for OSD::SetSignal (OSD_SignalMode_Set, theFloatingSignal).
0087   static void SetSignal(const bool theFloatingSignal = true)
0088   {
0089     SetSignal(OSD_SignalMode_Set, theFloatingSignal);
0090   }
0091 
0092   //! Initializes thread-local signal handlers.
0093   //! This includes _set_se_translator() on Windows platform, and SetFloatingSignal().
0094   //! The main purpose of this method is initializing handlers for newly created threads
0095   //! without overriding global handlers (set by application or by OSD::SetSignal()).
0096   Standard_EXPORT static void SetThreadLocalSignal(OSD_SignalMode theSignalMode,
0097                                                    bool           theFloatingSignal);
0098 
0099   //! Enables / disables generation of C signal on floating point exceptions (FPE).
0100   //! This call does NOT register a handler for signal raised in case of FPE -
0101   //! SetSignal() should be called beforehand for complete setup.
0102   //! Note that FPE setting is thread-local, new threads inherit it from parent.
0103   Standard_EXPORT static void SetFloatingSignal(bool theFloatingSignal);
0104 
0105   //! Returns signal mode set by the last call to SetSignal().
0106   //! By default, returns OSD_SignalMode_AsIs.
0107   Standard_EXPORT static OSD_SignalMode SignalMode();
0108 
0109   //! Returns true if floating point exceptions will raise C signal
0110   //! according to current (platform-dependent) settings in this thread.
0111   Standard_EXPORT static bool ToCatchFloatingSignals();
0112 
0113   //! Commands the process to sleep for a number of seconds.
0114   Standard_EXPORT static void SecSleep(const int theSeconds);
0115 
0116   //! Commands the process to sleep for a number of milliseconds
0117   Standard_EXPORT static void MilliSecSleep(const int theMilliseconds);
0118 
0119   //! Converts aReal into aCstring in exponential format with a period as decimal point,
0120   //! no thousand separator and no grouping of digits.
0121   //! The conversion is independent from the current locale
0122   Standard_EXPORT static bool RealToCString(const double aReal, Standard_PCharacter& aString);
0123 
0124   //! Converts aCstring representing a real with a period as decimal point,
0125   //! no thousand separator and no grouping of digits into aReal.
0126   //!
0127   //! The conversion is independent from the current locale.
0128   Standard_EXPORT static bool CStringToReal(const char* const aString, double& aReal);
0129 
0130   //! since Windows NT does not support 'SIGINT' signal like UNIX,
0131   //! then this method checks whether Ctrl-Break keystroke was or
0132   //! not. If yes then raises Exception_CTRL_BREAK.
0133   Standard_EXPORT static void ControlBreak();
0134 
0135   //! Returns a length of stack trace to be put into exception redirected from signal;
0136   //! 0 by default meaning no stack trace.
0137   //! @sa Standard_Failure::GetStackString()
0138   Standard_EXPORT static int SignalStackTraceLength();
0139 
0140   //! Sets a length of stack trace to be put into exception redirected from signal.
0141   Standard_EXPORT static void SetSignalStackTraceLength(int theLength);
0142 };
0143 
0144 #endif // _OSD_HeaderFile