Skyscraper 2.0
XrString.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
7#include <openxr/openxr.h>
8#include <openxr/openxr_platform.h>
9#include <openxr/openxr_reflection.h>
10
11#include <string>
12#include <vector>
13
14#include "XrToString.h"
15#include "XrError.h"
16
17namespace xr {
18
19 inline XrPath StringToPath(XrInstance instance, const char* str) {
20 XrPath path;
21 CHECK_XRCMD(xrStringToPath(instance, str, &path));
22 return path;
23 }
24
25 inline std::string PathToString(XrInstance instance, XrPath path) {
26 uint32_t count;
27 CHECK_XRCMD(xrPathToString(instance, path, 0, &count, nullptr));
28 std::string string;
29 string.resize(count);
30 CHECK_XRCMD(xrPathToString(instance, path, count, &count, string.data()));
31 // Remove the null character
32 string.resize(count - 1);
33 return string;
34 }
35
36 inline std::vector<XrPath> StringsToPaths(XrInstance instance, const std::vector<std::string>& strings) {
37 std::vector<XrPath> paths;
38
39 for (auto string : strings) {
40 paths.push_back(xr::StringToPath(instance, string.c_str()));
41 }
42
43 return paths;
44 }
45
46#ifdef _WIN32
47 inline std::wstring utf8_to_wide(std::string_view utf8Text) {
48 if (utf8Text.empty()) {
49 return {};
50 }
51
52 std::wstring wideText;
53 const int wideLength = ::MultiByteToWideChar(CP_UTF8, 0, utf8Text.data(), (int)utf8Text.size(), nullptr, 0);
54 if (wideLength == 0) {
55 DEBUG_PRINT("utf8_to_wide get size error.");
56 return {};
57 }
58
59 // MultiByteToWideChar returns number of chars of the input buffer, regardless of null terminitor
60 wideText.resize(wideLength, 0);
61 const int length = ::MultiByteToWideChar(CP_UTF8, 0, utf8Text.data(), (int)utf8Text.size(), wideText.data(), wideLength);
62 if (length != wideLength) {
63 DEBUG_PRINT("utf8_to_wide convert string error.");
64 return {};
65 }
66
67 return wideText;
68 }
69
70 inline std::string wide_to_utf8(std::wstring_view wideText) {
71 if (wideText.empty()) {
72 return {};
73 }
74
75 std::string narrowText;
76 int narrowLength = ::WideCharToMultiByte(CP_UTF8, 0, wideText.data(), (int)wideText.size(), nullptr, 0, nullptr, nullptr);
77 if (narrowLength == 0) {
78 DEBUG_PRINT("wide_to_utf8 get size error.");
79 return {};
80 }
81
82 // WideCharToMultiByte returns number of chars of the input buffer, regardless of null terminitor
83 narrowText.resize(narrowLength, 0);
84 const int length =
85 ::WideCharToMultiByte(CP_UTF8, 0, wideText.data(), (int)wideText.size(), narrowText.data(), narrowLength, nullptr, nullptr);
86 if (length != narrowLength) {
87 DEBUG_PRINT("wide_to_utf8 convert string error.");
88 return {};
89 }
90
91 return narrowText;
92 }
93#endif
94
95} // namespace xr
#define CHECK_XRCMD(cmd)
Definition XrError.h:14
#define DEBUG_PRINT(...)
Definition XrError.h:20
The xr::DispatchTable struct contains all available PFN pointers to xr functions including those in a...
std::vector< XrPath > StringsToPaths(XrInstance instance, const std::vector< std::string > &strings)
Definition XrString.h:36
std::string PathToString(XrInstance instance, XrPath path)
Definition XrString.h:25
XrPath StringToPath(XrInstance instance, const char *str)
Definition XrString.h:19