Monado OpenXR Runtime
Loading...
Searching...
No Matches
g_blob_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 constellation blob sink interfaces.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup aux_util
8 */
9
10#pragma once
11
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 t_blob_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:
29 * - `void pushBlobs(t_blob_observation &observation)`, which takes ownership of
30 * the observation's frame reference, see @ref t_blob_observation::frame.
31 * - `static void destroyBlobSink(t_blob_sink *sink)`, called from
32 * @ref t_blob_sink_destroy. It is static because it usually frees the
33 * object, similar to `destroyFrameNode` in @ref FrameNodeBase.
34 *
35 * Exceptions thrown from `pushBlobs` are caught and logged.
36 */
37template <class T> class BlobSinkBase
38{
39public: // Methods
40 BlobSinkBase() noexcept
41 {
42 static_assert(std::is_standard_layout_v<BlobSinkBase>,
43 "glue base must be standard layout for pointer recovery");
44 static_assert(is_non_virtual_base_v<BlobSinkBase, T>,
45 "glue base must be a non-virtual base of T for pointer recovery");
46
47 mBlobSink.push_blobs = pushBlobsWrap;
48 mBlobSink.destroy = destroyBlobSinkWrap;
49 }
50
51 ~BlobSinkBase() noexcept = default;
52
53 const T &
54 derived() const noexcept
55 {
56 return static_cast<const T &>(*this);
57 }
58
59 T &
60 derived() noexcept
61 {
62 return static_cast<T &>(*this);
63 }
64
65 static const T *
66 fromBlobSink(const t_blob_sink *sink) noexcept
67 {
68 return &(reinterpret_cast<const BlobSinkBase *>(sink)->derived());
69 }
70
71 static T *
72 fromBlobSink(t_blob_sink *sink) noexcept
73 {
74 return &(reinterpret_cast<BlobSinkBase *>(sink)->derived());
75 }
76
77 const t_blob_sink *
78 getBlobSink() const noexcept
79 {
80 return &mBlobSink;
81 }
82
84 getBlobSink() noexcept
85 {
86 return &mBlobSink;
87 }
88
89
90private: // Members
91 /*!
92 * Wrapped @ref t_blob_sink. Must be the first data member: a pointer to
93 * it is then interconvertible with a pointer to this standard-layout base,
94 * which lets the glue cast a C pointer back to the derived C++ class. See
95 * @ref cpp-glue-wrappers.
96 */
97 t_blob_sink mBlobSink = {};
98
99
100private: // Functions
101// Not `->derived()`, that is ambiguous when T has more than one glue base.
102#define GET(sink) (*fromBlobSink(sink))
103
104 static void
105 pushBlobsWrap(struct t_blob_sink *sink, struct t_blob_observation *observation) noexcept
106 try {
107 GET(sink).pushBlobs(*observation);
108 }
109 G_CATCH_GUARDS_VOID
110
111 static void
112 destroyBlobSinkWrap(struct t_blob_sink *sink) noexcept
113 try {
114 T::destroyBlobSink(sink);
115 }
116 G_CATCH_GUARDS_VOID
117
118#undef GET
119};
120
121} // namespace xrt::util
CRTP glue wrapper for t_blob_sink.
Definition g_blob_sink.hpp:38
Catch guards for glue classes.
Shared type traits for glue classes.
Definition t_constellation.h:104
A generic interface to allow a tracking system to receive "snapshots" of seen t_blob in a frame.
Definition t_constellation.h:136
void(* push_blobs)(struct t_blob_sink *tbs, struct t_blob_observation *observation)
Push a set of blobs into the sink.
Definition t_constellation.h:144
void(* destroy)(struct t_blob_sink *tbs)
Destroy this blob sink.
Definition t_constellation.h:149
Header defining the tracking system integration in Monado.