cpptick - make timer types more explicitc

This commit is contained in:
erki 2023-10-25 11:02:51 +03:00
parent a8175db127
commit ddb4931920
3 changed files with 14 additions and 8 deletions

View File

@ -97,7 +97,7 @@ struct Scheduler : BaseScheduler
timers_executed = true; timers_executed = true;
storeCall(timer->second->getSlot()); storeCall(timer->second->getSlot());
if (timer->second->is_one_shot) if (timer->second->type == Timer::ONE_SHOT)
{ {
timer->first = -1u; timer->first = -1u;
timer->second = nullptr; timer->second = nullptr;

View File

@ -18,10 +18,16 @@ struct BaseScheduler;
struct Timer struct Timer
{ {
Timer(BaseScheduler* scheduler, const std::uint32_t timeout_ms, const bool one_shot = false) enum Type
{
ONE_SHOT,
PERIODIC
};
Timer(BaseScheduler* scheduler, const std::uint32_t timeout_ms, const Type type)
: scheduler_(scheduler) : scheduler_(scheduler)
, period_ms(timeout_ms) , period_ms(timeout_ms)
, is_one_shot(one_shot) , type(type)
{ } { }
Timer() = delete; Timer() = delete;
@ -54,7 +60,7 @@ struct Timer
} }
std::uint32_t period_ms; std::uint32_t period_ms;
bool is_one_shot; Type type;
private: private:
BaseScheduler* scheduler_; BaseScheduler* scheduler_;
Utility::IFunction<void (ArgStorage&)>* slot_ = nullptr; Utility::IFunction<void (ArgStorage&)>* slot_ = nullptr;

View File

@ -128,7 +128,7 @@ TEST_CASE("Timer will be invoked.", "[cpptick]")
callback_count = 0; callback_count = 0;
REQUIRE(callback_count == 0); REQUIRE(callback_count == 0);
cpptick::Timer timer(&scheduler, 100, true); cpptick::Timer timer(&scheduler, 100, cpptick::Timer::ONE_SHOT);
timer.setSlot(slot); timer.setSlot(slot);
SECTION("Timer will be invoked after 100 milliseconds.") SECTION("Timer will be invoked after 100 milliseconds.")
@ -185,7 +185,7 @@ TEST_CASE("Periodic timers will be invoked repeatedly.", "[cpptick]")
callback_count = 0; callback_count = 0;
REQUIRE(callback_count == 0); REQUIRE(callback_count == 0);
cpptick::Timer timer(&scheduler, 100, false); cpptick::Timer timer(&scheduler, 100, cpptick::Timer::PERIODIC);
timer.setSlot(slot); timer.setSlot(slot);
SECTION("Timer will be invoked after 100 milliseconds.") SECTION("Timer will be invoked after 100 milliseconds.")
@ -229,7 +229,7 @@ TEST_CASE("Sequential timers operate appropriately.", "[cpptick]")
cpptick::Slot<void ()> slot_a(&scheduler); cpptick::Slot<void ()> slot_a(&scheduler);
auto* f = slot_a.connect(callbackByOne); auto* f = slot_a.connect(callbackByOne);
cpptick::Timer timer_a(&scheduler, 100, false); cpptick::Timer timer_a(&scheduler, 100, cpptick::Timer::PERIODIC);
timer_a.setSlot(slot_a); timer_a.setSlot(slot_a);
auto callback_by_two = []() -> void auto callback_by_two = []() -> void
@ -239,7 +239,7 @@ TEST_CASE("Sequential timers operate appropriately.", "[cpptick]")
cpptick::Slot<void ()> slot_b(&scheduler); cpptick::Slot<void ()> slot_b(&scheduler);
auto* ff = slot_b.connect(callback_by_two); auto* ff = slot_b.connect(callback_by_two);
cpptick::Timer timer_b(&scheduler, 50, true); cpptick::Timer timer_b(&scheduler, 50, cpptick::Timer::ONE_SHOT);
timer_b.setSlot(slot_b); timer_b.setSlot(slot_b);
callback_count = 0; callback_count = 0;