2022-08-08 22:53:20 +00:00
|
|
|
// This file Copyright 2022 Mnemosyne LLC.
|
|
|
|
// 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;
|
|
|
|
virtual void setCallback(std::function<void()> callback) = 0;
|
|
|
|
virtual void setRepeating(bool repeating = true) = 0;
|
|
|
|
virtual void setInterval(std::chrono::milliseconds) = 0;
|
|
|
|
virtual void start() = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] virtual std::chrono::milliseconds interval() const noexcept = 0;
|
|
|
|
[[nodiscard]] virtual bool isRepeating() const noexcept = 0;
|
|
|
|
|
|
|
|
void start(std::chrono::milliseconds msec)
|
|
|
|
{
|
|
|
|
setInterval(msec);
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void startRepeating(std::chrono::milliseconds msec)
|
|
|
|
{
|
|
|
|
setRepeating();
|
|
|
|
start(msec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void startSingleShot(std::chrono::milliseconds msec)
|
|
|
|
{
|
|
|
|
setRepeating(false);
|
|
|
|
start(msec);
|
|
|
|
}
|
|
|
|
|
|
|
|
using CStyleCallback = void (*)(void* user_data);
|
|
|
|
void setCallback(CStyleCallback callback, void* user_data)
|
|
|
|
{
|
|
|
|
setCallback([user_data, callback]() { callback(user_data); });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
timer->setCallback(std::move(callback));
|
|
|
|
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();
|
|
|
|
timer->setCallback(callback, user_data);
|
|
|
|
return timer;
|
|
|
|
}
|
2022-08-08 22:53:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace libtransmission
|