-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cc
More file actions
67 lines (54 loc) · 1.44 KB
/
example.cc
File metadata and controls
67 lines (54 loc) · 1.44 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
#include <iostream>
#include <string>
#include "callback.hh"
class Button {
public:
Button(const NCallback<void, int>& cb)
: notify(cb)
{
}
void click(int x = 5)
{
notify(x);
}
private:
NCallback<void, int> notify;
};
class AnotherButton {
public:
AnotherButton(const NCallback<int, int, float, std::string>& cb)
: notify(cb)
{
}
int click(int x = 5, float y = 5.5, std::string z = "Hello World")
{
notify(x, y, z);
return 42;
}
private:
NCallback<int, int, float, std::string> notify;
};
class MemberCallback {
public:
void buttonCB(int x) { std::cout << "Callback called with x = " << x << std::endl; }
int anotherButtonCB(int x, float y, std::string z)
{
std::cout << "Callback called with x = " << x << " y = " << y << " z = " << z << std::endl;
return 42;
}
};
void funcCallback(int x)
{
std::cout << "Callback called with x = " << x << std::endl;
}
int main()
{
MemberCallback cbClass;
Button b1(NGenerateCallback((NCallback<void, int>*)0, cbClass, &MemberCallback::buttonCB));
AnotherButton b2(NGenerateCallback((NCallback<int, int, float, std::string>*)0, cbClass, &MemberCallback::anotherButtonCB));
Button b3(NGenerateCallback((NCallback<void, int>*)0, &funcCallback));
b1.click(532);
std::cout << "With return: " << b2.click(42, 43.5, "Hello Callback") << std::endl;
b3.click();
return 0;
}