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#pragma once
6
7#include "../macros.hpp"
8#include <type_traits>
9#include <functional>
10
11hi_export_module(hikogui.utility.defer);
12
13hi_export namespace hi { inline namespace v1 {
14
21hi_export class defer {
22public:
23 defer() = delete;
24 defer(defer &&) = delete;
25 defer(defer const &) = delete;
26 defer &operator=(defer &&) = delete;
27 defer &operator=(defer const &) = delete;
28
29 template<std::invocable<> Func>
30 [[nodiscard]] defer(Func &&func) noexcept : _func(std::forward<Func>(func)) {}
31
32 ~defer()
33 {
34 if (_func) {
35 _func();
36 }
37 }
38
39 void cancel() noexcept
40 {
41 _func = nullptr;
42 }
43
44private:
45 std::function<void()> _func;
46};
47
48}}
49
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Defer execution of a lambda to the end of the scope.
Definition defer.hpp:21