diff options
author | Yuqian Yang <crupest@crupest.life> | 2025-09-24 01:10:46 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-09-24 01:10:46 +0800 |
commit | 26252b0f4a24e536074aa034a88868bf8ea07b76 (patch) | |
tree | 60c3a3d8718a9a1a789277fa7dd8480047a26d78 /include | |
parent | 4927e536aac37607cd8aeaf6475b8e5bc91ddd1d (diff) | |
download | cru-26252b0f4a24e536074aa034a88868bf8ea07b76.tar.gz cru-26252b0f4a24e536074aa034a88868bf8ea07b76.tar.bz2 cru-26252b0f4a24e536074aa034a88868bf8ea07b76.zip |
Free all xcb reply.
Diffstat (limited to 'include')
-rw-r--r-- | include/cru/base/Guard.h | 26 |
1 files changed, 26 insertions, 0 deletions
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 <cstdlib> #include <functional> namespace cru { @@ -23,4 +24,29 @@ struct Guard { ExitFunc on_exit; }; + +template <typename T> +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 |