2024-01-05 21:16:50 +00:00
|
|
|
/**
|
|
|
|
* @file Timer.h
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief Millisecond-precision timer.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2024-10-19 19:25:17 +00:00
|
|
|
#include <luna/Action.h>
|
2024-01-05 21:16:50 +00:00
|
|
|
#include <luna/LinkedList.h>
|
|
|
|
#include <luna/OwnedPtr.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
namespace os
|
|
|
|
{
|
|
|
|
class EventLoop;
|
|
|
|
class Timer : LinkedListNode<Timer>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static Result<OwnedPtr<Timer>> create_singleshot(unsigned int milliseconds, Action&& action);
|
|
|
|
static Result<OwnedPtr<Timer>> create_repeating(unsigned int milliseconds, Action&& action);
|
|
|
|
|
2024-01-08 18:01:59 +00:00
|
|
|
void restart();
|
|
|
|
void reset(unsigned int milliseconds);
|
|
|
|
void stop();
|
|
|
|
|
2024-01-05 21:16:50 +00:00
|
|
|
~Timer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
timer_t m_timerid { -1 };
|
|
|
|
bool m_repeating;
|
2024-01-08 18:01:59 +00:00
|
|
|
bool m_stopped { false };
|
2024-01-05 21:16:50 +00:00
|
|
|
struct timespec m_interval;
|
|
|
|
Action m_action;
|
|
|
|
|
|
|
|
void check_if_should_invoke_action();
|
2024-01-08 18:01:59 +00:00
|
|
|
void readd_if_necessary();
|
2024-01-05 21:16:50 +00:00
|
|
|
|
|
|
|
Timer() = default;
|
|
|
|
|
|
|
|
friend class EventLoop;
|
|
|
|
};
|
|
|
|
}
|