forked from ZolotovPavel/EventHandling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
141 lines (115 loc) · 3.24 KB
/
test.cpp
File metadata and controls
141 lines (115 loc) · 3.24 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
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
#include <iostream>
#include <functional>
#include "eventhandling.hpp"
using namespace events;
class Foo
{
public:
Foo() :
onMake( m_onMake ),
m_onMake(),
m_onMakeInner(),
m_makeCount( 0 )
{
m_onMakeInner += FUNCTOR_HANDLER( m_onMake );
}
IEvent<unsigned int>& onMake;
void make()
{
m_onMakeInner( m_makeCount++ );
}
private:
TEvent<unsigned int> m_onMake, m_onMakeInner;
unsigned int m_makeCount;
};
namespace instances
{
Foo& getFoo()
{
static Foo _foo;
return _foo;
}
} // instances
struct FunctorHandler
{
int operator()( unsigned int makeCount );
};
int functionHandler( unsigned int makeCount );
class ClassHandler
{
public:
int handle( unsigned int makeCount );
};
namespace instances
{
FunctorHandler& getFunctorHandler()
{
static FunctorHandler _functorHandler;
return _functorHandler;
}
std::function<int( unsigned int )>& getStdFunctionHandler()
{
static std::function<int( unsigned int )> _stdFunctionHandler = []( unsigned int makeCount )
{
std::cout << "It's std::function handler" << std::endl;
if( makeCount >= 2 )
instances::getFoo().onMake -= STD_FUNCTION_HANDLER( instances::getStdFunctionHandler() );
return 7;
};
return _stdFunctionHandler;
}
ClassHandler& getClassHandler()
{
static ClassHandler _classHandler;
return _classHandler;
}
} // instances
int FunctorHandler::operator()( unsigned int makeCount )
{
std::cout << "It's functor handler" << std::endl;
if( makeCount >= 0 )
instances::getFoo().onMake -= FUNCTOR_HANDLER( instances::getFunctorHandler() );
return 6;
}
int functionHandler( unsigned int makeCount )
{
std::cout << "It's function handler" << std::endl;
if( makeCount >= 3 )
instances::getFoo().onMake -= FUNCTION_HANDLER( functionHandler );
return 9;
}
int ClassHandler::handle( unsigned int makeCount )
{
std::cout << "It's method handler" << std::endl;
if( makeCount >= 4 )
instances::getFoo().onMake -= MY_METHOD_HANDLER( ClassHandler::handle );
return 74;
}
int main( int argc, char* argv[] )
{
Foo& foo = instances::getFoo();
auto lambdaHandler = []( unsigned int )
{
std::cout << "It's lambda handler" << std::endl;
return 12;
};
foo.onMake += FUNCTOR_HANDLER( instances::getFunctorHandler() );
foo.onMake += LAMBDA_HANDLER( lambdaHandler );
EventJoin lambdaJoin = foo.onMake += LAMBDA_HANDLER( ( [ &foo, &lambdaHandler ]( unsigned int makeCount )
{
if( makeCount >= 1 )
foo.onMake -= LAMBDA_HANDLER( lambdaHandler );
return 78;
} ) );
foo.onMake += STD_FUNCTION_HANDLER( instances::getStdFunctionHandler() );
foo.onMake += FUNCTION_HANDLER( functionHandler );
foo.onMake += METHOD_HANDLER( instances::getClassHandler(), ClassHandler::handle );
for( int i = 0; i < 6; ++i )
{
std::cout << "Make " << i << " time:" << std::endl;
foo.make();
std::cout << std::endl;
}
lambdaJoin.unjoin();
return 0;
}