69 void log(
const string & msg)
72 cout <<
"[SmartHome] " << msg << endl;
85 state->log(
"Lights turned ON");
94 state->log(
"Lights turned OFF");
103 state->log(
"Temperature increased to " +
to_string(state->temperature) +
"C");
112 state->log(
"Security System ARMED");
121 state->alarm_armed =
true;
122 state->log(
"PANIC! Lights ON, Alarm ARMED, Police Notified!");
132 cout <<
"\n=== LEGACY EXAMPLE: Smart Home Event Simulation (void* interface) ===" << endl;
139 cout <<
"Registering events..." << endl;
147 cout <<
"Events registered. Total events: " << event_system.
size() << endl;
148 cout <<
"-----------------------------------" << endl;
160 vector<Action> scenario = {
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!"}
169 for (
const auto & [event_id, description]: scenario)
171 cout <<
"\n> Scenario: " << description << endl;
177 if (event_id == EVT_PANIC)
179 cout <<
" (System responded correctly to panic)" << endl;
182 cout <<
"\n=== Legacy Example Finished ===" << endl;
197 void log(
const string & msg)
199 cout <<
"[ModernHome] " << msg << endl;
205 cout <<
"\n=== MODERN EXAMPLE: Type-Safe Event System with Lambdas ===" << endl;
217 h.log(
"Lights turned ON (lambda)");
223 h.log(
"Lights turned OFF (lambda)");
229 h.log(
"Temperature increased to " +
to_string(
h.temperature) +
235 h.alarm_armed =
true;
236 h.log(
"Security System ARMED (lambda)");
244 return h.alarm_armed && !
h.lights_on;
249 return h.temperature >= 20 &&
h.temperature <= 24;
257 h.temperature = target;
258 h.log(
"Temperature set to " +
to_string(target) +
259 "C (lambda with param)");
263 cout <<
"\n--- Scenario: Evening Routine ---" << endl;
266 cout <<
"Is secure? " << (query_events.
execute_event(QUERY_IS_SECURE, home) ?
"Yes" :
"No") << endl;
269 cout <<
"Is comfortable? " << (query_events.
execute_event(QUERY_IS_COMFORTABLE, home) ?
"Yes" :
"No") << endl;
274 cout <<
"Is secure? " << (query_events.
execute_event(QUERY_IS_SECURE, home) ?
"Yes" :
"No") << endl;
277 cout <<
"\n--- Advanced: Stateful Event (Counter) ---" << endl;
285 cout <<
"Event executed " << event_count <<
" time(s)" << endl;
292 cout <<
"Total event executions: " << event_count << endl;
294 cout <<
"\n=== Modern Example Finished ===" << endl;
308 cout <<
"\n=== All Examples Completed ===" << endl;
Dynamic (growable) event table implementation.
size_t size() const override
Get the current table size (implements Event_Table virtual method)
void register_event(const size_t index, Callable &&fct)
Register an event at a specific index.
auto execute_event(const size_t index, Args &&... args) const
Execute the event at the given index with type-safe arguments.
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)
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
void log(const string &msg)
void log(const string &msg)