-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional.hpp
38 lines (33 loc) · 912 Bytes
/
functional.hpp
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
#ifndef _ASP_FUNCTIONAL_HPP_
#define _ASP_FUNCTIONAL_HPP_
#include <memory>
#include <utility>
namespace asp {
template<typename _T> class function;
template<typename _R, typename... _Args> class function<_R(_Args...)> {
public:
template<typename _T> function(_T&& t):
_call(std::make_unique<_Callable<_T>>(t)) {}
_R operator()(_Args&&... _args) {
return _call->invoke(std::forward<_Args>(_args)...);
}
private:
class _Base {
public:
virtual ~_Base() = default;
virtual _R invoke(_Args&&... _args) = 0;
};
template<typename _T> class _Callable : public _Base {
public:
_Callable(const _T& t): _t(t) {}
~_Callable() override = default;
_R invoke(_Args&&... _args) override {
return _t(std::forward<_Args>(_args)...);
}
private:
_T _t;
};
std::unique_ptr<_Base> _call;
};
};
#endif