stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
assert.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#ifdef _WIN32
10#include "windows.h"
11#endif
12#include <assert.h>
13#include <stdint.h>
14#include <stdlib.h>
15
16#ifdef NDEBUG
17#define stdex_assert(e) _Analysis_assume_(e)
18#define stdex_verify(e) ((void)(e))
19#else
20#if defined(_WIN32)
21#define stdex_assert(e) (!!(e) ? (void)0 : stdex::do_assert(_L(__FILE__), (unsigned)(__LINE__), _L(#e)))
22#elif defined(__APPLE__)
23#define stdex_assert(e) (!!(e) ? (void)0 : stdex::do_assert(__func__, __ASSERT_FILE_NAME, __LINE__, #e))
24#else
25#error Implement!
26#endif
27#define stdex_verify(e) stdex_assert(e)
28#endif
29
30namespace stdex
31{
33#if defined(_WIN32)
34 inline void do_assert(const wchar_t* file, unsigned line, const wchar_t* expression)
35 {
36 // Non-interactive processes (NT services, ISAPI and ActiveX DLLs running in IIS etc.)
37 // MUST NOT raise asserts. It'd block the process, and process host (SCM, IIS) would
38 // continue to see the process as alive but non-responding, preventing recovery.
39 // RaiseException instead to have the process terminated and possibly trigger Windows
40 // Error Reporting or AHroščar.
41 // For interactive processes, it is more convenient to alert the user looking at the
42 // desktop right now. Maybe it is the developer and debugging the very process is
43 // possible?
44 HWINSTA hWinSta = GetProcessWindowStation();
45 if (hWinSta) {
46 WCHAR sName[MAX_PATH];
47 if (GetUserObjectInformationW(hWinSta, UOI_NAME, sName, sizeof(sName), NULL)) {
48 sName[_countof(sName) - 1] = 0;
49 // Only "WinSta0" is interactive (Source: KB171890)
50 if (_wcsicmp(sName, L"WinSta0") == 0) {
51 _wassert(expression, file, line);
52 return;
53 }
54 }
55 }
56 RaiseException(STATUS_ASSERTION_FAILURE, EXCEPTION_NONCONTINUABLE, 0, NULL);
57 }
58#elif defined(__APPLE__)
59 inline void do_assert(const char* function, const char* file, int line, const char* expression)
60 {
61 __assert_rtn(function, file, line, expression);
62 }
63#else
64#error Implement!
65#endif
67}