-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_function.h
56 lines (42 loc) · 1.28 KB
/
basic_function.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Created by konstantin on 19.06.24.
//
#pragma once
#include <memory>
namespace NComponents {
template <class U>
class BasicFunction;
template <typename R, typename... Args>
class BasicFunction<R(Args...)> {
class function_holder_base;
using function_holder_base_ptr = std::shared_ptr<function_holder_base>;
function_holder_base_ptr invoker;
public:
BasicFunction() = default;
template <typename F>
BasicFunction(F f) : invoker(new free_function_holder(std::move(f))) {}
R operator()(Args... args) {
return invoker->invoke(std::forward<Args...>(args)...);
}
explicit operator bool() const { return invoker.operator bool(); }
private:
class function_holder_base {
public:
function_holder_base() = default;
virtual ~function_holder_base() = default;
virtual R invoke(Args...) = 0;
};
template <typename F>
class free_function_holder : public function_holder_base {
public:
explicit free_function_holder(F f)
: function_holder_base(), func(std::move(f)) {}
~free_function_holder() override = default;
R invoke(Args... args) override {
return func(std::forward<Args...>(args)...);
}
private:
F func;
};
};
} // namespace NComponents