2017-08-29 15:53:57 +02:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2017-08-29 15:53:57 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_TIMER_EVENT_HXX
|
|
|
|
#define MPD_TIMER_EVENT_HXX
|
|
|
|
|
2020-10-08 19:35:36 +02:00
|
|
|
#include "Chrono.hxx"
|
2017-08-29 15:53:57 +02:00
|
|
|
#include "util/BindMethod.hxx"
|
|
|
|
|
|
|
|
#include <boost/intrusive/set_hook.hpp>
|
|
|
|
|
|
|
|
class EventLoop;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class invokes a callback function after a certain amount of
|
|
|
|
* time. Use Schedule() to start the timer or Cancel() to cancel it.
|
|
|
|
*
|
|
|
|
* This class is not thread-safe, all methods must be called from the
|
|
|
|
* thread that runs the #EventLoop, except where explicitly documented
|
|
|
|
* as thread-safe.
|
|
|
|
*/
|
2020-10-08 16:57:32 +02:00
|
|
|
class TimerEvent final
|
2020-10-08 17:27:15 +02:00
|
|
|
: public boost::intrusive::set_base_hook<boost::intrusive::link_mode<boost::intrusive::auto_unlink>>
|
2020-10-08 16:57:32 +02:00
|
|
|
{
|
2021-02-04 21:25:57 +01:00
|
|
|
friend class TimerList;
|
2017-08-29 15:53:57 +02:00
|
|
|
|
|
|
|
EventLoop &loop;
|
|
|
|
|
2020-10-08 19:34:26 +02:00
|
|
|
using Callback = BoundMethod<void() noexcept>;
|
2017-08-29 15:53:57 +02:00
|
|
|
const Callback callback;
|
|
|
|
|
|
|
|
/**
|
2020-12-04 09:57:19 +01:00
|
|
|
* When is this timer due? This is only valid if IsPending()
|
2017-08-29 15:53:57 +02:00
|
|
|
* returns true.
|
|
|
|
*/
|
2020-10-08 19:35:36 +02:00
|
|
|
Event::Clock::time_point due;
|
2017-08-29 15:53:57 +02:00
|
|
|
|
|
|
|
public:
|
2017-12-20 10:45:30 +01:00
|
|
|
TimerEvent(EventLoop &_loop, Callback _callback) noexcept
|
2018-11-12 22:01:47 +01:00
|
|
|
:loop(_loop), callback(_callback) {}
|
2017-08-29 15:53:57 +02:00
|
|
|
|
2019-04-04 19:48:28 +02:00
|
|
|
auto &GetEventLoop() const noexcept {
|
2017-08-29 15:53:57 +02:00
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
2021-02-04 21:25:57 +01:00
|
|
|
constexpr auto GetDue() const noexcept {
|
|
|
|
return due;
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:40:28 +01:00
|
|
|
bool IsPending() const noexcept {
|
2020-10-08 16:57:32 +02:00
|
|
|
return is_linked();
|
2017-08-29 15:53:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 19:35:36 +02:00
|
|
|
void Schedule(Event::Duration d) noexcept;
|
2020-10-08 17:27:15 +02:00
|
|
|
|
2018-11-12 22:01:47 +01:00
|
|
|
/**
|
|
|
|
* Like Schedule(), but is a no-op if there is a due time
|
|
|
|
* earlier than the given one.
|
|
|
|
*/
|
|
|
|
void ScheduleEarlier(Event::Duration d) noexcept;
|
|
|
|
|
2020-10-08 17:27:15 +02:00
|
|
|
void Cancel() noexcept {
|
|
|
|
unlink();
|
|
|
|
}
|
2017-08-29 15:53:57 +02:00
|
|
|
|
|
|
|
private:
|
2017-12-20 10:45:30 +01:00
|
|
|
void Run() noexcept {
|
2017-08-29 15:53:57 +02:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* MAIN_NOTIFY_H */
|