Skyscraper 2.0
XrUuid.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include <functional>
7#include <openxr/openxr.h>
8
9inline bool operator==(const XrUuidMSFT& lh, const XrUuidMSFT& rh) noexcept {
10 return memcmp(&rh, &lh, sizeof(XrUuidMSFT)) == 0;
11}
12
13inline bool operator!=(const XrUuidMSFT& lh, const XrUuidMSFT& rh) noexcept {
14 return !(lh == rh);
15}
16
17inline bool operator<(const XrUuidMSFT& lh, const XrUuidMSFT& rh) noexcept {
18 return memcmp(&lh, &rh, sizeof(XrUuidMSFT)) < 0;
19}
20
21namespace xr {
22 // Type safe UUID for ensuring that two different types of UUIDs do not get mixed.
23 // Example:
24 // XrUuidMSFT uuid {};
25 // TypedUuid<Plane> planeId(uuid); // allowed
26 // TypedUuid<Mesh> meshId{planeId}; // compile error
27 // meshId = planeId; // compile error
28 // meshId = uuid; // allowed
29 // uuid = meshId; // compiler error
30 // uuid = static_cast<XrUuidMSFT>(meshId); // allowed
31 // if (meshId < meshId_2) {} // allowed
32 // if (meshId != planeId) {} // compiler error
33 // To access the uint8_t[16] array in TypedUuid, use
34 // static_cast<XrUuidMSFT>(meshId).bytes
35 //
36 template <typename Type>
37 struct TypedUuid {
38 TypedUuid() noexcept = default;
39 explicit TypedUuid(const XrUuidMSFT& uuid) noexcept
40 : m_uuid(uuid) {
41 }
42
43 TypedUuid& operator=(const XrUuidMSFT& uuid) noexcept {
44 m_uuid = uuid;
45 return *this;
46 }
47
48 explicit operator XrUuidMSFT() const noexcept {
49 return m_uuid;
50 }
51
52 bool operator==(const TypedUuid& other) const noexcept {
53 return m_uuid == other.m_uuid;
54 }
55
56 bool operator!=(const TypedUuid& other) const noexcept {
57 return m_uuid != other.m_uuid;
58 }
59
60 bool operator<(const TypedUuid& other) const noexcept {
61 return m_uuid < other.m_uuid;
62 }
63
64 private:
65 XrUuidMSFT m_uuid;
66 };
67
68} // namespace xr
69
70namespace std {
71 // This template specialization allows XrUuidMSFT to be used as the key in a std::unordered_map.
72 template <>
73 struct hash<XrUuidMSFT> {
74 std::size_t operator()(const XrUuidMSFT& uuid) const noexcept {
75 static_assert(sizeof(XrUuidMSFT) == sizeof(uint64_t) * 2);
76 const uint64_t* v = reinterpret_cast<const uint64_t*>(uuid.bytes);
77 return std::hash<uint64_t>{}(v[0]) ^ (std::hash<uint64_t>{}(v[1]) << 1);
78 }
79 };
80
81 // This template specialization allows TypedUuid to be used as the key in a std::unordered_map.
82 template <typename T>
83 struct hash<xr::TypedUuid<T>> {
84 std::size_t operator()(const xr::TypedUuid<T>& uuid) const noexcept {
85 return std::hash<XrUuidMSFT>{}(static_cast<XrUuidMSFT>(uuid));
86 }
87 };
88} // namespace std
bool operator<(const XrUuidMSFT &lh, const XrUuidMSFT &rh) noexcept
Definition XrUuid.h:17
bool operator!=(const XrUuidMSFT &lh, const XrUuidMSFT &rh) noexcept
Definition XrUuid.h:13
bool operator==(const XrUuidMSFT &lh, const XrUuidMSFT &rh) noexcept
Definition XrUuid.h:9
Definition XrUuid.h:70
The xr::DispatchTable struct contains all available PFN pointers to xr functions including those in a...
std::size_t operator()(const XrUuidMSFT &uuid) const noexcept
Definition XrUuid.h:74
std::size_t operator()(const xr::TypedUuid< T > &uuid) const noexcept
Definition XrUuid.h:84
bool operator<(const TypedUuid &other) const noexcept
Definition XrUuid.h:60
bool operator!=(const TypedUuid &other) const noexcept
Definition XrUuid.h:56
TypedUuid() noexcept=default
XrUuidMSFT m_uuid
Definition XrUuid.h:65
bool operator==(const TypedUuid &other) const noexcept
Definition XrUuid.h:52
TypedUuid & operator=(const XrUuidMSFT &uuid) noexcept
Definition XrUuid.h:43