diff options
Diffstat (limited to 'include/cru/base/Guard.h')
-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 |