ClickHouse/base/ext/scope_guard.h

115 lines
2.9 KiB
C++
Raw Normal View History

2015-10-05 00:33:43 +00:00
#pragma once
#include <functional>
2020-01-29 15:49:47 +00:00
#include <memory>
#include <utility>
2015-10-05 00:33:43 +00:00
namespace ext
{
template <class F>
class [[nodiscard]] basic_scope_guard
{
2015-10-05 00:33:43 +00:00
public:
constexpr basic_scope_guard() = default;
2020-03-05 14:42:02 +00:00
constexpr basic_scope_guard(basic_scope_guard && src) : function{src.release()} {}
constexpr basic_scope_guard & operator=(basic_scope_guard && src)
{
if (this != &src)
{
invoke();
2020-03-05 14:42:02 +00:00
function = src.release();
2020-01-29 15:49:47 +00:00
}
return *this;
}
template <typename G, typename = std::enable_if_t<std::is_convertible_v<G, F>, void>>
2020-03-05 14:42:02 +00:00
constexpr basic_scope_guard(basic_scope_guard<G> && src) : function{src.release()} {}
2020-01-29 15:49:47 +00:00
template <typename G, typename = std::enable_if_t<std::is_convertible_v<G, F>, void>>
constexpr basic_scope_guard & operator=(basic_scope_guard<G> && src)
{
if (this != &src)
{
invoke();
2020-03-05 14:42:02 +00:00
function = src.release();
}
return *this;
}
template <typename G, typename = std::enable_if_t<std::is_convertible_v<G, F>, void>>
constexpr basic_scope_guard(const G & function_) : function{function_} {}
template <typename G, typename = std::enable_if_t<std::is_convertible_v<G, F>, void>>
constexpr basic_scope_guard(G && function_) : function{std::move(function_)} {}
~basic_scope_guard() { invoke(); }
2020-03-05 14:42:02 +00:00
static constexpr bool is_nullable = std::is_constructible_v<bool, F>;
2020-01-29 15:49:47 +00:00
explicit operator bool() const
{
2020-03-05 14:42:02 +00:00
if constexpr (is_nullable)
2020-01-29 15:49:47 +00:00
return static_cast<bool>(function);
return true;
}
2020-03-05 14:42:02 +00:00
void reset()
{
invoke();
release();
}
F release()
{
static_assert(is_nullable);
return std::exchange(function, {});
}
2020-01-29 15:49:47 +00:00
template <typename G, typename = std::enable_if_t<std::is_convertible_v<G, F>, void>>
basic_scope_guard<F> & join(basic_scope_guard<G> && other)
{
if (other.function)
{
if (function)
{
2020-03-05 14:42:02 +00:00
function = [x = std::make_shared<std::pair<F, G>>(std::move(function), other.release())]()
2020-01-29 15:49:47 +00:00
{
std::move(x->first)();
std::move(x->second)();
};
}
else
2020-03-05 14:42:02 +00:00
function = other.release();
2020-01-29 15:49:47 +00:00
}
return *this;
}
private:
void invoke()
{
2020-03-05 14:42:02 +00:00
if constexpr (is_nullable)
{
if (!function)
return;
}
2020-01-29 15:49:47 +00:00
std::move(function)();
}
F function = F{};
2015-10-05 00:33:43 +00:00
};
using scope_guard = basic_scope_guard<std::function<void(void)>>;
2015-10-05 00:33:43 +00:00
template <class F>
inline basic_scope_guard<F> make_scope_guard(F && function_) { return std::forward<F>(function_); }
2015-10-05 00:33:43 +00:00
}
#define SCOPE_EXIT_CONCAT(n, ...) \
const auto scope_exit##n = ext::make_scope_guard([&] { __VA_ARGS__; })
#define SCOPE_EXIT_FWD(n, ...) SCOPE_EXIT_CONCAT(n, __VA_ARGS__)
#define SCOPE_EXIT(...) SCOPE_EXIT_FWD(__LINE__, __VA_ARGS__)