-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
82 lines (65 loc) · 2.19 KB
/
main.cpp
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
#include <iostream>
#include <list>
#include <memory>
#include "signal.hpp"
#include "core/task_queue.h"
#include "core/task_queue_manager.h"
struct DeviceInfo {
std::string deviceId;
std::string deviceName;
};
// Slot object with shared_ptr
class UiController1 {
public:
~UiController1() {}
void onDevicePlugged(const std::shared_ptr<DeviceInfo>& info) {
std::cout << "onDevicePlugged: " << info->deviceName << std::endl;
}
};
// Slot object with observer
class UiController2 : public sigslot::observer {
public:
~UiController2() {
// Needed to ensure proper disconnection prior to object destruction in multithreaded contexts.
this->disconnect_all();
}
void onDevicePlugged(const std::shared_ptr<DeviceInfo>& info) {
std::cout << "onDevicePlugged: " << info->deviceName << std::endl;
}
};
class DeviceController {
public:
std::list<std::shared_ptr<DeviceInfo>> getDeviceList();
void mockCallback() {
auto deviceInfo = std::make_shared<DeviceInfo>();
deviceInfo->deviceId = "uuid-12345678900987654321";
deviceInfo->deviceName = "microphone";
onDeviceEventTriggered(deviceInfo);
}
private:
void onDeviceEventTriggered(const std::shared_ptr<DeviceInfo>& info) {
pluggedSignal(info);
}
public:
sigslot::signal<const std::shared_ptr<DeviceInfo>&> pluggedSignal;
};
int main()
{
// create a task queue
TQMgr->create({"worker"});
auto dc = std::make_shared<DeviceController>();
// example 1, slot object with shared_ptr
auto ui1 = std::make_shared<UiController1>();
dc->pluggedSignal.connect(ui1.get(), &UiController1::onDevicePlugged, sigslot::connection_type::auto_connection, TQ("worker"));
// example 2, slot object with observer
UiController2 ui2;
dc->pluggedSignal.connect(&ui2, &UiController2::onDevicePlugged, sigslot::connection_type::queued_connection, TQ("worker"));
dc->mockCallback();
// expamle 3
sigslot::signal<int> printSignal;
printSignal.connect([](int x){
std::cout << "Hello World: " << x << std::endl;
}, sigslot::connection_type::blocking_queued_connection, TQ("worker"));
printSignal(5);
return 0;
}