2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 22:53:20 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-08-08 22:53:20 +00:00
|
|
|
|
|
|
|
namespace libtransmission
|
|
|
|
{
|
|
|
|
|
|
|
|
class Timer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Timer() = default;
|
|
|
|
virtual ~Timer() = default;
|
|
|
|
|
|
|
|
Timer(Timer&&) = delete;
|
|
|
|
Timer(Timer const&) = delete;
|
|
|
|
Timer& operator=(Timer&&) = delete;
|
|
|
|
Timer& operator=(Timer const&) = delete;
|
|
|
|
|
|
|
|
virtual void stop() = 0;
|
2023-05-06 04:11:05 +00:00
|
|
|
virtual void set_callback(std::function<void()> callback) = 0;
|
|
|
|
virtual void set_repeating(bool repeating = true) = 0;
|
|
|
|
virtual void set_interval(std::chrono::milliseconds) = 0;
|
2022-08-08 22:53:20 +00:00
|
|
|
virtual void start() = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] virtual std::chrono::milliseconds interval() const noexcept = 0;
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] virtual bool is_repeating() const noexcept = 0;
|
2022-08-08 22:53:20 +00:00
|
|
|
|
|
|
|
void start(std::chrono::milliseconds msec)
|
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
set_interval(msec);
|
2022-08-08 22:53:20 +00:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void start_repeating(std::chrono::milliseconds msec)
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
set_repeating();
|
2022-08-08 22:53:20 +00:00
|
|
|
start(msec);
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void start_single_shot(std::chrono::milliseconds msec)
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
set_repeating(false);
|
2022-08-08 22:53:20 +00:00
|
|
|
start(msec);
|
|
|
|
}
|
|
|
|
|
|
|
|
using CStyleCallback = void (*)(void* user_data);
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_callback(CStyleCallback callback, void* user_data)
|
2022-08-08 22:53:20 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
set_callback([user_data, callback]() { callback(user_data); });
|
2022-08-08 22:53:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TimerMaker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~TimerMaker() = default;
|
|
|
|
[[nodiscard]] virtual std::unique_ptr<Timer> create() = 0;
|
2022-08-11 17:28:37 +00:00
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
[[nodiscard]] std::unique_ptr<Timer> create(std::function<void()> callback)
|
2022-08-11 17:28:37 +00:00
|
|
|
{
|
|
|
|
auto timer = create();
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(std::move(callback));
|
2022-08-11 17:28:37 +00:00
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2022-11-16 21:13:31 +00:00
|
|
|
[[nodiscard]] std::unique_ptr<Timer> create(Timer::CStyleCallback callback, void* user_data)
|
2022-08-11 17:28:37 +00:00
|
|
|
{
|
|
|
|
auto timer = create();
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback, user_data);
|
2022-08-11 17:28:37 +00:00
|
|
|
return timer;
|
|
|
|
}
|
2022-08-08 22:53:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace libtransmission
|