Monado OpenXR Runtime
xrt_deleters.hpp
Go to the documentation of this file.
1 // Copyright 2019-2022, 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 
14 namespace 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  */
22 namespace deleters {
23  /*!
24  * Deleter type for interfaces with destroy functions that take pointers to interface pointers (so they may be
25  * zeroed).
26  */
27  template <typename T, void (*DeleterFn)(T **)> struct ptr_ptr_deleter
28  {
29  void
30  operator()(T *obj) const noexcept
31  {
32  if (obj == nullptr) {
33  return;
34  }
35  DeleterFn(&obj);
36  }
37  };
38 
39  /*!
40  * Deleter type for interfaces with destroy functions that take just pointers.
41  */
42  template <typename T, void (*DeleterFn)(T *)> struct ptr_deleter
43  {
44  void
45  operator()(T *obj) const noexcept
46  {
47  if (obj == nullptr) {
48  return;
49  }
50  DeleterFn(obj);
51  }
52  };
53 
54  /*!
55  * Deleter type for ref-counted interfaces with two-parameter `reference(dest, src)` functions.
56  */
57  template <typename T, void (*ReferenceFn)(T **, T *)> struct reference_deleter
58  {
59  void
60  operator()(T *obj) const noexcept
61  {
62  if (obj == nullptr) {
63  return;
64  }
65  ReferenceFn(&obj, nullptr);
66  }
67  };
68 } // namespace deleters
69 
70 } // namespace xrt
Deleter type for interfaces with destroy functions that take just pointers.
Definition: xrt_deleters.hpp:43
Deleter type for interfaces with destroy functions that take pointers to interface pointers (so they ...
Definition: xrt_deleters.hpp:28
Deleter type for ref-counted interfaces with two-parameter reference(dest, src) functions.
Definition: xrt_deleters.hpp:58