0.2.0-dev Download ZIP
115 lines ·3.2 KB Raw
| 1 | // Copyright (c) 2017-2026 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 <rylie.pavlik@collabora.com> |
| 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, bool prefix = true) { |
| 25 | std::string out(bytes * 2 + (prefix ? 2 : 0), '?'); |
| 26 | if (prefix) { |
| 27 | out[0] = '0'; |
| 28 | out[1] = 'x'; |
| 29 | } |
| 30 | static const char* hex = "0123456789abcdef"; |
| 31 | auto ch = out.end(); |
| 32 | for (size_t i = 0; i < bytes; ++i) { |
| 33 | auto b = data[i]; |
| 34 | *--ch = hex[(b >> 0) & 0xf]; |
| 35 | *--ch = hex[(b >> 4) & 0xf]; |
| 36 | } |
| 37 | return out; |
| 38 | } |
| 39 | |
| 40 | template <typename T> |
| 41 | inline std::string to_hex(const T& data) { |
| 42 | return to_hex(reinterpret_cast<const uint8_t* const>(&data), sizeof(T)); |
| 43 | } |
| 44 | |
| 45 | template <typename T> |
| 46 | inline std::string to_hex(T* const data) { |
| 47 | return to_hex(reinterpret_cast<const uint8_t* const>(&data), sizeof(T*)); |
| 48 | } |
| 49 | |
| 50 | #if XR_PTR_SIZE == 8 |
| 51 | /// Convert a handle into a same-sized integer. |
| 52 | template <typename T> |
| 53 | static inline uint64_t MakeHandleGeneric(T handle) { |
| 54 | return reinterpret_cast<uint64_t>(handle); |
| 55 | } |
| 56 | |
| 57 | /// Treat an integer as a handle |
| 58 | template <typename T> |
| 59 | static inline T& TreatIntegerAsHandle(uint64_t& handle) { |
| 60 | return reinterpret_cast<T&>(handle); |
| 61 | } |
| 62 | |
| 63 | /// @overload |
| 64 | template <typename T> |
| 65 | static inline T const& TreatIntegerAsHandle(uint64_t const& handle) { |
| 66 | return reinterpret_cast<T const&>(handle); |
| 67 | } |
| 68 | |
| 69 | /// Does a correctly-sized integer represent a null handle? |
| 70 | static inline bool IsIntegerNullHandle(uint64_t handle) { return XR_NULL_HANDLE == reinterpret_cast<void*>(handle); } |
| 71 | |
| 72 | #else |
| 73 | |
| 74 | /// Convert a handle into a same-sized integer: no-op on 32-bit systems |
| 75 | static inline uint64_t MakeHandleGeneric(uint64_t handle) { return handle; } |
| 76 | |
| 77 | /// Treat an integer as a handle: no-op on 32-bit systems |
| 78 | template <typename T> |
| 79 | static inline T& TreatIntegerAsHandle(uint64_t& handle) { |
| 80 | return handle; |
| 81 | } |
| 82 | |
| 83 | /// @overload |
| 84 | template <typename T> |
| 85 | static inline T const& TreatIntegerAsHandle(uint64_t const& handle) { |
| 86 | return handle; |
| 87 | } |
| 88 | |
| 89 | /// Does a correctly-sized integer represent a null handle? |
| 90 | static inline bool IsIntegerNullHandle(uint64_t handle) { return XR_NULL_HANDLE == handle; } |
| 91 | |
| 92 | #endif |
| 93 | |
| 94 | /// Turns a uint64_t into a string formatted as hex. |
| 95 | /// |
| 96 | /// The core of the HandleToHexString implementation is in here. |
| 97 | inline std::string Uint64ToHexString(uint64_t val) { return to_hex(val); } |
| 98 | |
| 99 | /// Turns a uint32_t into a string formatted as hex. |
| 100 | inline std::string Uint32ToHexString(uint32_t val) { return to_hex(val); } |
| 101 | |
| 102 | /// Turns an OpenXR handle into a string formatted as hex. |
| 103 | template <typename T> |
| 104 | inline std::string HandleToHexString(T handle) { |
| 105 | return to_hex(handle); |
| 106 | } |
| 107 | |
| 108 | /// Turns a pointer-sized integer into a string formatted as hex. |
| 109 | inline std::string UintptrToHexString(uintptr_t val) { return to_hex(val); } |
| 110 | |
| 111 | /// Convert a pointer to a string formatted as hex. |
| 112 | template <typename T> |
| 113 | inline std::string PointerToHexString(T const* ptr) { |
| 114 | return to_hex(ptr); |
| 115 | } |