AIStatefulTask ‐ Asynchronous, Stateful Task Scheduler library.

Threads-like task objects evolving through user-defined states.

AIDelayedFunction.h
Go to the documentation of this file.
1
34#pragma once
35
36#include <functional>
37#include <tuple>
38
39#ifndef DOXYGEN
40template<typename F>
41class AIDelayedFunction; // not defined.
42#endif
43
77template<typename R, typename ...Args>
78class AIDelayedFunction<R(Args...)>
79{
80 private:
81 std::function<R(Args...)> m_function; // Pointer to the (member) function.
82 std::tuple<Args...> m_args; // Copy of the arguments to be passed.
83 R m_result; // Future result of the function.
84
85 public:
87 AIDelayedFunction(R (*fp)(Args...)) { m_function = fp; }
88
94 template<class C>
95 AIDelayedFunction(C* object, R (C::*memfn)(Args...))
96 { m_function = [object, memfn](Args... args){ (object->*memfn)(args...); }; }
97
99 void swap(AIDelayedFunction& other) noexcept
100 {
101 m_function.swap(other.m_function);
102 m_args.swap(other.m_args);
103 std::swap(m_result, other.m_result);
104 }
105
107 void operator()(Args... args) { m_args = std::make_tuple(args...); }
108
110 void invoke() { m_result = std::apply(m_function, m_args); }
111
113 R const& get() const { return m_result; }
114};
115
117template<typename ...Args>
118class AIDelayedFunction<void(Args...)>
119{
120 private:
121 std::function<void(Args...)> m_function; // Pointer to the (member) function.
122 std::tuple<Args...> m_args; // Copy of the arguments to be passed.
123
124 public:
126 AIDelayedFunction(void (*fp)(Args...)) { m_function = fp; }
127
133 template<class C>
134 AIDelayedFunction(C* object, void (C::*memfn)(Args...))
135 { m_function = [object, memfn](Args... args){ (object->*memfn)(args...); }; }
136
138 void operator()(Args... args) { m_args = std::make_tuple(args...); }
139
141 void invoke() { std::apply(m_function, m_args); }
142};
void operator()(Args... args)
Store the arguments to be passed.
Definition: AIDelayedFunction.h:107
void invoke()
Actually invoke the call to the stored function with the stored arguments.
Definition: AIDelayedFunction.h:110
AIDelayedFunction(R(*fp)(Args...))
Construct an AIDelayedFunction for a free function <span class="inlinecode">R f(Args....
Definition: AIDelayedFunction.h:87
R const & get() const
Get the result, only valid after invoke was called.
Definition: AIDelayedFunction.h:113
void swap(AIDelayedFunction &other) noexcept
Exchange the state with that of other.
Definition: AIDelayedFunction.h:99
AIDelayedFunction(C *object, R(C::*memfn)(Args...))
Definition: AIDelayedFunction.h:95
AIDelayedFunction(C *object, void(C::*memfn)(Args...))
Definition: AIDelayedFunction.h:134
void operator()(Args... args)
Store the arguments to be passed.
Definition: AIDelayedFunction.h:138
AIDelayedFunction(void(*fp)(Args...))
Construct a AIDelayedFunction for a free function.
Definition: AIDelayedFunction.h:126
void invoke()
Actually invoke the call to the stored function with the stored arguments.
Definition: AIDelayedFunction.h:141