dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Timer.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <chrono>
13#include <functional>
14
15// Project includes
16#include "PollerFd.hpp"
19
20namespace dfx::FdWatch
21{
22class Poller;
23
51class Timer
52{
53 DFX_PRIVATE_STATE(Timer, DFX_FDWATCH_LOGGER())
54
55public:
57 enum class Mode : bool
58 {
61 };
62
63public:
65 using Callback = std::move_only_function<void ()>;
66
67public:
73
82 Timer(Poller & poller, std::chrono::milliseconds timeout, Callback cb, Mode mode = Mode::SingleShot);
83
85
87 std::chrono::milliseconds timeout() const noexcept;
88
91 std::chrono::milliseconds remainingTime() const;
92
94 Mode mode() const noexcept;
95
97 bool isSingleShot() const noexcept;
98
100 bool isRepeating() const noexcept;
101
104 bool isActive() const noexcept;
105
106public:
119 void attach(Poller & poller, std::chrono::milliseconds timeout, Callback cb, Mode mode = Mode::SingleShot);
120
124 void start();
125
131 void start(std::chrono::milliseconds timeout);
132
138 void stop();
139
146 void close() noexcept;
147};
148} // !namespace dfx::FdWatch
149
Macro-based enum <-> string utilities for dfx.
#define DECLARE_ENUM_STRING_FUNCTIONS(E)
Declare the enum string API (and std::formatter) for enum type E.
Definition EnumString.hpp:327
Macros to implement the Private State (PIMPL-like) idiom with value semantics.
#define DFX_PRIVATE_STATE(Class, logger)
Declare a private implementation state for a class.
Definition PrivateState.hpp:52
#define DFX_PRIVATE_STATE_DECLARE_RULE_OF_5(Class)
Declare Rule-of-5 special member functions using the private state pattern.
Definition PrivateState.hpp:82
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
Timer(Poller &poller, std::chrono::milliseconds timeout, Callback cb, Mode mode=Mode::SingleShot)
Constructs and attaches a timer to a poller.
bool isSingleShot() const noexcept
Convenience check for Mode::SingleShot.
std::move_only_function< void()> Callback
Callback invoked when the timer expires.
Definition Timer.hpp:65
void close() noexcept
Closes the underlying timer resources and detaches from the poller.
Mode
Timer firing mode.
Definition Timer.hpp:58
@ SingleShot
Fire once, then automatically become inactive.
Definition Timer.hpp:59
@ Repeating
Fire repeatedly at the configured interval until stopped.
Definition Timer.hpp:60
void stop()
Disarms the timer without releasing the underlying FD.
Timer()
Constructs a detached (inactive) timer.
std::chrono::milliseconds timeout() const noexcept
Returns the configured timeout/period.
Mode mode() const noexcept
Returns the current mode.
bool isActive() const noexcept
Returns whether the timer is currently armed. A timer is considered active when it is registered in a...
bool isRepeating() const noexcept
Convenience check for Mode::Repeating.
void attach(Poller &poller, std::chrono::milliseconds timeout, Callback cb, Mode mode=Mode::SingleShot)
Attaches the timer to a poller and configures it.
std::chrono::milliseconds remainingTime() const
Returns the remaining time before the next expiration. If the timer is inactive, the returned duratio...
void start()
Arms the timer using the currently configured timeout.
Definition SocketClient.hpp:23
Definition Message.hpp:21
STL namespace.