33#define ENUM_CASE_STR(name, val) case name: return #name;
34#define MAKE_TO_STRING_FUNC(enumType) \
35 inline const char* to_string(enumType e) { \
37 XR_LIST_ENUM_##enumType(ENUM_CASE_STR) \
38 default: return "Unknown " #enumType; \
43MAKE_TO_STRING_FUNC(XrReferenceSpaceType);
44MAKE_TO_STRING_FUNC(XrViewConfigurationType);
45MAKE_TO_STRING_FUNC(XrEnvironmentBlendMode);
46MAKE_TO_STRING_FUNC(XrSessionState);
47MAKE_TO_STRING_FUNC(XrResult);
48MAKE_TO_STRING_FUNC(XrFormFactor);
50inline std::string Fmt(
const char* fmt, ...) {
53 int size = std::vsnprintf(
nullptr, 0, fmt, vl);
57 std::unique_ptr<char[]> buffer(
new char[size + 1]);
60 size = std::vsnprintf(buffer.get(), size + 1, fmt, vl);
63 return std::string(buffer.get(), size);
67 throw std::runtime_error(
"Unexpected vsnprintf failure");
71#define CHK_STRINGIFY(x) #x
72#define TOSTRING(x) CHK_STRINGIFY(x)
73#define FILE_AND_LINE __FILE__ ":" TOSTRING(__LINE__)
75[[noreturn]]
inline void Throw(std::string failureMessage,
const char* originator =
nullptr,
const char* sourceLocation =
nullptr) {
76 if (originator !=
nullptr) {
77 failureMessage += Fmt(
"\n Origin: %s", originator);
79 if (sourceLocation !=
nullptr) {
80 failureMessage += Fmt(
"\n Source: %s", sourceLocation);
83 throw std::logic_error(failureMessage);
86#define THROW(msg) Throw(msg, nullptr, FILE_AND_LINE);
90 Throw("Check failed", #exp, FILE_AND_LINE); \
93#define CHECK_MSG(exp, msg) \
96 Throw(msg, #exp, FILE_AND_LINE); \
100[[noreturn]]
inline void ThrowXrResult(XrResult res,
const char* originator =
nullptr,
const char* sourceLocation =
nullptr) {
101 Throw(Fmt(
"XrResult failure [%s]", to_string(res)), originator, sourceLocation);
104inline XrResult CheckXrResult(XrResult res,
const char* originator =
nullptr,
const char* sourceLocation =
nullptr) {
105 if (XR_FAILED(res)) {
106 ThrowXrResult(res, originator, sourceLocation);
112#define THROW_XR(xr, cmd) ThrowXrResult(xr, #cmd, FILE_AND_LINE);
113#define CHECK_XRCMD(cmd) CheckXrResult(cmd, #cmd, FILE_AND_LINE);
114#define CHECK_XRRESULT(res, cmdStr) CheckXrResult(res, cmdStr, FILE_AND_LINE);
116inline std::vector<const char*> ParseExtensionString(
char* names) {
117 std::vector<const char*> list;
118 while (*names != 0) {
119 list.push_back(names);
120 while (*(++names) != 0) {
file that contains the common include for the Vulkan part