-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGioDelegateExamples.cpp
More file actions
106 lines (79 loc) · 2.05 KB
/
Copy pathGioDelegateExamples.cpp
File metadata and controls
106 lines (79 loc) · 2.05 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
// Copyright Giovanni Tramarin Zambiasi 2023
#include <iostream>
#include "Delegate.h"
class Foo
{
public:
void DoFoo()
{
std::cout << "Do Foo!" << std::endl;
}
void DoSpecialFoo(int x)
{
std::cout << "Do special foo " << x << "!" << std::endl;
}
static void DoStaticFoo()
{
std::cout << "Do static foo!" << std::endl;
}
};
void GlobalFoo()
{
std::cout << "Do global foo!" << std::endl;
}
class Base{};
class Child : public Base{};
int main()
{
{
Foo foo{};
Gio::Delegate<> del{};
del.Bind(&GlobalFoo);
del.BindMember(foo,&Foo::DoFoo);
del.Clear();
del();
}
// Simple member function
{
Foo foo{};
Gio::Delegate<> del{}; // From C++17 onwards, the "<>" can be omitted
del.BindMember(foo, &Foo::DoFoo);
del(); // The delegates are callable!
Gio::SimpleDelegate delAlternative; // For versions earlier than C++17, this alternative syntax can be used
delAlternative.BindMember(foo, &Foo::DoFoo);
delAlternative();
}
// Specialized member function
{
Foo foo{};
Gio::Delegate<int> del{};
del.BindMember(foo, &Foo::DoSpecialFoo);
del(10);
typedef Gio::Delegate<int> MySpecialDelegate; // You can also typedef your own specialized delegates!
MySpecialDelegate specialDelAlternative{};
specialDelAlternative.BindMember(foo, &Foo::DoSpecialFoo);
del(5);
}
// Static member function
{
Gio::Delegate<> del{};
del.Bind(&Foo::DoStaticFoo);
del();
}
// Lambda
{
Gio::Delegate<int> del{};
del.Bind([](int bar)
{
std::cout << "Do specialized lambda " << bar << "!" << std::endl;
});
del(10);
}
// Global function
{
Gio::Delegate<> del{};
del.Bind(&GlobalFoo);
del();
}
std::getchar();
}