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 278914a commit af533f5
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions libraries/libcore/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @file timer.h
* @author Krisna Pranav
* @brief timer
* @version 6.0
* @date 2023-10-01
*
* @copyright Copyright (c) 2021 - 2023 pranaOS Developers, Krisna Pranav
*
*/

#pragma once

#include <mods/function.h>
#include <libcore/object.h>

namespace Core
{

class Timer final : public Object
{
public:
/**
* @param interval
* @return NonnullRefPtr<Timer>
*/
static NonnullRefPtr<Timer> create_single_shot(int interval)
{
auto timer = adopt(*new Timer(interval, move(timeout_handler)));
timer->set_single_shot(true);
return timer;
}

/// @brief Destroy the Timer object
virtual ~Timer() override;

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

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

void stop();

private:

/**
* @brief Construct a new Timer object
*
* @param parent
*/
explicit Timer(Object* parent = nullptr);

/**
* @param interval
* @param timeout_handler
* @param parent
*/
Timer(int interval, Function<void()>&& timeout_handler, Object* parent = nullptr);

virtual void timer_event(TimerEvent&) override;

bool m_active { false };
bool m_single_shot { false };
bool m_interval_dirty { false };

int m_interval { 0 };


}; // class Timer

} // namespace Core

0 comments on commit af533f5

Please sign in to comment.