Skyscraper 2.0
XrHandle.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 <cassert>
7
8namespace xr {
9
10 template <typename HandleType>
12 using PFN_DestroyFunction = XrResult(XRAPI_PTR*)(HandleType);
13
14 public:
15 UniqueXrHandle() = default;
17 UniqueXrHandle(UniqueXrHandle&& other) noexcept {
18 *this = std::move(other);
19 }
20
21 ~UniqueXrHandle() noexcept {
22 Reset();
23 }
24
27 if (m_handle != other.m_handle || m_destroyer != other.m_destroyer) {
28 Reset();
29
30 m_handle = other.m_handle;
31 m_destroyer = other.m_destroyer;
32
33 other.m_handle = XR_NULL_HANDLE;
34 other.m_destroyer = nullptr;
35 }
36 return *this;
37 }
38
39 bool operator==(const UniqueXrHandle& other) noexcept {
40 return m_handle == other.m_handle;
41 }
42
43 bool operator!=(const UniqueXrHandle& other) noexcept {
44 return m_handle != other.m_handle;
45 }
46
47 explicit operator bool() const noexcept {
48 return m_handle != XR_NULL_HANDLE;
49 }
50
51 HandleType Get() const noexcept {
52 return m_handle;
53 }
54
55 // Extension functions cannot be statically linked, so the creator must pass in the destroy function.
56 HandleType* Put(PFN_DestroyFunction destroyFunction) noexcept {
57 assert(destroyFunction != nullptr);
58 Reset();
59 m_destroyer = destroyFunction;
60 return &m_handle;
61 }
62
63 void Reset() noexcept {
64 if (m_handle != XR_NULL_HANDLE) {
66 m_handle = XR_NULL_HANDLE;
67 }
68
69 m_destroyer = nullptr;
70 }
71
72 private:
73 HandleType m_handle{XR_NULL_HANDLE};
75 };
76
77 class ActionHandle : public UniqueXrHandle<XrAction> {};
78 class ActionSetHandle : public UniqueXrHandle<XrActionSet> {};
79 class InstanceHandle : public UniqueXrHandle<XrInstance> {};
80 class SessionHandle : public UniqueXrHandle<XrSession> {};
81 class SpaceHandle : public UniqueXrHandle<XrSpace> {};
82 class SwapchainHandle : public UniqueXrHandle<XrSwapchain> {};
83 class SpatialAnchorHandle : public UniqueXrHandle<XrSpatialAnchorMSFT> {};
84 class HandTrackerHandle : public UniqueXrHandle<XrHandTrackerEXT> {};
85
86} // namespace xr
XrResult(XRAPI_PTR *)(HandleType) PFN_DestroyFunction
Definition XrHandle.h:12
UniqueXrHandle(UniqueXrHandle &&other) noexcept
Definition XrHandle.h:17
bool operator!=(const UniqueXrHandle &other) noexcept
Definition XrHandle.h:43
HandleType m_handle
Definition XrHandle.h:73
UniqueXrHandle & operator=(const UniqueXrHandle &)=delete
void Reset() noexcept
Definition XrHandle.h:63
~UniqueXrHandle() noexcept
Definition XrHandle.h:21
PFN_DestroyFunction m_destroyer
Definition XrHandle.h:74
HandleType Get() const noexcept
Definition XrHandle.h:51
HandleType * Put(PFN_DestroyFunction destroyFunction) noexcept
Definition XrHandle.h:56
UniqueXrHandle & operator=(UniqueXrHandle &&other) noexcept
Definition XrHandle.h:26
bool operator==(const UniqueXrHandle &other) noexcept
Definition XrHandle.h:39
UniqueXrHandle(const UniqueXrHandle &)=delete
UniqueXrHandle()=default
The xr::DispatchTable struct contains all available PFN pointers to xr functions including those in a...