File size: 1,064 Bytes
2492322 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#ifndef MAMBA_CORE_UTIL_SCOPE_HPP
#define MAMBA_CORE_UTIL_SCOPE_HPP
#include <stdexcept>
#include "mamba/core/output.hpp"
#include "spdlog/spdlog.h"
namespace mamba
{
template <typename F>
struct on_scope_exit
{
F func;
explicit on_scope_exit(F&& f)
: func(std::forward<F>(f))
{
}
~on_scope_exit()
{
try
{
func();
}
catch (const std::exception& ex)
{
LOG_ERROR << fmt::format("Scope exit error (caught and ignored): {}", ex.what());
}
catch (...)
{
LOG_ERROR << "Scope exit unknown error (caught and ignored)";
}
}
// Deactivate copy & move until we implement moves
on_scope_exit(const on_scope_exit&) = delete;
on_scope_exit& operator=(const on_scope_exit&) = delete;
on_scope_exit(on_scope_exit&&) = delete;
on_scope_exit& operator=(on_scope_exit&&) = delete;
};
}
#endif
|