mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-01-01 12:46:12 +00:00
xrt: Factor out deleters
This commit is contained in:
parent
2cf07dedf2
commit
8ee10c5a6b
70
src/xrt/include/xrt/xrt_deleters.hpp
Normal file
70
src/xrt/include/xrt/xrt_deleters.hpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
// Copyright 2019-2022, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
* @brief Generic unique_ptr deleters for Monado types
|
||||
* @author Ryan Pavlik <ryan.pavlik@collabora.com>
|
||||
* @ingroup xrt_iface
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace xrt {
|
||||
|
||||
/*!
|
||||
* Generic deleter functors for the variety of interface/object types in Monado.
|
||||
*
|
||||
* Use these with std::unique_ptr to make per-interface type aliases for unique ownership.
|
||||
* These are stateless deleters whose function pointer is statically specified as a template argument.
|
||||
*/
|
||||
namespace deleters {
|
||||
/*!
|
||||
* Deleter type for interfaces with destroy functions that take pointers to interface pointers (so they may be
|
||||
* zeroed).
|
||||
*/
|
||||
template <typename T, void (*DeleterFn)(T **)> struct ptr_ptr_deleter
|
||||
{
|
||||
void
|
||||
operator()(T *obj) const noexcept
|
||||
{
|
||||
if (obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
DeleterFn(&obj);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Deleter type for interfaces with destroy functions that take just pointers.
|
||||
*/
|
||||
template <typename T, void (*DeleterFn)(T *)> struct ptr_deleter
|
||||
{
|
||||
void
|
||||
operator()(T *obj) const noexcept
|
||||
{
|
||||
if (obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
DeleterFn(obj);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Deleter type for ref-counted interfaces with two-parameter `reference(dest, src)` functions.
|
||||
*/
|
||||
template <typename T, void (*ReferenceFn)(T **, T *)> struct reference_deleter
|
||||
{
|
||||
void
|
||||
operator()(T *obj) const noexcept
|
||||
{
|
||||
if (obj == nullptr) {
|
||||
return;
|
||||
}
|
||||
ReferenceFn(&obj, nullptr);
|
||||
}
|
||||
};
|
||||
} // namespace deleters
|
||||
|
||||
} // namespace xrt
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2019-2021, Collabora, Ltd.
|
||||
// Copyright 2019-2022, Collabora, Ltd.
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
/*!
|
||||
* @file
|
||||
|
@ -10,25 +10,14 @@
|
|||
#pragma once
|
||||
|
||||
#include "xrt_device.h"
|
||||
#include "xrt_deleters.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace xrt {
|
||||
|
||||
namespace deleters {
|
||||
//! Deleter type for xrt_device
|
||||
struct xrt_device_deleter
|
||||
{
|
||||
void
|
||||
operator()(xrt_device *dev) const noexcept
|
||||
{
|
||||
xrt_device_destroy(&dev);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace deleters
|
||||
|
||||
//! Unique-ownership smart pointer for a @ref xrt_device implementation.
|
||||
using unique_xrt_device = std::unique_ptr<xrt_device, deleters::xrt_device_deleter>;
|
||||
using unique_xrt_device = std::unique_ptr<xrt_device, deleters::ptr_ptr_deleter<xrt_device, xrt_device_destroy>>;
|
||||
|
||||
} // namespace xrt
|
||||
|
|
Loading…
Reference in a new issue