libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
function.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <functional>
14#include <type_traits>
15#include <utility>
16#include <promeki/namespace.h>
17
18PROMEKI_NAMESPACE_BEGIN
19
55template <typename Sig> class Function;
56
64template <typename R, typename... Args> class Function<R(Args...)> {
65 public:
67 using DataType = std::function<R(Args...)>;
68
70 using ResultType = R;
71
73 Function() = default;
74
76 Function(std::nullptr_t) noexcept {}
77
89 template <typename F,
90 typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, Function> &&
91 std::is_invocable_r_v<R, F &, Args...>>>
92 Function(F &&f) : d(std::forward<F>(f)) {}
93
98 Function(const DataType &f) : d(f) {}
99
104 Function(DataType &&f) noexcept : d(std::move(f)) {}
105
107 Function(const Function &other) = default;
108
110 Function(Function &&other) noexcept = default;
111
113 ~Function() = default;
114
116 Function &operator=(const Function &other) = default;
117
119 Function &operator=(Function &&other) noexcept = default;
120
122 Function &operator=(std::nullptr_t) noexcept {
123 d = nullptr;
124 return *this;
125 }
126
133 template <typename F,
134 typename = std::enable_if_t<!std::is_same_v<std::decay_t<F>, Function> &&
135 std::is_invocable_r_v<R, F &, Args...>>>
136 Function &operator=(F &&f) {
137 d = std::forward<F>(f);
138 return *this;
139 }
140
149 R operator()(Args... args) const { return d(std::forward<Args>(args)...); }
150
155 explicit operator bool() const noexcept { return static_cast<bool>(d); }
156
158 bool isNull() const noexcept { return !d; }
159
161 void clear() noexcept { d = nullptr; }
162
164 void swap(Function &other) noexcept { d.swap(other.d); }
165
167 const DataType &toStdFunction() const noexcept { return d; }
168
170 DataType &toStdFunction() noexcept { return d; }
171
173 friend bool operator==(const Function &f, std::nullptr_t) noexcept { return !f.d; }
174
176 friend bool operator==(std::nullptr_t, const Function &f) noexcept { return !f.d; }
177
179 friend bool operator!=(const Function &f, std::nullptr_t) noexcept { return static_cast<bool>(f.d); }
180
182 friend bool operator!=(std::nullptr_t, const Function &f) noexcept { return static_cast<bool>(f.d); }
183
184 private:
185 DataType d;
186};
187
188PROMEKI_NAMESPACE_END
189
190#endif // PROMEKI_ENABLE_CORE