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;
storeCall(timer->second->getSlot());
if (timer->second->is_one_shot)
if (timer->second->type == Timer::ONE_SHOT)
{
timer->first = -1u;
timer->second = nullptr;

View File

@ -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<void (ArgStorage&)>* slot_ = nullptr;

View File

@ -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<void ()> 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<void ()> 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;