Monado OpenXR Runtime
Loading...
Searching...
No Matches
xrt_deleters.hpp
Go to the documentation of this file.
1// Copyright 2019-2026, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Generic unique_ptr deleters for Monado types
6 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
7 * @ingroup xrt_iface
8 */
9
10#pragma once
11
12#include <memory>
13
14namespace xrt {
15
16/*!
17 * Generic deleter functors for the variety of interface/object types in Monado.
18 *
19 * Use these with std::unique_ptr to make per-interface type aliases for unique ownership.
20 * These are stateless deleters whose function pointer is statically specified as a template argument.
21 */
22namespace deleters {
23 /*!
24 * Deleter type for interfaces with destroy functions that take pointers to interface pointers (so they may be
25 * zeroed).
26 * Wraps the type and the destroy function for use with `std::unique_ptr`
27 */
28 template <typename T, void (*DeleterFn)(T **)> struct ptr_ptr_deleter
29 {
30 void
31 operator()(T *obj) const noexcept
32 {
33 if (obj == nullptr) {
34 return;
35 }
36 DeleterFn(&obj);
37 }
38 };
39
40 /*!
41 * Deleter type for interfaces with destroy functions that take just pointers.
42 * Wraps the type and the destroy function for use with `std::unique_ptr`
43 */
44 template <typename T, void (*DeleterFn)(T *)> struct ptr_deleter
45 {
46 void
47 operator()(T *obj) const noexcept
48 {
49 if (obj == nullptr) {
50 return;
51 }
52 DeleterFn(obj);
53 }
54 };
55
56 /*!
57 * Deleter type for ref-counted interfaces with two-parameter `reference(dest, src)` functions.
58 * Wraps the type and the interface reference function for use with `std::unique_ptr`
59 */
60 template <typename T, void (*ReferenceFn)(T **, T *)> struct reference_deleter
61 {
62 void
63 operator()(T *obj) const noexcept
64 {
65 if (obj == nullptr) {
66 return;
67 }
68 ReferenceFn(&obj, nullptr);
69 }
70 };
71
72
73 /*!
74 * Deleter type for opaque object types for interfaces with destroy functions that take pointers to
75 * interface pointers (so they may be zeroed).
76 * Wraps the types and the interface destroy function for use with `std::unique_ptr`
77 */
78 template <typename Derived, typename T, void (*DeleterFn)(T **)> struct cast_ptr_ptr_deleter
79 {
80 void
81 operator()(Derived *obj) const noexcept
82 {
83 if (obj == nullptr) {
84 return;
85 }
86 T *base = reinterpret_cast<T *>(obj);
87 DeleterFn(&base);
88 }
89 };
90
91 namespace detail {
92 /// Get the base type of a non-opaque derived type.
93 template <typename Derived> using BaseType_t = decltype(std::declval<Derived>().base);
94
95 /// Get the base of the base type of a non-opaque derived type.
96 template <typename Derived> using BaseBaseType_t = decltype(std::declval<Derived>().base.base);
97 } // namespace detail
98
99 /*!
100 * Deleter type for non-opaque twice-derived object types from interfaces with destroy functions that take
101 * pointers to interface pointers (so they may be zeroed). Wraps the type and the interface destroy function for
102 * use with `std::unique_ptr` (interface type deduced)
103 */
104 template <typename Derived, void (*DeleterFn)(detail::BaseBaseType_t<Derived> **)>
106 {
107 void
108 operator()(Derived *obj) const noexcept
109 {
110 if (obj == nullptr) {
111 return;
112 }
113 detail::BaseBaseType_t<Derived> *base = &obj->base.base;
114 DeleterFn(&base);
115 }
116 };
117
118 /*!
119 * Deleter type for non-opaque once-derived object types from interfaces with destroy functions that take
120 * pointers to interface pointers (so they may be zeroed). Wraps the type and the interface destroy function for
121 * use with `std::unique_ptr` (interface type deduced)
122 */
123 template <typename Derived, void (*DeleterFn)(detail::BaseType_t<Derived> **)> struct base_ptr_ptr_deleter
124 {
125 void
126 operator()(Derived *obj) const noexcept
127 {
128 if (obj == nullptr) {
129 return;
130 }
131 detail::BaseType_t<Derived> *base = &obj->base;
132 DeleterFn(&base);
133 }
134 };
135
136} // namespace deleters
137
138} // namespace xrt
Deleter type for non-opaque twice-derived object types from interfaces with destroy functions that ta...
Definition xrt_deleters.hpp:106
Deleter type for non-opaque once-derived object types from interfaces with destroy functions that tak...
Definition xrt_deleters.hpp:124
Deleter type for opaque object types for interfaces with destroy functions that take pointers to inte...
Definition xrt_deleters.hpp:79
Deleter type for interfaces with destroy functions that take just pointers.
Definition xrt_deleters.hpp:45
Deleter type for interfaces with destroy functions that take pointers to interface pointers (so they ...
Definition xrt_deleters.hpp:29
Deleter type for ref-counted interfaces with two-parameter reference(dest, src) functions.
Definition xrt_deleters.hpp:61
decltype(std::declval< Derived >().base) BaseType_t
Get the base type of a non-opaque derived type.
Definition xrt_deleters.hpp:93
decltype(std::declval< Derived >().base.base) BaseBaseType_t
Get the base of the base type of a non-opaque derived type.
Definition xrt_deleters.hpp:96