Skip to content

Commit

Permalink
libraries: libcore[timer]
Browse files Browse the repository at this point in the history
  • Loading branch information
krishpranav committed Oct 1, 2023
1 parent af533f5 commit dbc9250
Showing 1 changed file with 60 additions and 11 deletions.
71 changes: 60 additions & 11 deletions libraries/libcore/timer.h
Expand Up @@ -14,19 +14,23 @@
#include <mods/function.h>
#include <libcore/object.h>

namespace Core
namespace Core
{

class Timer final : public Object
class Timer final : public Object
{
C_OBJECT(Timer);

public:

/**
* @param interval
* @param timeout_handler
* @param parent
* @return NonnullRefPtr<Timer>
*/
static NonnullRefPtr<Timer> create_single_shot(int interval)
static NonnullRefPtr<Timer> create_single_shot(int interval, Function<void()>&& timeout_handler, Object* parent = nullptr)
{
auto timer = adopt(*new Timer(interval, move(timeout_handler)));
auto timer = adopt(*new Timer(interval, move(timeout_handler), parent));
timer->set_single_shot(true);
return timer;
}
Expand All @@ -40,24 +44,71 @@ namespace Core
*/
void start(int interval);


void restart();
/**
* @param interval
*/
void restart(int interval);

void stop();

private:
/**
* @return true
* @return false
*/
bool is_active() const
{
return m_active;
}

/**
* @brief Construct a new Timer object
*
* @return int
*/
int interval() const
{
return m_interval;
}

/**
* @param interval
*/
void set_interval(int interval)
{
if (m_interval == interval)
return;
m_interval = interval;
m_interval_dirty = true;
}

/**
* @return true
* @return false
*/
bool is_single_shot() const
{
return m_single_shot;
}

/**
* @param single_shot
*/
void set_single_shot(bool single_shot)
{
m_single_shot = single_shot;
}

Function<void()> on_timeout;

private:
/**
* @param parent
*/
explicit Timer(Object* parent = nullptr);

/**
* @brief Construct a new Timer object
*
* @param interval
* @param timeout_handler
* @param parent
Expand All @@ -72,7 +123,5 @@ namespace Core

int m_interval { 0 };


}; // class Timer

} // namespace Core

0 comments on commit dbc9250

Please sign in to comment.