A simple demonstration of using timers.This example creates and event loop and starts two timers to print fizz and buzz every 3 respective 5 seconds.
The user can also configure a third timer via stdin to print woof.
This example assumes you already understand the events.cpp example.
#include <iostream>
#include <string>
struct interval_change_type;
{
public:
:
fz::event_handler(loop)
{
fizz_ =
add_timer(fz::duration::from_seconds(3),
false);
buzz_ =
add_timer(fz::duration::from_seconds(5),
false);
}
virtual ~fizzbuzz()
{
}
private:
{
fz::dispatch<fz::timer_event, interval_change>(ev, this, &fizzbuzz::on_timer, &fizzbuzz::on_interval_change);
}
{
}
void on_timer(fz::timer_id const& id)
{
if (id == fizz_) {
std::cout << "fizz" << std::endl;
}
else if (id == buzz_) {
std::cout << "buzz" << std::endl;
}
else if (id == woof_) {
std::cout << "woof" << std::endl;
}
}
fz::timer_id fizz_{};
fz::timer_id buzz_{};
fz::timer_id woof_{};
};
int main()
{
fizzbuzz fb(loop);
std::cout << "Enter interval in seconds for the woof timer or 0 to stop program." << std::endl;
int seconds;
do {
std::cin >> seconds;
if (std::cin.good() && seconds > 0) {
fb.send_event<interval_change>(fz::duration::from_seconds(seconds));
}
}
while (std::cin.good() && seconds != 0);
return 0;
}