-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP517.FunctionPtr.cpp
187 lines (176 loc) · 4.5 KB
/
P517.FunctionPtr.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <concepts>
#include <exception>
template<typename R, typename... Args>
class FunctionBridge
{
public:
virtual ~FunctionBridge() {}
virtual FunctionBridge* clone() const = 0;
virtual R invoke(Args... args) const = 0;
virtual bool equals(const FunctionBridge* fb) const = 0;
};
// exception of no equality comparison operator
class NotEuqalityComparable : public std::exception {};
// tryto compare Functor with SFINAE, how to change to requires syntax? is there a way to do that?
template<typename T, bool = std::equality_comparable<T>>
struct TryEquals
{
static bool equals(const T& x1, const T& x2)
{
return x1 == x2;
}
};
template<typename T>
struct TryEquals<T, false>
{
static bool equals(const T& x1, const T& x2)
{
throw NotEuqalityComparable();
}
};
// specific bridge of FunctionBridge
template<typename Functor, typename R, typename... Args>
class SpecificFunctionBridge : public FunctionBridge<R, Args...>
{
private:
Functor functor;
public:
template<typename FunctorFwd>
SpecificFunctionBridge(FunctorFwd&& _functor) : functor(std::forward<FunctorFwd>(_functor)) {}
virtual SpecificFunctionBridge* clone() const override
{
return new SpecificFunctionBridge(functor);
}
virtual R invoke(Args... args) const override
{
return functor(std::forward<Args>(args)...);
}
virtual bool equals(const FunctionBridge<R, Args...>* fb) const override
{
if (auto specFb = dynamic_cast<const SpecificFunctionBridge*>(fb))
{
return TryEquals<Functor>::equals(functor, specFb->functor);
}
return false; // functor with different types are never equal
}
};
template<typename Signature>
class FunctionPtr;
template<typename R, typename... Args>
class FunctionPtr<R(Args...)>
{
private:
FunctionBridge<R, Args...>* bridge;
public:
// constructors
FunctionPtr() : bridge(nullptr) {}
FunctionPtr(const FunctionPtr& other)
{
if (other.bridge)
{
bridge = other.bridge->clone();
}
}
FunctionPtr(FunctionPtr& other) : FunctionPtr(static_cast<const FunctionPtr&>(other)) {}
FunctionPtr(FunctionPtr&& other) : bridge(other.bridge)
{
other.bridge = nullptr;
}
template<typename F>
FunctionPtr(F&& f) : bridge(nullptr)
{
using Functor = std::decay_t<F>;
using Bridge = SpecificFunctionBridge<Functor, R, Args...>;
bridge = new Bridge(std::forward<F>(f));
}
// assignment
FunctionPtr& operator=(const FunctionPtr& other)
{
FunctionPtr tmp(other);
swap(*this, tmp);
return *this;
}
FunctionPtr& operator=(FunctionPtr&& other)
{
delete bridge;
bridge = other.bridge;
other.bridge = nullptr;
return *this;
}
template<typename F>
FunctionPtr& operator=(F&& f)
{
FunctionPtr tmp(std::forward<F>(f));
swap(*this, tmp);
return *this;
}
// destructor
~FunctionPtr()
{
delete bridge;
}
friend void swap(FunctionPtr& fp1, FunctionPtr& fp2)
{
std::swap(fp1.bridge, fp2.bridge);
}
explicit operator bool() const
{
return bridge != nullptr;
}
R operator()(Args... args) const
{
return bridge->invoke(std::forward<Args>(args)...);
}
friend bool operator==(const FunctionPtr& f1, const FunctionPtr& f2)
{
if (!f1 || !f2)
{
return !f1 && !f2; // both are empty
}
return f1.bridge->equals(f2.bridge);
}
friend bool operator!=(const FunctionPtr& f1, const FunctionPtr& f2)
{
return !(f1 == f2);
}
};
void foo()
{
std::cout << "foo()" << std::endl;
}
class A
{
public:
bool operator==(const A&) const
{
return true;
}
void operator()() const
{
std::cout << "A::operator()()" << std::endl;
}
static void foo()
{
std::cout << "A::foo()" << std::endl;
}
};
int main(int argc, char const *argv[])
{
auto bar = []() -> void {
std::cout << "bar()" << std::endl;
};
auto f1 = FunctionPtr<void()>(foo);
f1();
auto f2 = FunctionPtr<void()>(A::foo);
f2();
auto f3 = FunctionPtr<void()>(A());
f3();
auto f4 = FunctionPtr<void()>(bar);
f4();
std::cout << std::boolalpha;
std::cout << (f1 == f2) << std::endl;
std::cout << (f3 == f4) << std::endl;
std::cout << (f3 == FunctionPtr<void()>(A())) << std::endl;
return 0;
}