From ae1dd80b927575184ea242d671274c74cd283224 Mon Sep 17 00:00:00 2001 From: Paul T Date: Tue, 9 Nov 2021 10:37:12 -0500 Subject: [PATCH] Minor Demo Tweaks Update some naming of objects and output messages. Also added example of class that holds a handler_registration as a member variable. --- demo/main.cpp | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/demo/main.cpp b/demo/main.cpp index 04ea42d..db10e95 100644 --- a/demo/main.cpp +++ b/demo/main.cpp @@ -21,15 +21,15 @@ struct third_event double value{ 234.00 }; }; -void foo(const first_event& evt) +void free_function_event_handler(const first_event& evt) { std::cout << "event: " << evt.message << std::endl; } -class my_callback_object +class first_event_callback_object { public: - my_callback_object() = default; + first_event_callback_object() = default; void on_event_fired(const first_event&) { event_count_++; } void on_third_event() {}; [[nodiscard]] int get_event_count() const { return event_count_; } @@ -37,27 +37,39 @@ class my_callback_object int event_count_{ 0 }; }; -class third_event_object +class third_event_callback_object { public: void on_third_event() {}; }; +class internal_registration_class { + dp::handler_registration reg; +public: + internal_registration_class(dp::event_bus& bus) : reg(std::move(bus.register_handler([](const first_event& evt) { + std::cout << "test class: " << evt.message << "\n"; + }))) { + + } +}; + int main() { using namespace dp; event_bus evt_bus; - my_callback_object callback_obj; - const auto reg = evt_bus.register_handler(&foo); + // register free function + const auto reg = evt_bus.register_handler(&free_function_event_handler); const auto third_event_reg = evt_bus.register_handler([](const third_event& evt) { std::cout << "my third event handler: " << evt.value << new_line; }); - const auto empty_event_handler = evt_bus.register_handler([]() {std::cout << "I just do stuff when a third_event is fired." << new_line; }); + const auto third_event_reg_2 = evt_bus.register_handler([]() {std::cout << "I just do stuff when a third_event is fired." << new_line; }); - first_event evt{ "hello from dummy event" }; + internal_registration_class test_object(evt_bus); + + first_event evt{ "hello from first event" }; evt_bus.fire_event(evt); evt_bus.fire_event(third_event{ 13.0 }); evt_bus.remove_handler(third_event_reg); @@ -67,32 +79,35 @@ int main() const auto other_event_reg = evt_bus.register_handler([](const second_event& other_evt) {std::cout << "first other event handler says: " << other_evt.message << std::endl; }); const auto other_event_second_reg = evt_bus.register_handler([](const second_event& other_evt){std::cout << "second other event handler says: " << other_evt.id << " " << other_evt.message << std::endl;}); const auto dmy_evt_first_reg = evt_bus.register_handler([](const first_event& dmy_evt) {std::cout << "third event handler says: " << dmy_evt.message << std::endl;}); - const auto dmy_evt_pmr_reg = evt_bus.register_handler(&callback_obj , &my_callback_object::on_event_fired); - const auto thrid_event_reg_pmr = evt_bus.register_handler(&callback_obj, &my_callback_object::on_third_event); + + first_event_callback_object callback_obj; + const auto dmy_evt_pmr_reg = evt_bus.register_handler(&callback_obj , &first_event_callback_object::on_event_fired); + const auto thrid_event_reg_pmr = evt_bus.register_handler(&callback_obj, &first_event_callback_object::on_third_event); // the following does not compile // third_event_object teo; // const auto rg = evt_bus.register_handler(&teo, &my_callback_object::on_third_event); - second_event other_evt{ 2, "hello there" }; - first_event dmy_event{ "oh boy..." }; + second_event snd_evt{ 2, "hello there from second event" }; + first_event first_evt{ "another first event" }; - evt_bus.fire_event(dmy_event); + evt_bus.fire_event(first_evt); - std::cout << "Firing other event\n"; - evt_bus.fire_event(other_evt); + std::cout << "Firing second event\n"; + evt_bus.fire_event(snd_evt); evt_bus.remove_handler(other_event_reg); - std::cout << "Firing other event\n"; - evt_bus.fire_event(other_evt); + std::cout << "Firing second and third event\n"; + evt_bus.fire_event(snd_evt); evt_bus.fire_event(third_event{}); std::cout << "callback count: " << callback_obj.get_event_count() << std::endl; std::cout << "handler count: " << evt_bus.handler_count() << "\n"; std::cout << "removing handlers..." << "\n"; + // this is optional as the handler registrations are RAII objects evt_bus.remove_handler(reg); evt_bus.remove_handler(other_event_second_reg); - evt_bus.remove_handler(empty_event_handler); + evt_bus.remove_handler(third_event_reg_2); evt_bus.remove_handler(dmy_evt_first_reg); evt_bus.remove_handler(dmy_evt_pmr_reg); evt_bus.remove_handler(thrid_event_reg_pmr);