Skip to content

Commit

Permalink
Minor Demo Tweaks
Browse files Browse the repository at this point in the history
Update some naming of objects and output messages. Also added example of class that holds a handler_registration as a member variable.
  • Loading branch information
DeveloperPaul123 committed Nov 9, 2021
1 parent e4f8468 commit ae1dd80
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,55 @@ 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_; }
private:
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<first_event>([](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<first_event>(&foo);
// register free function
const auto reg = evt_bus.register_handler<first_event>(&free_function_event_handler);
const auto third_event_reg = evt_bus.register_handler<third_event>([](const third_event& evt)
{
std::cout << "my third event handler: " << evt.value << new_line;
});

const auto empty_event_handler = evt_bus.register_handler<third_event>([]() {std::cout << "I just do stuff when a third_event is fired." << new_line; });
const auto third_event_reg_2 = evt_bus.register_handler<third_event>([]() {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);
Expand All @@ -67,32 +79,35 @@ int main()
const auto other_event_reg = evt_bus.register_handler<second_event>([](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<second_event>([](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<first_event>([](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<first_event>(&callback_obj , &my_callback_object::on_event_fired);
const auto thrid_event_reg_pmr = evt_bus.register_handler<third_event>(&callback_obj, &my_callback_object::on_third_event);

first_event_callback_object callback_obj;
const auto dmy_evt_pmr_reg = evt_bus.register_handler<first_event>(&callback_obj , &first_event_callback_object::on_event_fired);
const auto thrid_event_reg_pmr = evt_bus.register_handler<third_event>(&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<third_event>(&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);
Expand Down

0 comments on commit ae1dd80

Please sign in to comment.