From ddb493192046edec4453dc850db49595e5ef21a3 Mon Sep 17 00:00:00 2001 From: erki Date: Wed, 25 Oct 2023 11:02:51 +0300 Subject: [PATCH] cpptick - make timer types more explicitc --- CppTick/Inc/cpptick/scheduler.hpp | 2 +- CppTick/Inc/cpptick/timer.hpp | 12 +++++++++--- Tests/cpptick.cpp | 8 ++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CppTick/Inc/cpptick/scheduler.hpp b/CppTick/Inc/cpptick/scheduler.hpp index d45f97e..ed3ae19 100644 --- a/CppTick/Inc/cpptick/scheduler.hpp +++ b/CppTick/Inc/cpptick/scheduler.hpp @@ -97,7 +97,7 @@ struct Scheduler : BaseScheduler timers_executed = true; storeCall(timer->second->getSlot()); - if (timer->second->is_one_shot) + if (timer->second->type == Timer::ONE_SHOT) { timer->first = -1u; timer->second = nullptr; diff --git a/CppTick/Inc/cpptick/timer.hpp b/CppTick/Inc/cpptick/timer.hpp index 7bd9548..6e362ff 100644 --- a/CppTick/Inc/cpptick/timer.hpp +++ b/CppTick/Inc/cpptick/timer.hpp @@ -18,10 +18,16 @@ struct BaseScheduler; 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) , period_ms(timeout_ms) - , is_one_shot(one_shot) + , type(type) { } Timer() = delete; @@ -54,7 +60,7 @@ struct Timer } std::uint32_t period_ms; - bool is_one_shot; + Type type; private: BaseScheduler* scheduler_; Utility::IFunction* slot_ = nullptr; diff --git a/Tests/cpptick.cpp b/Tests/cpptick.cpp index 97666f3..c2f9ce9 100644 --- a/Tests/cpptick.cpp +++ b/Tests/cpptick.cpp @@ -128,7 +128,7 @@ TEST_CASE("Timer will be invoked.", "[cpptick]") 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); SECTION("Timer will be invoked after 100 milliseconds.") @@ -185,7 +185,7 @@ TEST_CASE("Periodic timers will be invoked repeatedly.", "[cpptick]") callback_count = 0; REQUIRE(callback_count == 0); - cpptick::Timer timer(&scheduler, 100, false); + cpptick::Timer timer(&scheduler, 100, cpptick::Timer::PERIODIC); timer.setSlot(slot); SECTION("Timer will be invoked after 100 milliseconds.") @@ -229,7 +229,7 @@ TEST_CASE("Sequential timers operate appropriately.", "[cpptick]") cpptick::Slot slot_a(&scheduler); 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); auto callback_by_two = []() -> void @@ -239,7 +239,7 @@ TEST_CASE("Sequential timers operate appropriately.", "[cpptick]") cpptick::Slot slot_b(&scheduler); 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); callback_count = 0;