nngn
Loading...
Searching...
No Matches
delegate.h
Go to the documentation of this file.
1#ifndef NNGN_UTILS_DELEGATE_H
2#define NNGN_UTILS_DELEGATE_H
3
4#include <functional>
5#include <tuple>
6#include <utility>
7
8#include "concepts/fundamental.h"
9#include "utils.h"
10
11namespace nngn {
12
22template<function_pointer auto F>
23class delegate_fn : std::integral_constant<decltype(F), F> {
24 using value_type = decltype(F);
25 using base = std::integral_constant<value_type, F>;
26 template<typename ...Ts>
27 static constexpr bool invocable = std::is_invocable_v<value_type, Ts...>;
28public:
29 using base::operator value_type;
31 template<typename ...Ts>
32 decltype(auto) operator()(Ts &&...ts) requires(invocable<Ts...>) {
33 return std::invoke(F, FWD(ts)...);
34 }
35};
36
42template<typename F, typename ...Args>
43class delegate : private F {
44 static constexpr bool has_args = sizeof...(Args);
45 [[no_unique_address]] std::tuple<Args...> args = {};
46public:
48 delegate(void) requires(has_args) = default;
49 ~delegate(void) = default;
50 explicit delegate(Args ...a) : F{}, args(std::move(a)...) {}
51 explicit delegate(F f) requires(has_args) : F{std::move(f)} {}
52 delegate(F f, Args ...a) : F{std::move(f)}, args(std::move(a)...) {}
53 decltype(auto) operator()()
54 { return std::apply(static_cast<F&>(*this), this->args); }
55 template<size_t n>
56 decltype(auto) arg() const { return std::get<n>(this->args); }
57 template<size_t n>
58 decltype(auto) arg() { return std::get<n>(this->args); }
59};
60
61}
62
63#endif
typedef value_type
Definition: delegate.h:24
std::integral_constant< value_type, F > base
Definition: delegate.h:25
static constexpr bool invocable
Definition: delegate.h:27
Static container for a callable and its arguments.
Definition: delegate.h:43
static constexpr bool has_args
Definition: delegate.h:44
std::tuple< Args... > args
Definition: delegate.h:45
delegate(F f, Args ...a)
Definition: delegate.h:52
arg() const
Definition: delegate.h:56
arg()
Definition: delegate.h:58
delegate(F f)
Definition: delegate.h:51
move
Definition: camera.lua:48
a
Definition: input.lua:31
function f()) end
#define F(...)
Definition: pp.cpp:12
#define FWD(...)
Definition: utils.h:18
#define NNGN_MOVE_ONLY(x)
Definition: utils.h:39
Definition: audio.cpp:7
Definition: debug.h:13