Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cDebugAssert.h
Go to the documentation of this file.
1 //
5 //
6 #ifndef _INC_cDebugAssert_H
7 #define _INC_cDebugAssert_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "GrayCore.h"
13 
14 namespace Gray
15 {
18 #ifndef STATIC_ASSERT
19 #if defined(_MSC_VER) && ( _MSC_VER >= 1600 )
20 #define STATIC_ASSERT(exp, name) static_assert(exp,#name)
21 #elif defined(__GNUC__)
22 #define STATIC_ASSERT(exp, name) static_assert(exp,#name) // (since C++11)
23 #else
24 #define STATIC_ASSERT(exp, name) typedef int static_assert_##name [(exp) ? 1 : -1] // This should fail in the compiler. __GNUC__ cant handle this ?
25 #endif
26 #endif
27 
29  {
33  public:
34  const char* m_pszFile;
35  const char* m_pszFunction; // __func__, __FUNCTION__, __FUNCDNAME__, and __FUNCSIG__
36  WORD m_uLine;
37 
38  public:
39  cDebugSourceLine(const char* pszFile = "", const char* pszFunction = "", WORD uLine = 0) noexcept
40  : m_pszFile(pszFile)
41  , m_pszFunction(pszFunction)
42  , m_uLine(uLine)
43  {
44  }
45  };
46 
49 #define DEBUGSOURCELINE ::Gray::cDebugSourceLine( __FILE__, __FUNCTION__, (WORD) __LINE__ )
50 
51  typedef bool (CALLBACK AssertCallback_t)(const char* pszExp, const cDebugSourceLine& src); // for redirect of assert in testing.
52 
53  struct GRAYCORE_LINK cDebugAssert // static
54  {
58 
59  static AssertCallback_t* sm_pAssertCallback;
60  static bool sm_bAssertTest;
61 
62  static bool CALLBACK Assert_System(const char* pszExp, const cDebugSourceLine& src);
63  static bool GRAYCALL Assert_Fail(const char* pszExp, const cDebugSourceLine src);
64  static CATTR_NORETURN void GRAYCALL Assert_Throw(const char* pszExp, const cDebugSourceLine src);
65  static bool GRAYCALL Debug_Fail(const char* pszExp, const cDebugSourceLine src) noexcept;
66  };
67 
68 #define ASSERT_THROW(exp) if (!(exp)) { ::Gray::cDebugAssert::Assert_Throw(#exp, DEBUGSOURCELINE ); } // Show the compiler that we wont proceed.
69 
70 #define ASSERT_N(exp) ASSERT_THROW(exp) // Null check, Cant ignore this !
71 
72 #if defined(_DEBUG) || defined(_DEBUG_FAST)
73  // debug checks
74  // Use macros so these can be compiled out.
75  // overload the assert statements
76 #undef ASSERT
77 #define ASSERT(exp) (void)((!!(exp)) || (::Gray::cDebugAssert::Assert_Fail(#exp, DEBUGSOURCELINE ), 0))
78 #undef DEBUG_CHECK
79 #define DEBUG_CHECK(exp) (void)((!!(exp)) || (::Gray::cDebugAssert::Debug_Fail(#exp, DEBUGSOURCELINE ), 0))
80 #undef DEBUG_ASSERT
81 #define DEBUG_ASSERT(exp,sDesc) (void)((!!(exp)) || (::Gray::cDebugAssert::Debug_Fail(sDesc, DEBUGSOURCELINE ), 0))
82 
83 #else // _DEBUG
84 
85  // NON DEBUG compiles out the checks.
86 #ifndef ASSERT
87 #define ASSERT(exp) __noop // acts like UNREFERENCED_PARAMETER() ?
88 #endif // ASSERT
89 #ifndef DEBUG_CHECK
90 #define DEBUG_CHECK(exp) __noop
91 #endif // DEBUG_CHECK
92 #ifndef DEBUG_ASSERT
93 #define DEBUG_ASSERT(exp,sDesc) __noop
94 #endif // DEBUG_ASSERT
95 
96 #endif // ! _DEBUG
97 
98 };
99 
100 #endif // _INC_cDebugAssert_H
#define CATTR_NORETURN
Definition: GrayCore.h:93
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
const cDebugSourceLine & src
Definition: cDebugAssert.h:51
typedef bool(CALLBACK AssertCallback_t)(const char *pszExp
Definition: cDebugAssert.h:54
static AssertCallback_t * sm_pAssertCallback
redirect callback on Assert_Fail usually used for unit tests.
Definition: cDebugAssert.h:59
static bool sm_bAssertTest
Just testing. not a real assert.
Definition: cDebugAssert.h:60
Definition: cDebugAssert.h:29
WORD m_uLine
line number in the source file. (1 based) LINE
Definition: cDebugAssert.h:36
cDebugSourceLine(const char *pszFile="", const char *pszFunction="", WORD uLine=0) noexcept
Definition: cDebugAssert.h:39
const char * m_pszFile
name of the source file. static text. FILE
Definition: cDebugAssert.h:34
const char * m_pszFunction
Definition: cDebugAssert.h:35