Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
Assert.hpp
1#pragma once
2
3#include <Windows.h>
4#include <source_location>
5#include <format>
6#include <string>
7#include <iostream>
8
9#include <Common/Break.hpp>
10#include <Common/String.hpp>
11
12#ifdef _DEBUG
13 #define GS_ENABLE_ASSERTS
14#endif
15
17 template<typename... Args>
18 void AssertLog(
19 std::format_string<Args...> format,
20 const ::std::source_location& location = ::std::source_location::current(),
21 Args&&... args
22 ) {
23 const std::string message = std::format(
24 format,
25 ::std::forward<Args>(args)...
26 );
27
28 const std::string fullMessage = std::format(
29 "{}({}): Assertion Failed\n"
30 "Function: {}\n\n"
31 "{}",
32 location.file_name(),
33 location.line(),
34 location.function_name(),
35 message
36 );
37
38 std::cerr << fullMessage << std::endl;
39
40 const int wideSize = MultiByteToWideChar(
41 CP_UTF8,
42 0,
43 fullMessage.data(),
44 static_cast<int>(fullMessage.size()),
45 nullptr,
46 0
47 );
48
49 std::wstring wideMessage(wideSize, L'\0');
50
51 MultiByteToWideChar(
52 CP_UTF8,
53 0,
54 fullMessage.data(),
55 static_cast<int>(fullMessage.size()),
56 wideMessage.data(),
57 wideSize
58 );
59
60 MessageBoxW(
61 nullptr,
62 wideMessage.c_str(),
63 L"Assertion Failed",
64 MB_ICONEXCLAMATION | MB_OK
65 );
66 }
67}
68
69#ifdef GS_ENABLE_ASSERTS
70
71#define GS_ASSERT_LOG(msg, ...) \
72 ::Grindstone::Debug::AssertLog(msg, std::source_location::current() __VA_OPT__(,) __VA_ARGS__)
73
74#define GS_BREAK_WITH_MESSAGE(msg, ...) \
75 do { \
76 GS_ASSERT_LOG(msg __VA_OPT__(,) __VA_ARGS__); \
77 GS_DEBUG_BREAK; \
78 } while (false)
79
80#define GS_ASSERT_ENGINE_WITH_MESSAGE(condition, msg, ...) \
81 do { \
82 if (!(condition)) { \
83 GS_ASSERT_LOG(msg __VA_OPT__(,) __VA_ARGS__); \
84 GS_DEBUG_BREAK; \
85 } \
86 } while (false)
87
88#define GS_ASSERT_WITH_MESSAGE(condition, msg, ...) \
89 do { \
90 if (!(condition)) { \
91 GS_ASSERT_LOG(msg __VA_OPT__(,) __VA_ARGS__); \
92 GS_DEBUG_BREAK; \
93 } \
94 } while (false)
95
96#define GS_ASSERT_ENGINE(condition) \
97 do { \
98 if (!(condition)) { \
99 GS_ASSERT_LOG("Assertion failed: {}", #condition); \
100 GS_DEBUG_BREAK; \
101 } \
102 } while (false)
103
104#define GS_ASSERT(condition) \
105 do { \
106 if (!(condition)) { \
107 GS_ASSERT_LOG("Assertion failed: {}", #condition); \
108 GS_DEBUG_BREAK; \
109 } \
110 } while (false)
111
112#else
113
114#define GS_ASSERT_LOG(msg, ...)
115#define GS_BREAK_WITH_MESSAGE(msg, ...)
116#define GS_ASSERT_ENGINE_WITH_MESSAGE(condition, msg, ...)
117#define GS_ASSERT_WITH_MESSAGE(condition, msg, ...)
118#define GS_ASSERT_ENGINE(condition)
119#define GS_ASSERT(condition)
120
121#endif
Definition Assert.hpp:16