@nonedark2008
1002 / 742 / 170
Регистрация: 28.07.2012
Сообщений: 2,055
|
01.07.2016, 18:55
|
|
rikimaru2013, ну тогда что-то такое:
C++ | 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
| #include <iostream>
#include <functional>
#include <type_traits>
using namespace std;
//////////////////////////////////////////////////////////////////////////
class IFoo
{
public:
virtual void onAppStarted(int, int) = 0;
};
//////////////////////////////////////////////////////////////////////////
template <typename T>
struct FunctionTypeDeduction : public FunctionTypeDeduction<decltype(&T::operator())>
{};
//////////////////////////////////////////////////////////////////////////
template<class ClassType, class ReturnType, class... Args>
struct FunctionTypeDeduction< ReturnType(ClassType::*)(Args...) >
{
using type = ReturnType(*)(Args...);
using functorType = std::function<ReturnType(Args...) >;
};
//////////////////////////////////////////////////////////////////////////
template<class ClassType, class ReturnType, class... Args>
struct FunctionTypeDeduction< ReturnType(ClassType::*)(Args...) const> : FunctionTypeDeduction< ReturnType(ClassType::*)(Args...)>
{};
//////////////////////////////////////////////////////////////////////////
class SubcribeSystem
{
public:
using tSomeMethodType = FunctionTypeDeduction< decltype(&IFoo::onAppStarted) >::type;
using tSomeMethodCallback = FunctionTypeDeduction< decltype(&IFoo::onAppStarted) >::functorType;
public:
void subribeOnSomeMethod(const tSomeMethodCallback& arr)
{
arr(3, 19);
}
};
#define SUBCRIBE_FOR_SOMEMETHOD( eventsObj, lambda ) \
do \
{ \
static_assert(is_same<SubcribeSystem::tSomeMethodType, FunctionTypeDeduction<decltype(lambda)>::type>::value, "bad type"); \
SubcribeSystem::tSomeMethodCallback const func_ptr = lambda; \
eventsObj.subribeOnSomeMethod( lambda ); \
} \
while(false);
//////////////////////////////////////////////////////////////////////////
class Bar
{
public:
void fn(SubcribeSystem& events)
{
auto onAppStart = [](int width, int height)
{
cout << width << " " << height << endl;
};
auto onAppStart2 = [](bool width, int height)
{
cout << width << " " << height << endl;
};
auto onAppStart3 = [this](int width, int height)
{
cout << width << " " << height << endl;
someOtherMethod(width);
};
SUBCRIBE_FOR_SOMEMETHOD(events, onAppStart); // expect ok
SUBCRIBE_FOR_SOMEMETHOD(events, onAppStart2); // expect error
SUBCRIBE_FOR_SOMEMETHOD(events, onAppStart3); // expect ok
}
void someOtherMethod(int x)
{
cout << "processed callback " << x << endl;
}
};
int main()
{
Bar bar;
SubcribeSystem sub;
bar.fn(sub);
} |
|
Как дедуцировать тип лямбды я честно нарыл в интернете...
2
|