Path: blob/master/thirdparty/openxr/src/common/hex_and_handles.h
9913 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(data));40}4142#if XR_PTR_SIZE == 843/// Convert a handle into a same-sized integer.44template <typename T>45static inline uint64_t MakeHandleGeneric(T handle) {46return reinterpret_cast<uint64_t>(handle);47}4849/// Treat an integer as a handle50template <typename T>51static inline T& TreatIntegerAsHandle(uint64_t& handle) {52return reinterpret_cast<T&>(handle);53}5455/// @overload56template <typename T>57static inline T const& TreatIntegerAsHandle(uint64_t const& handle) {58return reinterpret_cast<T const&>(handle);59}6061/// Does a correctly-sized integer represent a null handle?62static inline bool IsIntegerNullHandle(uint64_t handle) { return XR_NULL_HANDLE == reinterpret_cast<void*>(handle); }6364#else6566/// Convert a handle into a same-sized integer: no-op on 32-bit systems67static inline uint64_t MakeHandleGeneric(uint64_t handle) { return handle; }6869/// Treat an integer as a handle: no-op on 32-bit systems70template <typename T>71static inline T& TreatIntegerAsHandle(uint64_t& handle) {72return handle;73}7475/// @overload76template <typename T>77static inline T const& TreatIntegerAsHandle(uint64_t const& handle) {78return handle;79}8081/// Does a correctly-sized integer represent a null handle?82static inline bool IsIntegerNullHandle(uint64_t handle) { return XR_NULL_HANDLE == handle; }8384#endif8586/// Turns a uint64_t into a string formatted as hex.87///88/// The core of the HandleToHexString implementation is in here.89inline std::string Uint64ToHexString(uint64_t val) { return to_hex(val); }9091/// Turns a uint32_t into a string formatted as hex.92inline std::string Uint32ToHexString(uint32_t val) { return to_hex(val); }9394/// Turns an OpenXR handle into a string formatted as hex.95template <typename T>96inline std::string HandleToHexString(T handle) {97return to_hex(handle);98}99100/// Turns a pointer-sized integer into a string formatted as hex.101inline std::string UintptrToHexString(uintptr_t val) { return to_hex(val); }102103/// Convert a pointer to a string formatted as hex.104template <typename T>105inline std::string PointerToHexString(T const* ptr) {106return to_hex(ptr);107}108109110