Skyscraper 2.0
XrError.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include <memory>
7#include <stdexcept>
8#include <stdarg.h>
9#include "XrToString.h"
10#ifdef _WIN32
11#include <windows.h>
12#endif
13
14#define CHECK_XRCMD(cmd) xr::detail::_CheckXrResult(cmd, #cmd, FILE_AND_LINE)
15#define CHECK_XRRESULT(res, cmdStr) xr::detail::_CheckXrResult(res, cmdStr, FILE_AND_LINE)
16
17#define CHECK_HRCMD(cmd) xr::detail::_CheckHResult(cmd, #cmd, FILE_AND_LINE)
18#define CHECK_HRESULT(res, cmdStr) xr::detail::_CheckHResult(res, cmdStr, FILE_AND_LINE)
19
20#define DEBUG_PRINT(...) ::OutputDebugStringA((xr::detail::_Fmt(__VA_ARGS__) + "\n").c_str())
21
22namespace xr::detail {
23#define CHK_STRINGIFY(x) #x
24#define TOSTRING(x) CHK_STRINGIFY(x)
25#define FILE_AND_LINE __FILE__ ":" TOSTRING(__LINE__)
26
27 inline std::string _Fmt(const char* fmt, ...) {
28 va_list vl;
29 va_start(vl, fmt);
30 int size = std::vsnprintf(nullptr, 0, fmt, vl);
31 va_end(vl);
32
33 if (size != -1) {
34 std::unique_ptr<char[]> buffer(new char[size + 1]);
35
36 va_start(vl, fmt);
37 size = std::vsnprintf(buffer.get(), size + 1, fmt, vl);
38 va_end(vl);
39 if (size != -1) {
40 return std::string(buffer.get(), size);
41 }
42 }
43
44 throw std::runtime_error("Unexpected vsnprintf failure");
45 }
46
47 [[noreturn]] inline void _Throw(std::string failureMessage, const char* originator = nullptr, const char* sourceLocation = nullptr) {
48 if (originator != nullptr) {
49 failureMessage += _Fmt("\n Origin: %s", originator);
50 }
51 if (sourceLocation != nullptr) {
52 failureMessage += _Fmt("\n Source: %s", sourceLocation);
53 }
54
55 throw std::logic_error(failureMessage);
56 }
57
58#define THROW(msg) xr::detail::_Throw(msg, nullptr, FILE_AND_LINE)
59#define CHECK(exp) \
60 { \
61 if (!(exp)) { \
62 xr::detail::_Throw("Check failed", #exp, FILE_AND_LINE); \
63 } \
64 }
65#define CHECK_MSG(exp, msg) \
66 { \
67 if (!(exp)) { \
68 xr::detail::_Throw(msg, #exp, FILE_AND_LINE); \
69 } \
70 }
71
72 [[noreturn]] inline void _ThrowXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
73 xr::detail::_Throw(_Fmt("XrResult failure [%s]", xr::ToCString(res)), originator, sourceLocation);
74 }
75
76 inline XrResult _CheckXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
77 if (XR_FAILED(res)) {
78 xr::detail::_ThrowXrResult(res, originator, sourceLocation);
79 }
80
81 return res;
82 }
83
84#ifdef _WIN32
85 [[noreturn]] inline void _ThrowHResult(HRESULT hr, const char* originator = nullptr, const char* sourceLocation = nullptr) {
86 xr::detail::_Throw(xr::detail::_Fmt("HRESULT failure [%x]", hr), originator, sourceLocation);
87 }
88
89 inline HRESULT _CheckHResult(HRESULT hr, const char* originator = nullptr, const char* sourceLocation = nullptr) {
90 if (FAILED(hr)) {
91 xr::detail::_ThrowHResult(hr, originator, sourceLocation);
92 }
93
94 return hr;
95 }
96#endif
97} // namespace xr::detail
void _ThrowXrResult(XrResult res, const char *originator=nullptr, const char *sourceLocation=nullptr)
Definition XrError.h:72
XrResult _CheckXrResult(XrResult res, const char *originator=nullptr, const char *sourceLocation=nullptr)
Definition XrError.h:76
void _Throw(std::string failureMessage, const char *originator=nullptr, const char *sourceLocation=nullptr)
Definition XrError.h:47
std::string _Fmt(const char *fmt,...)
Definition XrError.h:27