-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (75 loc) · 2.85 KB
/
main.cpp
File metadata and controls
95 lines (75 loc) · 2.85 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
#include "WorkerThreadStd.h"
#include "AsyncCallback.h"
#include "SysData.h"
#include "SysDataNoLock.h"
#include <iostream>
#include <chrono>
// @see https://github.com/endurodave/AsyncCallback
using namespace std;
WorkerThread workerThread1("WorkerThread1");
/// @brief Test client to get callbacks from SysData::SystemModeChangedCallback and
/// SysDataNoLock::SystemModeChangedCallback
class SysDataClient
{
public:
// Constructor
SysDataClient() :
m_numberOfCallbacks(0)
{
// Register for async callbacks
SysData::GetInstance().SystemModeChangedCallback.Register(&SysDataClient::CallbackFunction, &workerThread1, this);
SysDataNoLock::GetInstance().SystemModeChangedCallback.Register(&SysDataClient::CallbackFunction, &workerThread1, this);
}
~SysDataClient()
{
// Unregister the all registered callbacks at once
SysData::GetInstance().SystemModeChangedCallback.Clear();
// Alternatively unregister a single callback
SysDataNoLock::GetInstance().SystemModeChangedCallback.Unregister(&SysDataClient::CallbackFunction, &workerThread1, this);
}
private:
static void CallbackFunction(const SystemModeChanged& data, void* userData)
{
// The user data pointer oringates from the 3rd argument in the Register() function
// Typecast the void* to SysDataClient* to access object instance data/functions.
SysDataClient* instance = static_cast<SysDataClient*>(userData);
instance->m_numberOfCallbacks++;
cout << "CallbackFunction " << data.CurrentSystemMode << endl;
}
int m_numberOfCallbacks;
};
/// Simple free callback function
void SimpleCallback(const int& value, void* userData)
{
cout << "SimpleCallback " << value << endl;
}
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(void)
{
// Create the worker threads
workerThread1.CreateThread();
SysDataNoLock::GetInstance();
// Create async callback and register a function pointer
AsyncCallback<int> callback;
callback.Register(&SimpleCallback, &workerThread1);
// Asynchronously invoke the callback
callback(123);
callback.Invoke(123);
// Unregister the callback
callback.Unregister(&SimpleCallback, &workerThread1);
// Create a SysDataClient instance on the stack
SysDataClient sysDataClient;
// Set new SystemMode values. Each call will invoke callbacks to all
// registered client subscribers.
SysData::GetInstance().SetSystemMode(SystemMode::STARTING);
SysData::GetInstance().SetSystemMode(SystemMode::NORMAL);
// Set new SystemMode values for SysDataNoLock.
SysDataNoLock::GetInstance().SetSystemMode(SystemMode::SERVICE);
SysDataNoLock::GetInstance().SetSystemMode(SystemMode::SYS_INOP);
// Give time for callbacks to occur on worker threads
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
workerThread1.ExitThread();
return 0;
}