HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
function_fifo.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 "wfree_fifo.hpp"
8#include "functional.hpp"
9#include "../macros.hpp"
10#include <future>
11
12hi_export_module(hikogui.container.function_fifo);
13
14hi_export namespace hi::inline v1 {
15
25template<typename Proto = void(), std::size_t SlotSize = 64>
27public:
28 constexpr function_fifo() noexcept = default;
29 function_fifo(function_fifo const&) = delete;
30 function_fifo(function_fifo&&) = delete;
31 function_fifo& operator=(function_fifo const&) = delete;
32 function_fifo& operator=(function_fifo&&) = delete;
33
36 [[nodiscard]] bool empty() const noexcept
37 {
38 return _fifo.empty();
39 }
40
46 template<typename... Args>
47 auto run_one(Args const &...args) noexcept
48 {
49 return _fifo.take_one([args...](auto& item) {
50 return item(args...);
51 });
52 }
53
58 void run_all() noexcept
59 {
60 while (run_one()) {}
61 }
62
71 template<typename Func>
72 void add_function(Func&& func) noexcept
73 {
74 _fifo.insert(make_function<Proto>(std::forward<Func>(func)));
75 }
76
87 template<typename Func, typename... Args>
88 auto add_async_function(Func&& func, Args&&...args) noexcept
89 requires(std::is_invocable_v<std::decay_t<Func>, std::decay_t<Args>...>)
90 {
91 return _fifo.insert_and_invoke(
92 [](auto& item) {
93 return item.get_future();
94 },
95 make_function<Proto>(std::forward<Func>(func)));
96 }
97
98private : wfree_fifo<function<Proto>, SlotSize> _fifo;
99};
100
101} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A fifo (First-in, Firts-out) for asynchronous calls.
Definition function_fifo.hpp:26
void run_all() noexcept
Run all the functions posted or send on the fifo.
Definition function_fifo.hpp:58
auto run_one(Args const &...args) noexcept
Run one of the function that was posted or send.
Definition function_fifo.hpp:47
void add_function(Func &&func) noexcept
Asynchronously post a functor to the fifo to be executed later.
Definition function_fifo.hpp:72
auto add_async_function(Func &&func, Args &&...args) noexcept
Asynchronously send a functor to the fifo to be executed later.
Definition function_fifo.hpp:88
bool empty() const noexcept
Check if there are not functions added to the fifo.
Definition function_fifo.hpp:36
A wait-free multiple-producer/single-consumer fifo designed for absolute performance.
Definition wfree_fifo.hpp:39