From 4fa30d56305c51fa9db5e5960806ce93095192aa Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 30 Sep 2024 09:40:19 +0200 Subject: [PATCH] memory: add get_ptr This was migrated from WinStd being platform independent. Signed-off-by: Simon Rozman --- include/stdex/memory.hpp | 154 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/include/stdex/memory.hpp b/include/stdex/memory.hpp index 1041da3ce..97c6f05b7 100644 --- a/include/stdex/memory.hpp +++ b/include/stdex/memory.hpp @@ -139,6 +139,160 @@ namespace stdex public: unsigned char m_data[N]; ///< BLOB data }; + + /// + /// Helper class for returning pointers to std::unique_ptr + /// + template + class ref_unique_ptr + { + public: + /// + /// Takes ownership of the pointer + /// + /// \param[in,out] owner Object to attach helper to + /// + ref_unique_ptr(_Inout_ std::unique_ptr &owner) : + m_own(owner), + m_ptr(owner.release()) + {} + + /// + /// Moves object + /// + /// \param[in,out] other Source object + /// + ref_unique_ptr(_Inout_ ref_unique_ptr &&other) : + m_own(other.m_own), + m_ptr(other.m_ptr) + { + other.m_ptr = nullptr; + } + + /// + /// Returns ownership of the pointer + /// + ~ref_unique_ptr() + { + if (m_ptr != nullptr) + m_own.reset(m_ptr); + } + + /// + /// Operator for pointer-to-pointer parameters by value use-cases. + /// + /// \return Pointer to the pointer + /// + operator T**() + { + return &m_ptr; + } + + /// + /// Operator for reverence-to-pointer parameters by value use-cases. + /// + /// \return Reference to the pointer + /// + operator T*&() + { + return m_ptr; + } + + protected: + std::unique_ptr &m_own; ///< Original owner of the pointer + T *m_ptr; ///< Pointer + }; + + /// + /// Helper function template for returning pointers to std::unique_ptr + /// + /// \param[in,out] owner Original owner of the pointer + /// + /// \returns A helper wrapper class to handle returning a reference to the pointer + /// + template + ref_unique_ptr get_ptr(_Inout_ std::unique_ptr &owner) noexcept + { + return ref_unique_ptr(owner); + } + + /// + /// Helper class for returning pointers to std::unique_ptr + /// (specialization for arrays) + /// + template + class ref_unique_ptr + { + public: + /// + /// Takes ownership of the pointer + /// + /// \param[in,out] owner Object to attach helper to + /// + ref_unique_ptr(_Inout_ std::unique_ptr &owner) noexcept : + m_own(owner), + m_ptr(owner.release()) + {} + + /// + /// Moves object + /// + /// \param[in,out] other Source object + /// + ref_unique_ptr(_Inout_ ref_unique_ptr &&other) : + m_own(other.m_own), + m_ptr(other.m_ptr) + { + other.m_ptr = nullptr; + } + + /// + /// Returns ownership of the pointer + /// + virtual ~ref_unique_ptr() + { + if (m_ptr != nullptr) + m_own.reset(m_ptr); + } + + /// + /// Operator for pointer-to-pointer parameters by value use-cases. + /// + /// \return Pointer to the pointer + /// + operator T**() noexcept + { + return &m_ptr; + } + + /// + /// Operator for reverence-to-pointer parameters by value use-cases. + /// + /// \return Reference to the pointer + /// + operator T*&() + { + return m_ptr; + } + + protected: + std::unique_ptr &m_own; ///< Original owner of the pointer + T *m_ptr; ///< Pointer + }; + + /// + /// Helper function template for returning pointers to std::unique_ptr + /// (specialization for arrays) + /// + /// \param[in,out] owner Original owner of the pointer + /// + /// \returns A helper wrapper class to handle returning a reference to the pointer + /// + template + ref_unique_ptr get_ptr(_Inout_ std::unique_ptr& owner) noexcept + { + return ref_unique_ptr(owner); + } } #if defined(__GNUC__)