Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
driven_table_example.C
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
47#include <iostream>
48#include <string>
49#include <vector>
50#include <driven_table.H>
51
52using namespace std;
53
54// =============================================================================
55// Part 1: Legacy Example (void* interface for backward compatibility)
56// =============================================================================
57
58// =============================================================================
59// Simulation Context
60// =============================================================================
61
63{
64 bool lights_on = false;
65 int temperature = 22; // Celsius
66 bool alarm_armed = false;
67 string last_log;
68
69 void log(const string & msg)
70 {
71 last_log = msg;
72 cout << "[SmartHome] " << msg << endl;
73 }
74};
75
76// =============================================================================
77// Event Handlers
78// =============================================================================
79
80// Event: Turn Lights On
81void * on_lights_on(void *data)
82{
83 auto *state = static_cast<SmartHomeState *>(data);
84 state->lights_on = true;
85 state->log("Lights turned ON");
86 return nullptr;
87}
88
89// Event: Turn Lights Off
90void * on_lights_off(void *data)
91{
92 auto *state = static_cast<SmartHomeState *>(data);
93 state->lights_on = false;
94 state->log("Lights turned OFF");
95 return nullptr;
96}
97
98// Event: Increase Temperature
99void * on_temp_up(void *data)
100{
101 auto *state = static_cast<SmartHomeState *>(data);
102 state->temperature++;
103 state->log("Temperature increased to " + to_string(state->temperature) + "C");
104 return nullptr;
105}
106
107// Event: Arm Alarm
108void * on_arm_alarm(void *data)
109{
110 auto *state = static_cast<SmartHomeState *>(data);
111 state->alarm_armed = true;
112 state->log("Security System ARMED");
113 return nullptr;
114}
115
116// Event: Emergency (Panic Button)
117void * on_panic(void *data)
118{
119 auto *state = static_cast<SmartHomeState *>(data);
120 state->lights_on = true;
121 state->alarm_armed = true;
122 state->log("PANIC! Lights ON, Alarm ARMED, Police Notified!");
123 return nullptr;
124}
125
126// =============================================================================
127// Legacy Simulation
128// =============================================================================
129
131{
132 cout << "\n=== LEGACY EXAMPLE: Smart Home Event Simulation (void* interface) ===" << endl;
133
134 // 1. Create the Event Table (using legacy alias)
136
137 // 2. Register Events
138 // The table assigns a unique ID to each registered event.
139 cout << "Registering events..." << endl;
140
141 size_t EVT_LIGHTS_ON = event_system.register_event(on_lights_on);
142 size_t EVT_LIGHTS_OFF = event_system.register_event(on_lights_off);
143 size_t EVT_TEMP_UP = event_system.register_event(on_temp_up);
144 size_t EVT_ARM_ALARM = event_system.register_event(on_arm_alarm);
145 size_t EVT_PANIC = event_system.register_event(on_panic);
146
147 cout << "Events registered. Total events: " << event_system.size() << endl;
148 cout << "-----------------------------------" << endl;
149
150 // 3. Initialize System State
152
153 // 4. Simulate a sequence of actions (Event Loop)
154 struct Action
155 {
156 size_t event_id;
157 string description;
158 };
159
161 {EVT_LIGHTS_ON, "User arrives home"},
162 {EVT_TEMP_UP, "User feels cold"},
163 {EVT_TEMP_UP, "User feels still cold"},
164 {EVT_LIGHTS_OFF, "User goes to bed"},
165 {EVT_ARM_ALARM, "User arms security"},
166 {EVT_PANIC, "Intruder detected!"}
167 };
168
169 for (const auto & [event_id, description]: scenario)
170 {
171 cout << "\n> Scenario: " << description << endl;
172
173 // Execute the event, passing the shared state
174 event_system.execute_event(event_id, &home);
175
176 // Verify state (optional)
177 if (event_id == EVT_PANIC)
178 if (home.lights_on && home.alarm_armed)
179 cout << " (System responded correctly to panic)" << endl;
180 }
181
182 cout << "\n=== Legacy Example Finished ===" << endl;
183}
184
185
186// =============================================================================
187// Part 2: Modern Example (Type-Safe Templates with Lambdas)
188// =============================================================================
189
190// State for modern example
192{
193 bool lights_on = false;
194 int temperature = 22;
195 bool alarm_armed = false;
196
197 void log(const string & msg)
198 {
199 cout << "[ModernHome] " << msg << endl;
200 }
201};
202
204{
205 cout << "\n=== MODERN EXAMPLE: Type-Safe Event System with Lambdas ===" << endl;
206
208
209 // Create type-safe event tables for different event signatures
210
211 // 1. Events that modify state (void return, take state reference)
213
214 auto EVT_LIGHTS_ON = state_events.register_event([](ModernSmartHome & h)
215 {
216 h.lights_on = true;
217 h.log("Lights turned ON (lambda)");
218 });
219
220 auto EVT_LIGHTS_OFF = state_events.register_event([](ModernSmartHome & h)
221 {
222 h.lights_on = false;
223 h.log("Lights turned OFF (lambda)");
224 });
225
226 auto EVT_TEMP_UP = state_events.register_event([](ModernSmartHome & h)
227 {
228 h.temperature++;
229 h.log("Temperature increased to " + to_string(h.temperature) +
230 "C (lambda)");
231 });
232
233 auto EVT_ARM_ALARM = state_events.register_event([](ModernSmartHome & h)
234 {
235 h.alarm_armed = true;
236 h.log("Security System ARMED (lambda)");
237 });
238
239 // 2. Events that query state (return bool, take const reference)
241
242 auto QUERY_IS_SECURE = query_events.register_event([](const ModernSmartHome & h)
243 {
244 return h.alarm_armed && ! h.lights_on;
245 });
246
247 auto QUERY_IS_COMFORTABLE = query_events.register_event([](const ModernSmartHome & h)
248 {
249 return h.temperature >= 20 && h.temperature <= 24;
250 });
251
252 // 3. Events with multiple parameters (temperature control)
254
255 auto EVT_SET_TEMP = temp_control_events.register_event([](ModernSmartHome & h, int target)
256 {
257 h.temperature = target;
258 h.log("Temperature set to " + to_string(target) +
259 "C (lambda with param)");
260 });
261
262 // Run simulation scenario
263 cout << "\n--- Scenario: Evening Routine ---" << endl;
264
265 state_events.execute_event(EVT_LIGHTS_ON, home);
266 cout << "Is secure? " << (query_events.execute_event(QUERY_IS_SECURE, home) ? "Yes" : "No") << endl;
267
268 temp_control_events.execute_event(EVT_SET_TEMP, home, 23);
269 cout << "Is comfortable? " << (query_events.execute_event(QUERY_IS_COMFORTABLE, home) ? "Yes" : "No") << endl;
270
271 state_events.execute_event(EVT_LIGHTS_OFF, home);
272 state_events.execute_event(EVT_ARM_ALARM, home);
273
274 cout << "Is secure? " << (query_events.execute_event(QUERY_IS_SECURE, home) ? "Yes" : "No") << endl;
275
276 // Demonstrate stateful lambdas
277 cout << "\n--- Advanced: Stateful Event (Counter) ---" << endl;
278
279 int event_count = 0;
281
282 auto EVT_COUNT = simple_events.register_event([&event_count]()
283 {
284 event_count++;
285 cout << "Event executed " << event_count << " time(s)" << endl;
286 });
287
288 simple_events.execute_event(EVT_COUNT);
289 simple_events.execute_event(EVT_COUNT);
290 simple_events.execute_event(EVT_COUNT);
291
292 cout << "Total event executions: " << event_count << endl;
293
294 cout << "\n=== Modern Example Finished ===" << endl;
295}
296
297
298// =============================================================================
299// Main
300// =============================================================================
301
302int main()
303{
304 // Run both examples to show legacy and modern usage
307
308 cout << "\n=== All Examples Completed ===" << endl;
309 return 0;
310}
long double h
Definition btreepic.C:154
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Dynamic (growable) event table implementation.
Event-driven table abstraction for event-driven simulations.
void * on_lights_on(void *data)
void * on_lights_off(void *data)
void * on_temp_up(void *data)
void run_legacy_example()
void run_modern_example()
void * on_arm_alarm(void *data)
void * on_panic(void *data)
int main()
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
void log(const string &msg)
void log(const string &msg)