HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
functional.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 "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <type_traits>
10#include <future>
11
12
13
14namespace hi::inline v1 {
15
16template<typename Proto>
18
19template<typename Result, typename... Arguments>
21public:
22 using result_type = Result;
23
24 virtual ~function() = default;
25 virtual result_type operator()(Arguments... arguments) = 0;
26};
27
28namespace detail {
29
30template<typename Function, typename Proto>
32
33template<typename Function, typename Result, typename... Arguments>
34class function_impl<Function, Result(Arguments...)> final : public function<Result(Arguments...)> {
35public:
36 using result_type = typename function<Result(Arguments...)>::result_type;
37
38 template<typename Func>
39 function_impl(Func&& func) noexcept : _function(std::forward<Func>(func))
40 {
41 }
42
43 result_type operator()(Arguments... arguments) override
44 {
45 return _function(std::forward<decltype(arguments)>(arguments)...);
46 }
47
48private:
49 Function _function;
50};
51
52template<typename Function, typename Proto>
54
55template<typename Function, typename... Arguments>
56class async_function_impl<Function, void(Arguments...)> final : public function<void(Arguments...)> {
57public:
58 using result_type = void;
59 using async_result_type = std::invoke_result_t<Function, Arguments...>;
60
61 template<typename Func>
62 async_function_impl(Func&& func) noexcept : _function(std::forward<Func>(func)), _promise()
63 {
64 }
65
66 void operator()(Arguments... arguments) override
67 {
68 if constexpr (std::is_same_v<async_result_type, void>) {
69 try {
70 _function(std::forward<decltype(arguments)>(arguments)...);
71 _promise.set_value();
72 } catch (...) {
73 _promise.set_exception(std::current_exception());
74 }
75 } else {
76 try {
77 _promise.set_value(_function(std::forward<decltype(arguments)>(arguments)...));
78 } catch (...) {
79 _promise.set_exception(std::current_exception());
80 }
81 }
82 }
83
84 [[nodiscard]] std::future<async_result_type> get_future() noexcept
85 {
86 return _promise.get_future();
87 }
88
89private:
90 Function _function;
92};
93
94}
95
96template<typename Proto, typename Func>
97auto make_function(Func&& func)
98{
99 return detail::function_impl<std::decay_t<Func>, Proto>{std::forward<Func>(func)};
100}
101
102template<typename Proto, typename Func>
103auto make_async_function(Func&& func)
104{
105 return detail::async_function_impl<std::decay_t<Func>, Proto>{std::forward<Func>(func)};
106}
107
108} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Definition functional.hpp:17
Definition functional.hpp:31
Definition functional.hpp:53
T current_exception(T... args)
T forward(T... args)