From 26252b0f4a24e536074aa034a88868bf8ea07b76 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Wed, 24 Sep 2025 01:10:46 +0800 Subject: Free all xcb reply. --- include/cru/base/Guard.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'include') diff --git a/include/cru/base/Guard.h b/include/cru/base/Guard.h index 5a9f9c5d..6b6cf851 100644 --- a/include/cru/base/Guard.h +++ b/include/cru/base/Guard.h @@ -1,5 +1,6 @@ #pragma once +#include #include namespace cru { @@ -23,4 +24,29 @@ struct Guard { ExitFunc on_exit; }; + +template +struct FreeLater { + FreeLater(T* ptr) : ptr(ptr) {} + ~FreeLater() { ::free(ptr); } + + FreeLater(const FreeLater& other) = delete; + FreeLater& operator=(const FreeLater& other) = delete; + + FreeLater(FreeLater&& other) : ptr(other.ptr) { other.ptr = nullptr; } + FreeLater& operator=(FreeLater&& other) { + if (this != &other) { + ::free(ptr); + ptr = other.ptr; + other.ptr = nullptr; + } + return *this; + } + + operator T*() const { return ptr; } + T* operator->() { return ptr; } + + T* ptr; +}; + } // namespace cru -- cgit v1.2.3