Monado OpenXR Runtime
Loading...
Searching...
No Matches
g_imu_sink.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 IMU sink interfaces.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup aux_util
8 */
9
10#pragma once
11
12#include "xrt/xrt_tracking.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_imu_sink. 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 * The derived class `T` must implement `void pushImu(xrt_imu_sample &sample)`.
29 * Exceptions thrown from it are caught and logged. Unlike frame sinks, IMU
30 * sinks are not part of a frame context graph, so the derived class manages
31 * its own lifetime.
32 */
33template <class T> class ImuSinkBase
34{
35public: // Methods
36 ImuSinkBase() noexcept
37 {
38 static_assert(std::is_standard_layout_v<ImuSinkBase>,
39 "glue base must be standard layout for pointer recovery");
40 static_assert(is_non_virtual_base_v<ImuSinkBase, T>,
41 "glue base must be a non-virtual base of T for pointer recovery");
42
43 mImuSink.push_imu = pushImuWrap;
44 }
45
46 ~ImuSinkBase() 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 fromImuSink(const xrt_imu_sink *sink) noexcept
62 {
63 return &(reinterpret_cast<const ImuSinkBase *>(sink)->derived());
64 }
65
66 static T *
67 fromImuSink(xrt_imu_sink *sink) noexcept
68 {
69 return &(reinterpret_cast<ImuSinkBase *>(sink)->derived());
70 }
71
72 const xrt_imu_sink *
73 getImuSink() const noexcept
74 {
75 return &mImuSink;
76 }
77
79 getImuSink() noexcept
80 {
81 return &mImuSink;
82 }
83
84
85private: // Members
86 /*!
87 * Wrapped @ref xrt_imu_sink. 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_imu_sink mImuSink = {};
93
94
95private: // Functions
96// Not `->derived()`, that is ambiguous when T has more than one glue base.
97#define GET(sink) (*fromImuSink(sink))
98
99 static void
100 pushImuWrap(struct xrt_imu_sink *sink, struct xrt_imu_sample *sample) noexcept
101 try {
102 GET(sink).pushImu(*sample);
103 }
104 G_CATCH_GUARDS_VOID
105
106#undef GET
107};
108
109} // namespace xrt::util
CRTP glue wrapper for xrt_imu_sink.
Definition g_imu_sink.hpp:34
Catch guards for glue classes.
Shared type traits for glue classes.
IMU Sample.
Definition xrt_tracking.h:179
An object to send IMU samples to.
Definition xrt_tracking.h:260
void(* push_imu)(struct xrt_imu_sink *, struct xrt_imu_sample *sample)
Push an IMU sample into the sink.
Definition xrt_tracking.h:264
Header defining the tracking system integration in Monado.