|
||||
File indexing completed on 2025-01-18 10:04:58
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 // use debug CRT built-in function that show up message box to user 0095 // with formatted assert description and 3 possible actions 0096 inline Standard_Boolean Standard_ASSERT_REPORT_ (const char* theFile, 0097 const int theLine, 0098 const char* theExpr, 0099 const char* theDesc) 0100 { 0101 // 1 means user pressed Retry button 0102 return _CrtDbgReport (_CRT_ASSERT, theFile, theLine, NULL, 0103 "%s\n(Condition: \"%s\")\n", theDesc, theExpr) == 1; 0104 } 0105 #else 0106 // just log assertion description into standard error stream 0107 inline Standard_Boolean Standard_ASSERT_REPORT_ (const char* theFile, 0108 const int theLine, 0109 const char* theExpr, 0110 const char* theDesc) 0111 { 0112 std::cerr << "ERROR: statement '" << theExpr << "' is not TRUE!\n" 0113 << "\nFile: '" << theFile << "'" 0114 << "\nLine: " << theLine << "\n"; 0115 if (theDesc != NULL && *theDesc != '\0') 0116 std::cerr << "Description: " << theDesc << "\n"; 0117 0118 std::cerr << std::flush; 0119 return Standard_True; 0120 } 0121 #endif 0122 0123 // report issue and add debug breakpoint or abort execution 0124 #define Standard_ASSERT_INVOKE_(theExpr, theDesc) \ 0125 if (Standard_ASSERT_REPORT_ (__FILE__, __LINE__, #theExpr, theDesc)) { Standard_ASSERT_DBGBREAK_(); } \ 0126 else Standard_ASSERT_DO_NOTHING() 0127 0128 // Basic ASSERT macros 0129 #define Standard_ASSERT(theExpr, theDesc, theAction) \ 0130 if (!(theExpr)) { Standard_ASSERT_INVOKE_(theExpr, theDesc); theAction; } \ 0131 else Standard_ASSERT_DO_NOTHING() 0132 #define Standard_ASSERT_SKIP(theExpr, theDesc) \ 0133 Standard_ASSERT(theExpr, theDesc,) 0134 #define Standard_ASSERT_VOID(theExpr, theDesc) \ 0135 Standard_ASSERT(theExpr, theDesc,) 0136 #else 0137 0138 // dummy block 0139 #define Standard_ASSERT_INVOKE_(theExpr, theDesc) Standard_ASSERT_DO_NOTHING() 0140 0141 // Basic ASSERT macros 0142 #define Standard_ASSERT(theExpr, theDesc, theAction) \ 0143 if (!(theExpr)) { theAction; } \ 0144 else Standard_ASSERT_DO_NOTHING() 0145 #define Standard_ASSERT_SKIP(theExpr, theDesc) theExpr 0146 #define Standard_ASSERT_VOID(theExpr, theDesc) Standard_ASSERT_DO_NOTHING() 0147 0148 #endif 0149 0150 //! Raise exception (Standard_ProgramError) with the provided message 0151 #define Standard_ASSERT_RAISE(theExpr, theDesc) \ 0152 Standard_ASSERT(theExpr, theDesc, throw Standard_ProgramError( \ 0153 "*** ERROR: ASSERT in file '" __FILE__ "': \n" theDesc " (" #theExpr ")" ) ) 0154 0155 //! Return from the current function with specified value (empty 0156 //! if the function returns void) 0157 #define Standard_ASSERT_RETURN(theExpr, theDesc, theReturnValue) \ 0158 Standard_ASSERT(theExpr, theDesc, return theReturnValue) 0159 0160 //! Raise debug message 0161 #define Standard_ASSERT_INVOKE(theDesc) Standard_ASSERT_INVOKE_(always, theDesc) 0162 0163 //! Static assert -- 0164 //! empty default template 0165 template <bool condition> 0166 struct Standard_Static_Assert { }; 0167 0168 //! Static assert -- specialization for condition being true 0169 template <> 0170 struct Standard_Static_Assert<true> 0171 { 0172 static void assert_ok() {} 0173 }; 0174 0175 //! Cause compiler error if argument is not constant expression or 0176 //! evaluates to false 0177 #define Standard_STATIC_ASSERT(theExpr) \ 0178 Standard_Static_Assert<theExpr>::assert_ok(); 0179 0180 #endif // Standard_Assert_HeaderFile 0181 0182 #ifdef _MSC_VER 0183 #pragma once 0184 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |