API Documentation
Loading...
Searching...
No Matches
XRErrorHandling.h
Go to the documentation of this file.
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"
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);
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{
38 [[noreturn]] inline void Throw(String failureMessage, const char* originator = nullptr, const char* sourceLocation = nullptr) {
39 if (originator != nullptr) {
40 failureMessage += String("\n Origin: ") + originator;
41 }
42 if (sourceLocation != nullptr) {
43 failureMessage += String("Source: ")+ sourceLocation;
44 }
45
46 throw Exception(_t("OpenXR Error"), TranslatedString(failureMessage));
47 }
48
49#define THROW(msg) Throw(msg, nullptr, FILE_AND_LINE);
50#define CHECK(exp) \
51 { \
52 if (!(exp)) { \
53 Throw("Check failed", #exp, FILE_AND_LINE); \
54 } \
55 }
56#define CHECK_MSG(exp, msg) \
57 { \
58 if (!(exp)) { \
59 Throw(msg, #exp, FILE_AND_LINE); \
60 } \
61 }
62
63 [[noreturn]] inline void ThrowXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
64 String s("XrResult failure ");
65 s += to_string(res);
66 Throw(s, originator, sourceLocation);
67 }
68
69 inline XrResult CheckXrResult(XrResult res, const char* originator = nullptr, const char* sourceLocation = nullptr) {
70 if (XR_FAILED(res)) {
71 ThrowXrResult(res, originator, sourceLocation);
72 }
73
74 return res;
75 }
76
77#define THROW_XR(xr, cmd) ThrowXrResult(xr, #cmd, FILE_AND_LINE);
78#define CHECK_XRCMD(cmd) CheckXrResult(cmd, #cmd, FILE_AND_LINE);
79#define CHECK_XRRESULT(res, cmdStr) CheckXrResult(res, cmdStr, FILE_AND_LINE);
80
81#ifdef XR_USE_PLATFORM_WIN32
82
83 [[noreturn]] inline void ThrowHResult(HRESULT hr, const char* originator = nullptr, const char* sourceLocation = nullptr) {
84 Throw(Fmt("HRESULT failure [%x]", hr), originator, sourceLocation);
85 }
86
87 inline HRESULT CheckHResult(HRESULT hr, const char* originator = nullptr, const char* sourceLocation = nullptr) {
88 if (FAILED(hr)) {
89 ThrowHResult(hr, originator, sourceLocation);
90 }
91
92 return hr;
93 }
94
95#define THROW_HR(hr, cmd) ThrowHResult(hr, #cmd, FILE_AND_LINE);
96#define CHECK_HRCMD(cmd) CheckHResult(cmd, #cmd, FILE_AND_LINE);
97#define CHECK_HRESULT(res, cmdStr) CheckHResult(res, cmdStr, FILE_AND_LINE);
98
99#endif
100}
#define _t(english_string)
Definition Translator.h:90
#define MAKE_TO_STRING_FUNC(enumType)
Definition XRErrorHandling.h:17
const char * to_string(XrReferenceSpaceType e)
Definition XRErrorHandling.h:26
Provides consistent interface to handle errors through the throw expression. All exceptions generated...
Definition Exception.hpp:47
The core String class for the NDEVR API.
Definition String.h:69
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
Definition TranslatedString.h:13
Definition ACIColor.h:37
XrResult CheckXrResult(XrResult res, const char *originator=nullptr, const char *sourceLocation=nullptr)
Definition XRErrorHandling.h:69
void Throw(String failureMessage, const char *originator=nullptr, const char *sourceLocation=nullptr)
Definition XRErrorHandling.h:38
void ThrowXrResult(XrResult res, const char *originator=nullptr, const char *sourceLocation=nullptr)
Definition XRErrorHandling.h:63