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{}); + } +}