-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveOnlyFunctionWrapper.hpp
More file actions
38 lines (29 loc) · 1.02 KB
/
Copy pathMoveOnlyFunctionWrapper.hpp
File metadata and controls
38 lines (29 loc) · 1.02 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
#include <memory>
class move_only_function_wrapper {
struct impl_base {
virtual void call() = 0;
virtual ~impl_base() {};
};
std::unique_ptr<impl_base> impl;
template <typename F>
struct impl_type: impl_base {
F f;
impl_type(F&& f_): f(std::move(f_)) {}
void call() { f(); }
};
public:
move_only_function_wrapper() = default;
template <typename F>
move_only_function_wrapper(F&& f): impl(new impl_type<F>(std::move(f)))
{}
move_only_function_wrapper(move_only_function_wrapper&& other) :
impl(std::move(other.impl))
{}
move_only_function_wrapper& operator=(move_only_function_wrapper&& other) {
impl = std::move(other.impl);
return *this;
}
void operator() () {impl->call();}
move_only_function_wrapper(const move_only_function_wrapper&) = delete;
move_only_function_wrapper& operator=(const move_only_function_wrapper&) = delete;
};