AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
Timer.h
1/*
2Copyright (c) 2026 Alexandr Kuzmuk
3
4This file is part of the AsyncFw project. Licensed under the MIT License.
5See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
6*/
7
8#pragma once
9
10#include "../core/FunctionConnector.h"
11
12namespace AsyncFw {
15class Timer {
16public:
18 template <typename T>
19 static void single(int ms, T function) {
20 new SingleTimerTask(ms, function);
21 }
22
23 Timer();
24 ~Timer();
26 void start(int, bool = false);
28 void stop();
29
31 FunctionConnectorProtected<Timer>::Connector<> timeout;
32
33private:
34 template <typename T>
35 class SingleTimerTask : public AbstractThread::AbstractTask {
36 public:
37 SingleTimerTask(int ms, T _f) : f(std::move(_f)) {
39 id = t->appendTimer(ms, this);
40 }
41 void operator()() override {
42 t->removeTimer(id);
43 f();
44 }
45
46 private:
48 int id;
49 T f;
50 };
51 AbstractThread *thread_;
52 int timerId = -1;
53 bool single_;
54};
55} // namespace AsyncFw
The AbstractThread class provides the base functionality for thread management.
Definition AbstractThread.h:46
virtual int appendTimer(int, AbstractTask *)
Append timer.
Definition AbstractThread.cpp:675
AbstractFunction<> AbstractTask
The AbstractTask type.
Definition AbstractThread.h:56
static AbstractThread * currentThread()
Returns a pointer to the AsyncFw::AbstractThread that manages the currently executing thread.
Definition AbstractThread.cpp:295
virtual void removeTimer(int)
Remove timer.
Definition AbstractThread.cpp:712
The Timer class.
Definition Timer.h:15
void start(int, bool=false)
Starts or restarts a timer with the specified timeout. If the timer is already running,...
Definition Timer.cpp:29
void stop()
Stops the timer.
Definition Timer.cpp:34
FunctionConnectorProtected< Timer >::Connector<> timeout
The Timer::timeout connector.
Definition Timer.h:31
static void single(int ms, T function)
Starts a single shot timer with the specified timeout.
Definition Timer.h:19