Path: blob/master/thirdparty/openxr/src/common/hex_and_handles.h
22040 views
// Copyright (c) 2017-2025 The Khronos Group Inc.1// Copyright (c) 2017-2019 Valve Corporation2// Copyright (c) 2017-2019 LunarG, Inc.3// Copyright (c) 2019 Collabora, Ltd.4//5// SPDX-License-Identifier: Apache-2.0 OR MIT6//7// Initial Author: Rylie Pavlik <[email protected]>8//910/*!11* @file12*13* Some utilities, primarily for working with OpenXR handles in a generic way.14*/1516#pragma once1718#include <openxr/openxr.h>1920#include <string>21#include <stdint.h>2223inline std::string to_hex(const uint8_t* const data, size_t bytes) {24std::string out(2 + bytes * 2, '?');25out[0] = '0';26out[1] = 'x';27static const char* hex = "0123456789abcdef";28auto ch = out.end();29for (size_t i = 0; i < bytes; ++i) {30auto b = data[i];31*--ch = hex[(b >> 0) & 0xf];32*--ch = hex[(b >> 4) & 0xf];33}34return out;35}3637template <typename T>38inline std::string to_hex(const T& data) {39return to_hex(reinterpret_cast<const uint8_t* const>(&data), sizeof(T));40}4142template <typename T>43inline std::string to_hex(T* const data) {44return to_hex(reinterpret_cast<const uint8_t* const>(&data), sizeof(T*));45}4647#if XR_PTR_SIZE == 848/// Convert a handle into a same-sized integer.49template <typename T>50static inline uint64_t MakeHandleGeneric(T handle) {51return reinterpret_cast<uint64_t>(handle);52}5354/// Treat an integer as a handle55template <typename T>56static inline T& TreatIntegerAsHandle(uint64_t& handle) {57return reinterpret_cast<T&>(handle);58}5960/// @overload61template <typename T>62static inline T const& TreatIntegerAsHandle(uint64_t const& handle) {63return reinterpret_cast<T const&>(handle);64}6566/// Does a correctly-sized integer represent a null handle?67static inline bool IsIntegerNullHandle(uint64_t handle) { return XR_NULL_HANDLE == reinterpret_cast<void*>(handle); }6869#else7071/// Convert a handle into a same-sized integer: no-op on 32-bit systems72static inline uint64_t MakeHandleGeneric(uint64_t handle) { return handle; }7374/// Treat an integer as a handle: no-op on 32-bit systems75template <typename T>76static inline T& TreatIntegerAsHandle(uint64_t& handle) {77return handle;78}7980/// @overload81template <typename T>82static inline T const& TreatIntegerAsHandle(uint64_t const& handle) {83return handle;84}8586/// Does a correctly-sized integer represent a null handle?87static inline bool IsIntegerNullHandle(uint64_t handle) { return XR_NULL_HANDLE == handle; }8889#endif9091/// Turns a uint64_t into a string formatted as hex.92///93/// The core of the HandleToHexString implementation is in here.94inline std::string Uint64ToHexString(uint64_t val) { return to_hex(val); }9596/// Turns a uint32_t into a string formatted as hex.97inline std::string Uint32ToHexString(uint32_t val) { return to_hex(val); }9899/// Turns an OpenXR handle into a string formatted as hex.100template <typename T>101inline std::string HandleToHexString(T handle) {102return to_hex(handle);103}104105/// Turns a pointer-sized integer into a string formatted as hex.106inline std::string UintptrToHexString(uintptr_t val) { return to_hex(val); }107108/// Convert a pointer to a string formatted as hex.109template <typename T>110inline std::string PointerToHexString(T const* ptr) {111return to_hex(ptr);112}113114115