Monado OpenXR Runtime
Loading...
Searching...
No Matches
g_io_cache.hpp
Go to the documentation of this file.
1// Copyright 2026, NVIDIA CORPORATION.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Header for glue classes to wrap xrt input/output cache interfaces.
6 * @ingroup aux_util
7 */
8
9#pragma once
10
11#include "xrt/xrt_io_cache.h"
12#include "g_catch_guard.hpp"
13#include "g_traits.hpp"
14
15#include <type_traits>
16
17
18namespace xrt::util {
19
20/*!
21 * CRTP glue wrapper for @ref xrt_io_cache. Relies on standard layout to
22 * recover the derived object from the C struct, and has some requirements and
23 * limitations because of that. See @ref cpp-glue-wrappers for the guide and
24 * conventions for these wrappers.
25 */
26template <class T> class IoCacheBase
27{
28public: // Methods
29 IoCacheBase() noexcept
30 {
31 static_assert(std::is_standard_layout_v<IoCacheBase>,
32 "glue base must be standard layout for pointer recovery");
33 static_assert(is_non_virtual_base_v<IoCacheBase, T>,
34 "glue base must be a non-virtual base of T for pointer recovery");
35
36 auto &xic = *getXIC();
37
38 xic.get_info = getInfoWrap;
39 xic.read = readWrap;
40 xic.create_space = createSpaceWrap;
41 xic.set_output = setOutputWrap;
42 xic.get_output_limits = getOutputLimitsWrap;
43 xic.destroy = destroyIoCacheWrap;
44 }
45
46 ~IoCacheBase() noexcept = default;
47
48 const T &
49 derived() const noexcept
50 {
51 return static_cast<const T &>(*this);
52 }
53
54 T &
55 derived() noexcept
56 {
57 return static_cast<T &>(*this);
58 }
59
60 static const T *
61 fromXIC(const xrt_io_cache *xic) noexcept
62 {
63 return &(reinterpret_cast<const IoCacheBase *>(xic)->derived());
64 }
65
66 static T *
67 fromXIC(xrt_io_cache *xic) noexcept
68 {
69 return &(reinterpret_cast<IoCacheBase *>(xic)->derived());
70 }
71
72 const xrt_io_cache *
73 getXIC() const noexcept
74 {
75 return &mIoCache;
76 }
77
79 getXIC() noexcept
80 {
81 return &mIoCache;
82 }
83
84
85private: // Members
86 /*!
87 * Wrapped @ref xrt_io_cache. Must be the first data member: a pointer to
88 * it is then interconvertible with a pointer to this standard-layout base,
89 * which lets the glue cast a C pointer back to the derived C++ class. See
90 * @ref cpp-glue-wrappers.
91 */
92 xrt_io_cache mIoCache = {};
93
94
95private: // Functions
96#define GET(xic) (fromXIC(xic)->derived())
97
98 static xrt_result_t
99 getInfoWrap(struct xrt_io_cache *xic, struct xrt_io_cache_info *out_info) noexcept
100 try {
101 return GET(xic).getInfo(out_info);
102 }
103 G_CATCH_GUARDS
104
105 static xrt_result_t
106 readWrap(struct xrt_io_cache *xic,
107 uint64_t generation_id,
108 uint32_t input_count,
109 struct xrt_input *inputs,
110 uint32_t output_count,
111 struct xrt_output *outputs,
112 bool *out_stale) noexcept
113 try {
114 return GET(xic).read(generation_id, input_count, inputs, output_count, outputs, out_stale);
115 }
116 G_CATCH_GUARDS
117
118 static xrt_result_t
119 createSpaceWrap(struct xrt_io_cache *xic,
120 struct xrt_device_id id,
121 enum xrt_input_name name,
122 struct xrt_space **out_space) noexcept
123 try {
124 return GET(xic).createSpace(id, name, out_space);
125 }
126 G_CATCH_GUARDS
127
128 static xrt_result_t
129 setOutputWrap(struct xrt_io_cache *xic,
130 struct xrt_device_id id,
131 enum xrt_output_name name,
132 const struct xrt_output_value *value) noexcept
133 try {
134 return GET(xic).setOutput(id, name, value);
135 }
136 G_CATCH_GUARDS
137
138 static xrt_result_t
139 getOutputLimitsWrap(struct xrt_io_cache *xic,
140 struct xrt_device_id id,
141 struct xrt_output_limits *out_limits) noexcept
142 try {
143 return GET(xic).getOutputLimits(id, out_limits);
144 }
145 G_CATCH_GUARDS
146
147 static void
148 destroyIoCacheWrap(struct xrt_io_cache *xic) noexcept
149 try {
150 T::destroyIoCache(xic);
151 }
152 G_CATCH_GUARDS_VOID
153
154#undef GET
155};
156
157} // namespace xrt::util
CRTP glue wrapper for xrt_io_cache.
Definition g_io_cache.hpp:27
Catch guards for glue classes.
Shared type traits for glue classes.
xrt_input_name
Every internal input source known to monado with a baked in type.
Definition xrt_defines.h:952
enum xrt_result xrt_result_t
Result type used across Monado.
xrt_output_name
Name of a output with a baked in type.
Definition xrt_defines.h:1636
A unique identifier for an xrt_device instance.
Definition xrt_device.h:47
A single named input, that sits on a xrt_device.
Definition xrt_device.h:236
Snapshot of the cache layout.
Definition xrt_io_cache.h:75
A cache of flattened device inputs and outputs.
Definition xrt_io_cache.h:116
Output limits of a particular device.
Definition xrt_device.h:275
A union of all output types.
Definition xrt_defines.h:2413
A single named output, that sits on a xrt_device.
Definition xrt_device.h:254
A space very similar to a OpenXR XrSpace but not a full one-to-one mapping, but used to power XrSpace...
Definition xrt_space.h:32
Header defining xrt input/output cache.