2023-10-24 23:22:47 +03:00

64 lines
1.1 KiB
C++

//
// Created by erki on 24/10/23.
//
#pragma once
#include <cstdint>
#include <utility_function.hpp>
#include "cpptick/argstore.hpp"
namespace cpptick
{
struct BaseScheduler;
struct Timer
{
Timer(BaseScheduler* scheduler, const std::uint32_t timeout_ms, const bool one_shot = false)
: scheduler_(scheduler)
, period_ms(timeout_ms)
, is_one_shot(one_shot)
{ }
Timer() = delete;
Timer(const Timer&) = delete;
Timer(Timer&&) = delete;
Timer& operator=(const Timer&) = delete;
Timer& operator=(Timer&&) = delete;
void start();
template<typename T>
void setSlot(T& slot)
{
setSlot(&slot.invoke_ptr);
}
void setSlot(Utility::IFunction<void (ArgStorage&)>* slot)
{
slot_ = slot;
}
Utility::IFunction<void (ArgStorage&)>* getSlot()
{
return slot_;
}
std::uint32_t expirationTime(const std::uint32_t current_time) const
{
return period_ms + current_time;
}
std::uint32_t period_ms;
bool is_one_shot;
private:
BaseScheduler* scheduler_;
Utility::IFunction<void (ArgStorage&)>* slot_ = nullptr;
};
}