From dfe62dcf8bcefc523b466e127c3edc4dc2756629 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 6 Oct 2024 13:57:39 +0800 Subject: Rename common to base. --- include/cru/base/Guard.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 include/cru/base/Guard.h (limited to 'include/cru/base/Guard.h') diff --git a/include/cru/base/Guard.h b/include/cru/base/Guard.h new file mode 100644 index 00000000..5a9f9c5d --- /dev/null +++ b/include/cru/base/Guard.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +namespace cru { +struct Guard { + using ExitFunc = std::function; + + Guard() = default; + explicit Guard(const ExitFunc& f) : on_exit(f) {} + explicit Guard(ExitFunc&& f) : on_exit(std::move(f)) {} + Guard(const Guard&) = delete; + Guard(Guard&&) = default; + Guard& operator=(const Guard&) = delete; + Guard& operator=(Guard&&) = default; + ~Guard() { + if (on_exit) { + on_exit(); + } + } + + void Drop() { on_exit = {}; } + + ExitFunc on_exit; +}; +} // namespace cru -- cgit v1.2.3