NDEVR
API Documentation
XRErrorHandling.h
1// Copyright (c) 2017-2022, The Khronos Group Inc.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6#include "DLLInfo.h"
7#include "Base/Headers/String.h"
8#include "Base/Headers/Exception.h"
9#include "Base/Headers/Translator.h"
10#include <openxr/openxr.h>
11
12#include <openxr/openxr_reflection.h>
13
14// Macro to generate stringify functions for OpenXR enumerations based data provided in openxr_reflection.h
15// clang-format off
16#define ENUM_CASE_STR(name, val) case name: return #name;
17#define MAKE_TO_STRING_FUNC(enumType) \
18 inline const char* to_string(enumType e) { \
19 switch (e) { \
20 XR_LIST_ENUM_##enumType(ENUM_CASE_STR) \
21 default: return "Unknown " #enumType; \
22 } \
23 }
24// clang-format on
25
26MAKE_TO_STRING_FUNC(XrReferenceSpaceType);
27MAKE_TO_STRING_FUNC(XrViewConfigurationType);
28MAKE_TO_STRING_FUNC(XrEnvironmentBlendMode);
29MAKE_TO_STRING_FUNC(XrSessionState);
30MAKE_TO_STRING_FUNC(XrResult);
31MAKE_TO_STRING_FUNC(XrFormFactor);
32
33#define CHK_STRINGIFY(x) #x
34#define TOSTRING(x) CHK_STRINGIFY(x)
35#define FILE_AND_LINE __FILE__ ":" TOSTRING(__LINE__)
36namespace NDEVR
37{
42 [[noreturn]] inline void Throw(StringView failure_message, const char* originator = nullptr, const char* sourceLocation = nullptr)
43 {
44 String message(failure_message);
45 if (originator != nullptr) {
46 message += StringView("\n Origin: ") + originator;
47 }
48 if (sourceLocation != nullptr) {
49 message += StringView("\n Source: ")+ sourceLocation;
50 }
51
52 throw Exception(_t("OpenXR Error"), TranslatedString(message));
53 }
54
55#define THROW(msg) Throw(msg, nullptr, FILE_AND_LINE);
56#define CHECK(exp) \
57 { \
58 if (!(exp)) { \
59 Throw("Check failed", #exp, FILE_AND_LINE); \
60 } \
61 }
62#define CHECK_MSG(exp, msg) \
63 { \
64 if (!(exp)) { \
65 Throw(msg, #exp, FILE_AND_LINE); \
66 } \
67 }
68
73 [[noreturn]] inline void ThrowXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
74 String s("XrResult failure ");
75 s += to_string(res);
76 Throw(s, originator, sourceLocation);
77 }
78
84 inline XrResult CheckXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
85 if (XR_FAILED(res)) {
86 ThrowXrResult(res, originator, sourceLocation);
87 }
88
89 return res;
90 }
91
92#define THROW_XR(xr, cmd) ThrowXrResult(xr, #cmd, FILE_AND_LINE);
93#define CHECK_XRCMD(cmd) CheckXrResult(cmd, #cmd, FILE_AND_LINE);
94#define CHECK_XRRESULT(res, cmdStr) CheckXrResult(res, cmdStr, FILE_AND_LINE);
95
96#ifdef XR_USE_PLATFORM_WIN32
97
98 [[noreturn]] inline void ThrowHResult(HRESULT hr, const char* originator = nullptr, const char* sourceLocation = nullptr) {
99 Throw(Fmt("HRESULT failure [%x]", hr), originator, sourceLocation);
100 }
101
102 inline HRESULT CheckHResult(HRESULT hr, const char* originator = nullptr, const char* sourceLocation = nullptr) {
103 if (FAILED(hr)) {
104 ThrowHResult(hr, originator, sourceLocation);
105 }
106
107 return hr;
108 }
109
110#define THROW_HR(hr, cmd) ThrowHResult(hr, #cmd, FILE_AND_LINE);
111#define CHECK_HRCMD(cmd) CheckHResult(cmd, #cmd, FILE_AND_LINE);
112#define CHECK_HRESULT(res, cmdStr) CheckHResult(res, cmdStr, FILE_AND_LINE);
113
114#endif
115}
Provides consistent interface to handle errors through the throw expression.
Definition Exception.hpp:47
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.
void Throw(StringView failure_message, const char *originator=nullptr, const char *sourceLocation=nullptr)
Throw an Exception with an OpenXR error message, optional originator, and source location.
void ThrowXrResult(XrResult res, const char *originator=nullptr, const char *sourceLocation=nullptr)
Throw an Exception for a failed XrResult.
XrResult CheckXrResult(XrResult res, const char *originator=nullptr, const char *sourceLocation=nullptr)
Check an XrResult and throw if it indicates failure.