Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-18 09:21:46

0001 // Created on: 2001-03-20
0002 // Created by: Andrey BETENEV
0003 // Copyright (c) 2001-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef Standard_Assert_HeaderFile
0017 #define Standard_Assert_HeaderFile
0018 
0019 #include <Standard_ProgramError.hxx>
0020 
0021 //!@file
0022 //! This header file defines a set of ASSERT macros intended for use
0023 //! in algorithms for debugging purposes and as a tool to organise
0024 //! checks for abnormal situations in the uniform way.
0025 //!
0026 //! In contrast to C assert() function that terminates the process, these
0027 //! macros provide choice of the action to be performed if assert failed,
0028 //! thus allowing execution to continue when possible.
0029 //! Except for the message for developer that appears only in Debug mode,
0030 //! the macros behave in the same way in both Release and Debug modes.
0031 //!
0032 //!
0033 //! The ASSERT macros differ in the way they react on a wrong situation:
0034 //! - Standard_ASSERT_RAISE:  raises exception Standard_ProgramError
0035 //! - Standard_ASSERT_RETURN: returns specified value (last argument may
0036 //!                           be left empty to return void)
0037 //! - Standard_ASSERT_SKIP:   does nothing
0038 //! - Standard_ASSERT_VOID:   does nothing; even does not evaluate first arg
0039 //!                           when in Release mode
0040 //! - Standard_ASSERT_INVOKE: causes unconditional assert
0041 //! - Standard_ASSERT:        base macro (used by other macros);
0042 //!                           does operation indicated in argument "todo"
0043 //!
0044 //! The assertion is assumed to fail if the first argument is
0045 //! evaluated to zero (false).
0046 //! The first argument is evaluated by all macros except Standard_ASSERT_VOID
0047 //! which does not evaluate first argument when in Release mode.
0048 //! The mode is triggered by preprocessor macro _DEBUG: if it is defined,
0049 //! Debug mode is assumed, Release otherwise.
0050 //!
0051 //! In debug mode, if condition is not satisfied the macros call
0052 //! Standard_ASSERT_INVOKE_ which:
0053 //! - on Windows (under VC++), stops code execution and prompts to attach
0054 //!   debugger to the process immediately.
0055 //! - on POSIX systems, prints message to cerr and raises signal SIGTRAP to stop
0056 //!   execution when under debugger (may terminate the process if not under debugger).
0057 //!
0058 //! The second argument (message) should be string constant ("...").
0059 //!
0060 //! The Standard_STATIC_ASSERT macro is to be used for compile time checks.
0061 //! To use this macro, write:
0062 //!
0063 //!   Standard_STATIC_ASSERT(const_expression);
0064 //!
0065 //! If const_expression is false, a compiler error occurs.
0066 //!
0067 //! The macros are formed as functions and require semicolon at the end.
0068 
0069 // Stub function used to make macros complete C++ operator
0070 inline void Standard_ASSERT_DO_NOTHING() {}
0071 
0072 // User messages are activated in debug mode only
0073 #ifdef _DEBUG
0074   #if (defined(_WIN32) || defined(__WIN32__))
0075     #if defined(_MSC_VER) || defined(__MINGW64__)
0076       // VS-specific intrinsic
0077       #define Standard_ASSERT_DBGBREAK_() __debugbreak()
0078     #else
0079       // WinAPI function
0080       #include <windows.h>
0081       #define Standard_ASSERT_DBGBREAK_() DebugBreak()
0082     #endif
0083   #elif defined(__EMSCRIPTEN__)
0084     #include <emscripten.h>
0085     #define Standard_ASSERT_DBGBREAK_() emscripten_debugger()
0086   #else
0087     // POSIX systems
0088     #include <signal.h>
0089     #define Standard_ASSERT_DBGBREAK_() raise(SIGTRAP)
0090   #endif
0091 
0092   #if defined(_MSC_VER)
0093     #include <crtdbg.h>
0094 
0095 // use debug CRT built-in function that show up message box to user
0096 // with formatted assert description and 3 possible actions
0097 inline bool Standard_ASSERT_REPORT_(const char* theFile,
0098                                     const int   theLine,
0099                                     const char* theExpr,
0100                                     const char* theDesc)
0101 {
0102   // 1 means user pressed Retry button
0103   return _CrtDbgReport(_CRT_ASSERT,
0104                        theFile,
0105                        theLine,
0106                        NULL,
0107                        "%s\n(Condition: \"%s\")\n",
0108                        theDesc,
0109                        theExpr)
0110          == 1;
0111 }
0112   #else
0113 // just log assertion description into standard error stream
0114 inline bool Standard_ASSERT_REPORT_(const char* theFile,
0115                                     const int   theLine,
0116                                     const char* theExpr,
0117                                     const char* theDesc)
0118 {
0119   std::cerr << "ERROR: statement '" << theExpr << "' is not TRUE!\n"
0120             << "\nFile: '" << theFile << "'"
0121             << "\nLine: " << theLine << "\n";
0122   if (theDesc != NULL && *theDesc != '\0')
0123     std::cerr << "Description: " << theDesc << "\n";
0124 
0125   std::cerr << std::flush;
0126   return true;
0127 }
0128   #endif
0129 
0130   // report issue and add debug breakpoint or abort execution
0131   #define Standard_ASSERT_INVOKE_(theExpr, theDesc)                                                \
0132     if (Standard_ASSERT_REPORT_(__FILE__, __LINE__, #theExpr, theDesc))                            \
0133     {                                                                                              \
0134       Standard_ASSERT_DBGBREAK_();                                                                 \
0135     }                                                                                              \
0136     else                                                                                           \
0137       Standard_ASSERT_DO_NOTHING()
0138 
0139   // Basic ASSERT macros
0140   #define Standard_ASSERT(theExpr, theDesc, theAction)                                             \
0141     if (!(theExpr))                                                                                \
0142     {                                                                                              \
0143       Standard_ASSERT_INVOKE_(theExpr, theDesc);                                                   \
0144       theAction;                                                                                   \
0145     }                                                                                              \
0146     else                                                                                           \
0147       Standard_ASSERT_DO_NOTHING()
0148   #define Standard_ASSERT_SKIP(theExpr, theDesc)                                                   \
0149     Standard_ASSERT(theExpr, theDesc, Standard_VOID_RETURN)
0150   #define Standard_ASSERT_VOID(theExpr, theDesc)                                                   \
0151     Standard_ASSERT(theExpr, theDesc, Standard_VOID_RETURN)
0152 #else
0153 
0154   // dummy block
0155   #define Standard_ASSERT_INVOKE_(theExpr, theDesc) Standard_ASSERT_DO_NOTHING()
0156 
0157   // Basic ASSERT macros
0158   #define Standard_ASSERT(theExpr, theDesc, theAction)                                             \
0159     if (!(theExpr))                                                                                \
0160     {                                                                                              \
0161       theAction;                                                                                   \
0162     }                                                                                              \
0163     else                                                                                           \
0164       Standard_ASSERT_DO_NOTHING()
0165   #define Standard_ASSERT_SKIP(theExpr, theDesc) theExpr
0166   #define Standard_ASSERT_VOID(theExpr, theDesc) Standard_ASSERT_DO_NOTHING()
0167 
0168 #endif
0169 
0170 //! Raise exception (Standard_ProgramError) with the provided message
0171 #define Standard_ASSERT_RAISE(theExpr, theDesc)                                                    \
0172   Standard_ASSERT(theExpr,                                                                         \
0173                   theDesc,                                                                         \
0174                   throw Standard_ProgramError("*** ERROR: ASSERT in file '" __FILE__               \
0175                                               "': \n" theDesc " (" #theExpr ")"))
0176 
0177 //! Empty return value for use with Standard_ASSERT_RETURN in void functions.
0178 //! Using this macro instead of empty argument prevents clang-tidy from corrupting the code.
0179 // NOLINTBEGIN(modernize-use-nullptr)
0180 #define Standard_VOID_RETURN
0181 // NOLINTEND(modernize-use-nullptr)
0182 
0183 //! Return from the current function with specified value.
0184 //! Use Standard_VOID_RETURN as theReturnValue for void functions.
0185 #define Standard_ASSERT_RETURN(theExpr, theDesc, theReturnValue)                                   \
0186   Standard_ASSERT(theExpr, theDesc, return theReturnValue)
0187 
0188 //! Raise debug message
0189 #define Standard_ASSERT_INVOKE(theDesc) Standard_ASSERT_INVOKE_(always, theDesc)
0190 
0191 //! Static assert --
0192 //! empty default template
0193 template <bool condition>
0194 struct Standard_Static_Assert
0195 {
0196 };
0197 
0198 //! Static assert -- specialization for condition being true
0199 template <>
0200 struct Standard_Static_Assert<true>
0201 {
0202   static void assert_ok() {}
0203 };
0204 
0205 //! Cause compiler error if argument is not constant expression or
0206 //! evaluates to false
0207 #define Standard_STATIC_ASSERT(theExpr) Standard_Static_Assert<theExpr>::assert_ok();
0208 
0209 #endif // Standard_Assert_HeaderFile
0210 
0211 #ifdef _MSC_VER
0212 #pragma once
0213 #endif