-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_simple.cpp
More file actions
172 lines (137 loc) · 5.33 KB
/
example_simple.cpp
File metadata and controls
172 lines (137 loc) · 5.33 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @file example_simple.cpp
* @brief EventBus simple usage example
*/
#include "eventbus.hpp"
#include <iostream>
#include <string>
#include <vector>
using namespace eventbus;
// Simulate user management system
class UserManager
{
private:
EventBus& bus_;
public:
explicit UserManager(EventBus& bus) : bus_(bus)
{
// Subscribe to user-related events
bus_.subscribe("user_login", [this](const std::string& username) {
onUserLogin(username);
});
bus_.subscribe("user_logout", [this](const std::string& username) {
onUserLogout(username);
});
}
void onUserLogin(const std::string& username)
{
std::cout << "UserManager: " << username << " logged in" << std::endl;
// Publish user status change event
bus_.publish("user_status_changed", username, "online");
}
void onUserLogout(const std::string& username)
{
std::cout << "UserManager: " << username << " logged out" << std::endl;
bus_.publish("user_status_changed", username, "offline");
}
};
// Simulate notification system
class NotificationService
{
private:
EventBus& bus_;
public:
explicit NotificationService(EventBus& bus) : bus_(bus)
{
bus_.subscribe("user_status_changed", [this](const std::string& username, const std::string& status) {
sendNotification(username, status);
});
bus_.subscribe("system_alert", [this](const std::string& message, int priority) {
sendAlert(message, priority);
});
}
void sendNotification(const std::string& username, const std::string& status)
{
std::cout << "Notification: " << username << " status changed to " << status << std::endl;
}
void sendAlert(const std::string& message, int priority)
{
std::cout << "System Alert [Priority:" << priority << "] " << message << std::endl;
}
};
// Simulate logging system
class LogService
{
private:
EventBus& bus_;
public:
explicit LogService(EventBus& bus) : bus_(bus)
{
// Subscribe to all user events for logging
bus_.subscribe("user_login", [this](const std::string& username) {
log("INFO", "User " + username + " logged in");
});
bus_.subscribe("user_logout", [this](const std::string& username) {
log("INFO", "User " + username + " logged out");
});
bus_.subscribe("system_alert", [this](const std::string& message, int priority) {
log(priority >= 3 ? "ERROR" : "WARN", message);
});
}
void log(const std::string& level, const std::string& message)
{
std::cout << "[LOG-" << level << "] " << message << std::endl;
}
};
int main()
{
std::cout << "=== EventBus Practical Usage Example ===" << std::endl << std::endl;
// Create event bus (disable verbose logging for clean output)
EventBus bus(false);
// Create service components
UserManager userManager(bus);
NotificationService notificationService(bus);
LogService logService(bus);
std::cout << "1. Simulate user login/logout flow" << std::endl;
std::cout << "-----------------------------------" << std::endl;
// Simulate user operations
bus.publish("user_login", "Alice");
std::cout << std::endl;
bus.publish("user_login", "Bob");
std::cout << std::endl;
bus.publish("user_logout", "Alice");
std::cout << std::endl;
std::cout << "2. Simulate system alerts" << std::endl;
std::cout << "-------------------------" << std::endl;
bus.publish("system_alert", "Low disk space", 2);
std::cout << std::endl;
bus.publish("system_alert", "Database connection failed", 5);
std::cout << std::endl;
std::cout << "3. View event bus status" << std::endl;
std::cout << "------------------------" << std::endl;
auto stats = bus.getStats();
std::cout << "Total event types: " << stats.total_events << std::endl;
std::cout << "Total callbacks: " << stats.total_callbacks << std::endl;
std::cout << "Most popular event: " << stats.most_subscribed_event << std::endl;
auto events = bus.getAllEventNames();
std::cout << "Registered events: ";
for (const auto& event : events) {
std::cout << event << "(" << bus.getCallbackCount(event) << ") ";
}
std::cout << std::endl << std::endl;
std::cout << "4. Demonstrate type conversion feature" << std::endl;
std::cout << "--------------------------------------" << std::endl;
// Subscribe to an event that requires std::string parameter
bus.subscribe("string_event", [](const std::string& msg) {
std::cout << "Received string: " << msg << std::endl;
});
// Publish using const char*, will auto-convert to std::string
bus.publish("string_event", "This is a string literal");
std::cout << std::endl << "=== Example Complete ===" << std::endl;
std::cout << "This example demonstrates:" << std::endl;
std::cout << " Loose coupling between components" << std::endl;
std::cout << " Event-driven architecture implementation" << std::endl;
std::cout << " Automatic type conversion functionality" << std::endl;
std::cout << " System status monitoring capabilities" << std::endl;
return 0;
}