-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionWrapper.hpp
More file actions
80 lines (65 loc) · 1.94 KB
/
FunctionWrapper.hpp
File metadata and controls
80 lines (65 loc) · 1.94 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
#ifndef _LIBRARY_UTILITIES_FUNCTIONWRAPPER_HPP
#define _LIBRARY_UTILITIES_FUNCTIONWRAPPER_HPP
#include <functional>
#include <memory>
#include <utility>
namespace Utilities
{
class FunctionWrapper
{
struct ICallable
{
virtual void call() = 0;
virtual ~ICallable() = default;
};
template<typename T>
struct CallableImpl : ICallable
{
explicit CallableImpl(T&& f)
: callable(std::move(f))
{
}
inline void call() override
{
callable();
}
private:
T callable;
};
std::unique_ptr<ICallable> pImpl;
public:
/*
* perhaps, this implementation can act as type-erased wrapper for
* functions with arguments
* */
template<typename F, typename... Args>
FunctionWrapper(F&& callable, Args&&... args)
: pImpl(std::make_unique<CallableImpl<F>>(
std::move(std::bind(std::forward<F>(callable), std::forward<Args>(args)...))))
{
}
template<typename F>
FunctionWrapper(F&& callable)
: pImpl(std::make_unique<CallableImpl<F>>(std::move(callable)))
{
}
// can't be 'default'-ed due to collision with argument-based ctor above
FunctionWrapper(FunctionWrapper&& other) noexcept
: pImpl(std::move(other.pImpl))
{
}
FunctionWrapper& operator=(FunctionWrapper&& other) noexcept
{
pImpl.swap(other.pImpl);
return *this;
}
FunctionWrapper(const FunctionWrapper&) = delete;
FunctionWrapper& operator=(const FunctionWrapper&) = delete;
~FunctionWrapper() = default;
void operator()()
{
pImpl->call();
}
};
} // namespace Utilities
#endif // !_LIBRARY_UTILITIES_FUNCTIONWRAPPER_HPP