-
Notifications
You must be signed in to change notification settings - Fork 2
/
CallableWrapper.hpp
51 lines (41 loc) · 1.16 KB
/
CallableWrapper.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
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
#include <functional>
#include <memory>
#include <iostream>
template<int Ind, typename R, typename... Args>
struct Wrapper;
template <int Ind, typename Callable, typename R, typename... Args>
R wrapper(Args... args);
template<int Ind, typename Callable>
struct CallableStorage
{
static void store(Callable callable_)
{
callable = std::make_unique<Callable>(callable_);
}
template <typename R, typename... Args>
static R invoke(Args... args)
{
return (*callable)(args...);
}
private:
static std::unique_ptr<Callable> callable;
};
template <int Ind, typename Callable>
std::unique_ptr<Callable> CallableStorage<Ind, Callable>::callable;
template<int Ind, typename R, typename... Args>
struct Wrapper<Ind, R(Args...)>
{
using RawType = R(*)(Args...);
template<typename Callable>
static RawType wrap(Callable callable)
{
CallableStorage<Ind, Callable>::store(callable);
return wrapper<Ind, Callable, R, Args...>;
}
};
template <int Ind, typename Callable, typename R, typename... Args>
R wrapper(Args... args)
{
return CallableStorage<Ind, Callable>::template invoke<R>(args...);
}