From d13421e4b607aa7b0c5c79dd9a81d89c75e3942f Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 15 Mar 2023 09:08:36 +0100 Subject: [PATCH] Add noop deleter Signed-off-by: Simon Rozman --- include/stdex/memory.h | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 include/stdex/memory.h diff --git a/include/stdex/memory.h b/include/stdex/memory.h new file mode 100644 index 000000000..dd7df16d7 --- /dev/null +++ b/include/stdex/memory.h @@ -0,0 +1,52 @@ +/* + SPDX-License-Identifier: MIT + Copyright © 2023 Amebis +*/ + +#pragma once + +#include + +namespace stdex +{ + /// + /// Noop deleter + /// + template + struct no_delete { + constexpr no_delete() noexcept = default; + + template , int> = 0> + inline no_delete(const no_delete&) noexcept {} + + inline void operator()(T* p) const noexcept { p; } + }; + + /// + /// Noop array deleter + /// + template + struct no_delete { + constexpr no_delete() noexcept = default; + + template , int> = 0> + inline no_delete(const no_delete<_Uty[]>&) noexcept {} + + template , int> = 0> + inline void operator()(_Uty* p) const noexcept { p; } + }; + + /// + /// Create shared_ptr with noop deleter. + /// + /// This may be used to wrap data on stack in a shared_ptr. However, be extra careful + /// that shared_ptr is completely dereferenced _before_ stack data goes out of scope. + /// + /// \param[in] p Pointer to assign to shared_ptr + /// + template + inline std::shared_ptr make_shared_no_delete(_In_ T* p) + { + return std::shared_ptr(p, no_delete{}); + } +}