-
Notifications
You must be signed in to change notification settings - Fork 257
/
check.h
72 lines (56 loc) · 2.49 KB
/
check.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2017-2024, The Khronos Group Inc.
//
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "common.h"
#define CHK_STRINGIFY(x) #x
#define TOSTRING(x) CHK_STRINGIFY(x)
#define FILE_AND_LINE __FILE__ ":" TOSTRING(__LINE__)
[[noreturn]] inline void Throw(std::string failureMessage, const char* originator = nullptr, const char* sourceLocation = nullptr) {
if (originator != nullptr) {
failureMessage += Fmt("\n Origin: %s", originator);
}
if (sourceLocation != nullptr) {
failureMessage += Fmt("\n Source: %s", sourceLocation);
}
throw std::logic_error(failureMessage);
}
#define THROW(msg) Throw(msg, nullptr, FILE_AND_LINE);
#define CHECK(exp) \
{ \
if (!(exp)) { \
Throw("Check failed", #exp, FILE_AND_LINE); \
} \
}
#define CHECK_MSG(exp, msg) \
{ \
if (!(exp)) { \
Throw(msg, #exp, FILE_AND_LINE); \
} \
}
[[noreturn]] inline void ThrowXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
Throw(Fmt("XrResult failure [%s]", to_string(res)), originator, sourceLocation);
}
inline XrResult CheckXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
if (XR_FAILED(res)) {
ThrowXrResult(res, originator, sourceLocation);
}
return res;
}
#define THROW_XR(xr, cmd) ThrowXrResult(xr, #cmd, FILE_AND_LINE);
#define CHECK_XRCMD(cmd) CheckXrResult(cmd, #cmd, FILE_AND_LINE);
#define CHECK_XRRESULT(res, cmdStr) CheckXrResult(res, cmdStr, FILE_AND_LINE);
#ifdef XR_USE_PLATFORM_WIN32
[[noreturn]] inline void ThrowHResult(HRESULT hr, const char* originator = nullptr, const char* sourceLocation = nullptr) {
Throw(Fmt("HRESULT failure [%x]", hr), originator, sourceLocation);
}
inline HRESULT CheckHResult(HRESULT hr, const char* originator = nullptr, const char* sourceLocation = nullptr) {
if (FAILED(hr)) {
ThrowHResult(hr, originator, sourceLocation);
}
return hr;
}
#define THROW_HR(hr, cmd) ThrowHResult(hr, #cmd, FILE_AND_LINE);
#define CHECK_HRCMD(cmd) CheckHResult(cmd, #cmd, FILE_AND_LINE);
#define CHECK_HRESULT(res, cmdStr) CheckHResult(res, cmdStr, FILE_AND_LINE);
#endif