1+ #include " event_system.h"
2+ #include " common_events.h"
3+ #include " event_integration_example.h"
4+ #include < iostream>
5+ #include < thread>
6+ #include < chrono>
7+
8+ /* *
9+ * Complete demonstration of the generic event system capabilities
10+ */
11+ namespace nativeapi {
12+
13+ void DemonstrateGenericEventSystem () {
14+ std::cout << " === Generic Event System Demonstration ===" << std::endl;
15+
16+ // Get global event dispatcher
17+ auto & dispatcher = GetGlobalEventDispatcher ();
18+
19+ std::cout << " \n 1. Setting up event listeners..." << std::endl;
20+
21+ // Example 1: Type-specific callback listeners
22+ auto key_listener = AddScopedListener<KeyPressedEvent>(dispatcher,
23+ [](const KeyPressedEvent& event) {
24+ std::cout << " 🎹 Key pressed: " << event.GetKeycode () << std::endl;
25+ });
26+
27+ auto display_listener = AddScopedListener<DisplayAddedEvent>(dispatcher,
28+ [](const DisplayAddedEvent& event) {
29+ std::cout << " 🖥️ Display added" << std::endl;
30+ });
31+
32+ // Example 2: Multi-event observer
33+ UnifiedSystemEventHandler system_handler;
34+ auto key_pressed_id = dispatcher.AddListener <KeyPressedEvent>(&system_handler);
35+ auto key_released_id = dispatcher.AddListener <KeyReleasedEvent>(&system_handler);
36+ auto display_added_id = dispatcher.AddListener <DisplayAddedEvent>(&system_handler);
37+ auto display_removed_id = dispatcher.AddListener <DisplayRemovedEvent>(&system_handler);
38+ auto app_started_id = dispatcher.AddListener <ApplicationStartedEvent>(&system_handler);
39+
40+ // Example 3: Application lifecycle listeners
41+ auto app_exit_listener = AddScopedListener<ApplicationExitingEvent>(dispatcher,
42+ [](const ApplicationExitingEvent& event) {
43+ std::cout << " 🚪 Application exiting with code: " << event.GetExitCode () << std::endl;
44+ });
45+
46+ std::cout << " Total listeners registered: " << dispatcher.GetTotalListenerCount () << std::endl;
47+
48+ // Example 4: Start async processing
49+ std::cout << " \n 2. Starting asynchronous event processing..." << std::endl;
50+ dispatcher.Start ();
51+
52+ // Example 5: Synchronous event dispatch
53+ std::cout << " \n 3. Dispatching synchronous events..." << std::endl;
54+
55+ dispatcher.DispatchSync <ApplicationStartedEvent>();
56+
57+ // Create dummy display for demonstration
58+ Display dummy_display; // Assuming Display has default constructor
59+ dispatcher.DispatchSync <DisplayAddedEvent>(dummy_display);
60+
61+ dispatcher.DispatchSync <KeyPressedEvent>(65 ); // 'A'
62+ dispatcher.DispatchSync <KeyReleasedEvent>(65 );
63+
64+ // Example 6: Asynchronous event dispatch
65+ std::cout << " \n 4. Dispatching asynchronous events..." << std::endl;
66+
67+ dispatcher.DispatchAsync <KeyPressedEvent>(66 ); // 'B'
68+ dispatcher.DispatchAsync <KeyPressedEvent>(67 ); // 'C'
69+ dispatcher.DispatchAsync <KeyReleasedEvent>(66 );
70+ dispatcher.DispatchAsync <KeyReleasedEvent>(67 );
71+
72+ dispatcher.DispatchAsync <DisplayRemovedEvent>(dummy_display);
73+
74+ // Wait for async events to process
75+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
76+
77+ // Example 7: Custom event with complex data
78+ std::cout << " \n 5. Custom complex event..." << std::endl;
79+
80+ class CustomUserEvent : public TypedEvent <CustomUserEvent> {
81+ public:
82+ CustomUserEvent (const std::string& action, int user_id, bool success)
83+ : action_(action), user_id_(user_id), success_(success) {}
84+
85+ const std::string& GetAction () const { return action_; }
86+ int GetUserId () const { return user_id_; }
87+ bool IsSuccess () const { return success_; }
88+
89+ private:
90+ std::string action_;
91+ int user_id_;
92+ bool success_;
93+ };
94+
95+ auto custom_listener = AddScopedListener<CustomUserEvent>(dispatcher,
96+ [](const CustomUserEvent& event) {
97+ std::cout << " 👤 User " << event.GetUserId ()
98+ << " " << event.GetAction ()
99+ << " - " << (event.IsSuccess () ? " success" : " failed" ) << std::endl;
100+ });
101+
102+ dispatcher.DispatchSync <CustomUserEvent>(" login" , 123 , true );
103+ dispatcher.DispatchAsync <CustomUserEvent>(" logout" , 123 , true );
104+
105+ // Wait for final async event
106+ std::this_thread::sleep_for (std::chrono::milliseconds (50 ));
107+
108+ // Example 8: Event timestamping
109+ std::cout << " \n 6. Event timestamping demonstration..." << std::endl;
110+
111+ auto timestamp_listener = AddScopedListener<ApplicationExitingEvent>(dispatcher,
112+ [](const ApplicationExitingEvent& event) {
113+ auto now = std::chrono::steady_clock::now ();
114+ auto event_time = event.GetTimestamp ();
115+ auto diff = std::chrono::duration_cast<std::chrono::microseconds>(now - event_time);
116+ std::cout << " ⏱️ Event processed " << diff.count () << " microseconds after creation" << std::endl;
117+ });
118+
119+ dispatcher.DispatchSync <ApplicationExitingEvent>(0 );
120+
121+ // Example 9: Cleanup demonstration
122+ std::cout << " \n 7. Cleanup and listener management..." << std::endl;
123+
124+ std::cout << " Listeners before cleanup: " << dispatcher.GetTotalListenerCount () << std::endl;
125+
126+ // Remove some specific listeners
127+ dispatcher.RemoveListener (key_pressed_id);
128+ dispatcher.RemoveListener (display_added_id);
129+
130+ std::cout << " Listeners after removing 2: " << dispatcher.GetTotalListenerCount () << std::endl;
131+
132+ // Scoped listeners will be automatically removed when they go out of scope
133+
134+ // Stop async processing
135+ std::cout << " \n 8. Stopping async processing..." << std::endl;
136+ dispatcher.Stop ();
137+
138+ std::cout << " \n === Demonstration Complete ===" << std::endl;
139+ std::cout << " The generic event system provides:" << std::endl;
140+ std::cout << " ✓ Type-safe event handling" << std::endl;
141+ std::cout << " ✓ Both observer and callback patterns" << std::endl;
142+ std::cout << " ✓ Synchronous and asynchronous dispatch" << std::endl;
143+ std::cout << " ✓ Thread-safe operations" << std::endl;
144+ std::cout << " ✓ Automatic memory management" << std::endl;
145+ std::cout << " ✓ Event timestamping" << std::endl;
146+ std::cout << " ✓ Exception safety" << std::endl;
147+ std::cout << " ✓ RAII-based cleanup" << std::endl;
148+
149+ // Clean up remaining listeners
150+ dispatcher.RemoveListener (key_released_id);
151+ dispatcher.RemoveListener (display_removed_id);
152+ dispatcher.RemoveListener (app_started_id);
153+
154+ std::cout << " \n Final listener count: " << dispatcher.GetTotalListenerCount () << std::endl;
155+ }
156+
157+ } // namespace nativeapi
158+
159+ #ifdef DEMO_MAIN
160+ int main () {
161+ nativeapi::DemonstrateGenericEventSystem ();
162+ return 0 ;
163+ }
164+ #endif
0 commit comments