XRTraits C++ OpenXR Utilities
UniqueHandles.h
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Header defining unique_ptr-based RAII types for OpenXR handles.
6  * @author Ryan Pavlik <ryan.pavlik@collabora.com>
7  */
8 
9 #pragma once
10 
11 // Internal Includes
12 // - none
13 
14 // Library Includes
15 #include <openxr/openxr.h>
16 
17 // Standard Includes
18 #include <memory>
19 #include <type_traits>
20 
21 namespace xrtraits {
22 
23 #ifdef XRTRAITS_DOXYGEN
24 namespace handle_destroyers {
25  /*! Placeholder for the generated, per-handle-type deleters for OpenXR
26  * handles.
27  */
28  class OpenXRHandleDestroyer;
29 } // namespace handle_destroyers
30 
31 /*!
32  * @defgroup RAIIHandles RAII Handle Wrappers
33  *
34  * @brief Types performing RAII-style automatic destruction of handles.
35  *
36  * These are currently using std::unique_ptr for their implementation, which
37  * does mean that they won't work on 32-bit platforms (where handles are 64-bit
38  * integers).
39  *
40  * @todo Make a custom class template to replace the use of std::unique_ptr for
41  * 32-bit compatibility.
42  */
43 //! @{
44 
45 //! Scoped RAII pointer type wrapping XrInstance
46 using UniqueInstance =
47  std::unique_ptr<std::remove_pointer_t<XrInstance>,
48  handle_destroyers::OpenXRHandleDestroyer>;
49 
50 //! Scoped RAII pointer type wrapping XrSession
51 using UniqueSession = std::unique_ptr<std::remove_pointer_t<XrSession>,
52  handle_destroyers::OpenXRHandleDestroyer>;
53 
54 //! Scoped RAII pointer type wrapping XrSwapchain
55 using UniqueSwapchain =
56  std::unique_ptr<std::remove_pointer_t<XrSwapchain>,
57  handle_destroyers::OpenXRHandleDestroyer>;
58 
59 //! Scoped RAII pointer type wrapping XrAction
60 using UniqueAction = std::unique_ptr<std::remove_pointer_t<XrAction>,
61  handle_destroyers::OpenXRHandleDestroyer>;
62 
63 //! Scoped RAII pointer type wrapping XrActionSet
64 using UniqueActionSet =
65  std::unique_ptr<std::remove_pointer_t<XrActionSet>,
66  handle_destroyers::OpenXRHandleDestroyer>;
67 
68 //! Scoped RAII pointer type wrapping XrSpace
69 using UniqueSpace = std::unique_ptr<std::remove_pointer_t<XrSpace>,
70  handle_destroyers::OpenXRHandleDestroyer>;
71 //! @}
72 #else
73 
74 #define XRTRAITS_CREATE_UNIQUE_HANDLE_TYPE(HandleName) \
75  namespace handle_destroyers { \
76  class Destroy##HandleName \
77  { \
78  public: \
79  Destroy##HandleName() = default; \
80  void operator()(Xr##HandleName handle) const \
81  { \
82  if (handle != XR_NULL_HANDLE) { \
83  xrDestroy##HandleName(handle); \
84  } \
85  } \
86  }; \
87  } \
88  using Unique##HandleName = \
89  std::unique_ptr<std::remove_pointer_t<Xr##HandleName>, \
90  handle_destroyers::Destroy##HandleName>
91 
92 XRTRAITS_CREATE_UNIQUE_HANDLE_TYPE(Instance);
93 XRTRAITS_CREATE_UNIQUE_HANDLE_TYPE(Session);
94 XRTRAITS_CREATE_UNIQUE_HANDLE_TYPE(Swapchain);
95 XRTRAITS_CREATE_UNIQUE_HANDLE_TYPE(Action);
96 XRTRAITS_CREATE_UNIQUE_HANDLE_TYPE(ActionSet);
97 XRTRAITS_CREATE_UNIQUE_HANDLE_TYPE(Space);
98 
99 #undef XRTRAITS_CREATE_UNIQUE_HANDLE_TYPE
100 
101 #endif // XRTRAITS_DOXYGEN
102 
103 } // namespace xrtraits
Main namespace for these C++ OpenXR utilities.
Definition: GetChained.h:26
std::unique_ptr< std::remove_pointer_t< XrAction >, handle_destroyers::OpenXRHandleDestroyer > UniqueAction
Scoped RAII pointer type wrapping XrAction.
Definition: UniqueHandles.h:61
std::unique_ptr< std::remove_pointer_t< XrSession >, handle_destroyers::OpenXRHandleDestroyer > UniqueSession
Scoped RAII pointer type wrapping XrSession.
Definition: UniqueHandles.h:52
std::unique_ptr< std::remove_pointer_t< XrSwapchain >, handle_destroyers::OpenXRHandleDestroyer > UniqueSwapchain
Scoped RAII pointer type wrapping XrSwapchain.
Definition: UniqueHandles.h:57
std::unique_ptr< std::remove_pointer_t< XrActionSet >, handle_destroyers::OpenXRHandleDestroyer > UniqueActionSet
Scoped RAII pointer type wrapping XrActionSet.
Definition: UniqueHandles.h:66
std::unique_ptr< std::remove_pointer_t< XrInstance >, handle_destroyers::OpenXRHandleDestroyer > UniqueInstance
Scoped RAII pointer type wrapping XrInstance.
Definition: UniqueHandles.h:48
std::unique_ptr< std::remove_pointer_t< XrSpace >, handle_destroyers::OpenXRHandleDestroyer > UniqueSpace
Scoped RAII pointer type wrapping XrSpace.
Definition: UniqueHandles.h:70