HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
defer.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5
6#include <type_traits>
7#include <concepts>
8#include <functional>
9
10#pragma once
11
12namespace hi::inline v1 {
13
20class defer {
21public:
22 defer() = delete;
23 defer(defer &&) = delete;
24 defer(defer const &) = delete;
25 defer &operator=(defer &&) = delete;
26 defer &operator=(defer const &) = delete;
27
28 template<std::invocable<> Func>
29 [[nodiscard]] defer(Func &&func) noexcept : _func(std::forward<Func>(func)) {}
30
31 ~defer()
32 {
33 if (_func) {
34 _func();
35 }
36 }
37
38 void cancel() noexcept
39 {
40 _func = nullptr;
41 }
42
43private:
44 std::function<void()> _func;
45};
46
47}
48
DOXYGEN BUG.
Definition algorithm.hpp:13
Defer execution of a lambda to the end of the scope.
Definition defer.hpp:20