-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNative.cpp
More file actions
113 lines (93 loc) · 1.83 KB
/
Native.cpp
File metadata and controls
113 lines (93 loc) · 1.83 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* \file Native.cpp
* \brief
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
template <typename T>
class NaiveFunction;
template <typename ReturnValue, typename... Args>
class NaiveFunction<ReturnValue(Args...)>
{
public:
template <typename T>
NaiveFunction& operator=(T t)
{
callable_ = std::make_unique<CallableT<T>>(t);
return *this;
}
ReturnValue operator()(Args... args) const
{
assert(callable_);
return callable_->Invoke(args...);
}
private:
class ICallable
{
public:
virtual ~ICallable() = default;
virtual ReturnValue Invoke(Args...) = 0;
};
template <typename T>
class CallableT :
public ICallable
{
public:
CallableT(const T& t) :
_t(t)
{
}
~CallableT() override = default;
ReturnValue Invoke(Args... args) override
{
return _t(args...);
}
private:
T _t {};
};
std::unique_ptr<ICallable> callable_ {};
};
//-------------------------------------------------------------------------------------------------
void func()
{
std::cout << "func" << std::endl;
}
//-------------------------------------------------------------------------------------------------
struct Functor
{
void operator()() const
{
std::cout << "Functor" << std::endl;
}
};
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
{
NaiveFunction<void()> f;
f = func;
f();
}
{
NaiveFunction<void()> f;
f = Functor();
f();
}
{
NaiveFunction<void()> f;
f = []() -> void
{
std::cout << "lambda" << std::endl;
};
f();
}
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
func
Functor
lambda
#endif