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); ((void)0); }
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
29 namespace stdex
30{
32#if defined(_WIN32)
33 inline void do_assert(const wchar_t* file, unsigned line, const wchar_t* expression)
34 {
35 // Non-interactive processes (NT services, ISAPI and ActiveX DLLs running in IIS etc.)
36 // MUST NOT raise asserts. It'd block the process, and process host (SCM, IIS) would
37 // continue to see the process as alive but non-responding, preventing recovery.
38 // RaiseException instead to have the process terminated and possibly trigger Windows
39 // Error Reporting or AHroščar.
40 // For interactive processes, it is more convenient to alert the user looking at the
41 // desktop right now. Maybe it is the developer and debugging the very process is
42 // possible?
43 HWINSTA hWinSta = GetProcessWindowStation();
44 if (hWinSta) {
45 WCHAR sName[MAX_PATH];
46 if (GetUserObjectInformationW(hWinSta, UOI_NAME, sName, sizeof(sName), NULL)) {
47 sName[_countof(sName) - 1] = 0;
48 // Only "WinSta0" is interactive (Source: KB171890)
49 if (_wcsicmp(sName, L"WinSta0") == 0) {
50 _wassert(expression, file, line);
51 return;
52 }
53 }
54 }
55 RaiseException(STATUS_ASSERTION_FAILURE, EXCEPTION_NONCONTINUABLE, 0, NULL);
56 }
57#elif defined(__APPLE__)
58 inline void do_assert(const char* function, const char* file, int line, const char* expression)
59 {
60 __assert_rtn(function, file, line, expression);
61 }
62#else
63#error Implement!
64#endif
66}
67#endif