2019-03-30 20:40:27 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <LibCore/CObject.h>
|
2019-03-30 20:40:27 +00:00
|
|
|
|
2019-04-11 22:09:45 +00:00
|
|
|
class CTimer final : public CObject {
|
2019-03-30 20:40:27 +00:00
|
|
|
public:
|
2019-04-11 22:09:45 +00:00
|
|
|
explicit CTimer(CObject* parent = nullptr);
|
2019-04-14 03:44:15 +00:00
|
|
|
CTimer(int interval, Function<void()>&& timeout_handler, CObject* parent = nullptr);
|
2019-04-11 22:09:45 +00:00
|
|
|
virtual ~CTimer() override;
|
2019-03-30 20:40:27 +00:00
|
|
|
|
|
|
|
void start();
|
|
|
|
void start(int interval);
|
2019-04-18 02:38:04 +00:00
|
|
|
void restart(int interval);
|
2019-03-30 20:40:27 +00:00
|
|
|
void stop();
|
|
|
|
|
|
|
|
bool is_active() const { return m_active; }
|
|
|
|
int interval() const { return m_interval; }
|
2019-04-18 02:38:04 +00:00
|
|
|
void set_interval(int interval)
|
|
|
|
{
|
|
|
|
if (m_interval == interval)
|
|
|
|
return;
|
|
|
|
m_interval = interval;
|
|
|
|
m_interval_dirty = true;
|
|
|
|
}
|
2019-03-30 20:40:27 +00:00
|
|
|
|
|
|
|
bool is_single_shot() const { return m_single_shot; }
|
|
|
|
void set_single_shot(bool single_shot) { m_single_shot = single_shot; }
|
|
|
|
|
|
|
|
Function<void()> on_timeout;
|
|
|
|
|
2019-04-11 22:09:45 +00:00
|
|
|
virtual const char* class_name() const override { return "CTimer"; }
|
2019-03-30 20:40:27 +00:00
|
|
|
|
|
|
|
private:
|
2019-04-10 14:56:55 +00:00
|
|
|
virtual void timer_event(CTimerEvent&) override;
|
2019-03-30 20:40:27 +00:00
|
|
|
|
|
|
|
bool m_active { false };
|
|
|
|
bool m_single_shot { false };
|
2019-04-18 02:38:04 +00:00
|
|
|
bool m_interval_dirty { false };
|
2019-03-30 20:40:27 +00:00
|
|
|
int m_interval { 0 };
|
|
|
|
};
|