From eca67e7d2bd227433eb4b47b499cfcc0106a3eaa Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 18 Nov 2025 20:25:52 +0800 Subject: Fix AutoFreePtr. --- include/cru/base/Guard.h | 44 ++++++++++---------------------------------- 1 file changed, 10 insertions(+), 34 deletions(-) (limited to 'include') diff --git a/include/cru/base/Guard.h b/include/cru/base/Guard.h index 65e2dee4..c05bc668 100644 --- a/include/cru/base/Guard.h +++ b/include/cru/base/Guard.h @@ -29,51 +29,27 @@ struct Guard { ExitFunc on_exit; }; -/** - * FreeFunc must handle nullptr correctly. - */ -template -struct TAutoFreePtr { - TAutoFreePtr(T* ptr) : ptr(ptr) {} - ~TAutoFreePtr() { FreeFunc(ptr); } - - CRU_DELETE_COPY(TAutoFreePtr) - - TAutoFreePtr(TAutoFreePtr&& other) noexcept : ptr(other.ptr) { - other.ptr = nullptr; - } - - TAutoFreePtr& operator=(TAutoFreePtr&& other) noexcept { - if (this != &other) { - FreeFunc(ptr); - ptr = other.ptr; - other.ptr = nullptr; - } - return *this; - } - - operator T*() const { return ptr; } - T* operator->() { return ptr; } - - T* ptr; -}; - namespace details { template -inline void MyFree(T* p) noexcept { - ::free(p); -} +struct AutoFreePtrDeleter { + void operator()(T* ptr) const noexcept { ::free(ptr); } +}; } // namespace details template -using AutoFreePtr = TAutoFreePtr>; +using AutoFreePtr = std::unique_ptr>; + +template +inline AutoFreePtr MakeAutoFree(T* ptr) { + return AutoFreePtr(ptr); +} template class AutoDestruct { public: AutoDestruct() : value_(std::nullopt), auto_destruct_(false) {} - AutoDestruct(T value, bool auto_destruct = true) + explicit AutoDestruct(T value, bool auto_destruct = true) : value_(std::move(value)), auto_destruct_(auto_destruct) {} CRU_DELETE_COPY(AutoDestruct) -- cgit v1.2.3