Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/openxr/src/common/hex_and_handles.h
22040 views
1
// Copyright (c) 2017-2025 The Khronos Group Inc.
2
// Copyright (c) 2017-2019 Valve Corporation
3
// Copyright (c) 2017-2019 LunarG, Inc.
4
// Copyright (c) 2019 Collabora, Ltd.
5
//
6
// SPDX-License-Identifier: Apache-2.0 OR MIT
7
//
8
// Initial Author: Rylie Pavlik <[email protected]>
9
//
10
11
/*!
12
* @file
13
*
14
* Some utilities, primarily for working with OpenXR handles in a generic way.
15
*/
16
17
#pragma once
18
19
#include <openxr/openxr.h>
20
21
#include <string>
22
#include <stdint.h>
23
24
inline std::string to_hex(const uint8_t* const data, size_t bytes) {
25
std::string out(2 + bytes * 2, '?');
26
out[0] = '0';
27
out[1] = 'x';
28
static const char* hex = "0123456789abcdef";
29
auto ch = out.end();
30
for (size_t i = 0; i < bytes; ++i) {
31
auto b = data[i];
32
*--ch = hex[(b >> 0) & 0xf];
33
*--ch = hex[(b >> 4) & 0xf];
34
}
35
return out;
36
}
37
38
template <typename T>
39
inline std::string to_hex(const T& data) {
40
return to_hex(reinterpret_cast<const uint8_t* const>(&data), sizeof(T));
41
}
42
43
template <typename T>
44
inline std::string to_hex(T* const data) {
45
return to_hex(reinterpret_cast<const uint8_t* const>(&data), sizeof(T*));
46
}
47
48
#if XR_PTR_SIZE == 8
49
/// Convert a handle into a same-sized integer.
50
template <typename T>
51
static inline uint64_t MakeHandleGeneric(T handle) {
52
return reinterpret_cast<uint64_t>(handle);
53
}
54
55
/// Treat an integer as a handle
56
template <typename T>
57
static inline T& TreatIntegerAsHandle(uint64_t& handle) {
58
return reinterpret_cast<T&>(handle);
59
}
60
61
/// @overload
62
template <typename T>
63
static inline T const& TreatIntegerAsHandle(uint64_t const& handle) {
64
return reinterpret_cast<T const&>(handle);
65
}
66
67
/// Does a correctly-sized integer represent a null handle?
68
static inline bool IsIntegerNullHandle(uint64_t handle) { return XR_NULL_HANDLE == reinterpret_cast<void*>(handle); }
69
70
#else
71
72
/// Convert a handle into a same-sized integer: no-op on 32-bit systems
73
static inline uint64_t MakeHandleGeneric(uint64_t handle) { return handle; }
74
75
/// Treat an integer as a handle: no-op on 32-bit systems
76
template <typename T>
77
static inline T& TreatIntegerAsHandle(uint64_t& handle) {
78
return handle;
79
}
80
81
/// @overload
82
template <typename T>
83
static inline T const& TreatIntegerAsHandle(uint64_t const& handle) {
84
return handle;
85
}
86
87
/// Does a correctly-sized integer represent a null handle?
88
static inline bool IsIntegerNullHandle(uint64_t handle) { return XR_NULL_HANDLE == handle; }
89
90
#endif
91
92
/// Turns a uint64_t into a string formatted as hex.
93
///
94
/// The core of the HandleToHexString implementation is in here.
95
inline std::string Uint64ToHexString(uint64_t val) { return to_hex(val); }
96
97
/// Turns a uint32_t into a string formatted as hex.
98
inline std::string Uint32ToHexString(uint32_t val) { return to_hex(val); }
99
100
/// Turns an OpenXR handle into a string formatted as hex.
101
template <typename T>
102
inline std::string HandleToHexString(T handle) {
103
return to_hex(handle);
104
}
105
106
/// Turns a pointer-sized integer into a string formatted as hex.
107
inline std::string UintptrToHexString(uintptr_t val) { return to_hex(val); }
108
109
/// Convert a pointer to a string formatted as hex.
110
template <typename T>
111
inline std::string PointerToHexString(T const* ptr) {
112
return to_hex(ptr);
113
}
114
115