Monado OpenXR Runtime
Loading...
Searching...
No Matches
g_frame_node.hpp
Go to the documentation of this file.
1// Copyright 2026, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Header for glue classes to wrap xrt frame node interfaces.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup aux_util
8 */
9
10#pragma once
11
12#include "xrt/xrt_frame.h"
13
14#include "g_catch_guard.hpp"
15#include "g_traits.hpp"
16
17#include <type_traits>
18
19
20namespace xrt::util {
21
22/*!
23 * CRTP glue wrapper for @ref xrt_frame_node. Relies on standard layout to
24 * recover the derived object from the C struct, and has some requirements and
25 * limitations because of that. See @ref cpp-glue-wrappers for the guide and
26 * conventions for these wrappers.
27 *
28 * Usually inherited alongside another glue base such as @ref FrameServerBase,
29 * so that the object is destroyed together with the @ref xrt_frame_context it
30 * is added to.
31 */
32template <class T> class FrameNodeBase
33{
34public: // Methods
35 FrameNodeBase() noexcept
36 {
37 static_assert(std::is_standard_layout_v<FrameNodeBase>,
38 "glue base must be standard layout for pointer recovery");
39 static_assert(is_non_virtual_base_v<FrameNodeBase, T>,
40 "glue base must be a non-virtual base of T for pointer recovery");
41
42 auto &node = *getFrameNode();
43
44 node.break_apart = breakApartWrap;
45 node.destroy = destroyFrameNodeWrap;
46 }
47
48 ~FrameNodeBase() noexcept = default;
49
50 const T &
51 derived() const noexcept
52 {
53 return static_cast<const T &>(*this);
54 }
55
56 T &
57 derived() noexcept
58 {
59 return static_cast<T &>(*this);
60 }
61
62 static const T *
63 fromFrameNode(const xrt_frame_node *node) noexcept
64 {
65 return &(reinterpret_cast<const FrameNodeBase *>(node)->derived());
66 }
67
68 static T *
69 fromFrameNode(xrt_frame_node *node) noexcept
70 {
71 return &(reinterpret_cast<FrameNodeBase *>(node)->derived());
72 }
73
74 const xrt_frame_node *
75 getFrameNode() const noexcept
76 {
77 return &mFrameNode;
78 }
79
81 getFrameNode() noexcept
82 {
83 return &mFrameNode;
84 }
85
86
87private: // Members
88 /*!
89 * Wrapped @ref xrt_frame_node. Must be the first data member: a pointer to
90 * it is then interconvertible with a pointer to this standard-layout base,
91 * which lets the glue cast a C pointer back to the derived C++ class. See
92 * @ref cpp-glue-wrappers.
93 */
94 xrt_frame_node mFrameNode = {};
95
96
97private: // Functions
98// Not `->derived()`, that is ambiguous when T has more than one glue base.
99#define GET(node) (*fromFrameNode(node))
100
101 static void
102 breakApartWrap(struct xrt_frame_node *node) noexcept
103 try {
104 GET(node).breakApart();
105 }
106 G_CATCH_GUARDS_VOID
107
108 static void
109 destroyFrameNodeWrap(struct xrt_frame_node *node) noexcept
110 try {
111 T::destroyFrameNode(node);
112 }
113 G_CATCH_GUARDS_VOID
114
115#undef GET
116};
117
118} // namespace xrt::util
CRTP glue wrapper for xrt_frame_node.
Definition g_frame_node.hpp:33
Catch guards for glue classes.
Shared type traits for glue classes.
A interface object used for destroying a frame graph.
Definition xrt_frame.h:87
Data frame header.