2016-06-17 18:20:11 +02:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2016-06-17 18:20:11 +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.
|
|
|
|
*/
|
|
|
|
|
2017-09-21 22:40:25 +02:00
|
|
|
#ifndef MPD_DEFER_EVENT_HXX
|
|
|
|
#define MPD_DEFER_EVENT_HXX
|
2016-06-17 18:20:11 +02:00
|
|
|
|
|
|
|
#include "util/BindMethod.hxx"
|
|
|
|
|
2017-11-12 17:34:06 +01:00
|
|
|
#include <boost/intrusive/list_hook.hpp>
|
|
|
|
|
|
|
|
class EventLoop;
|
|
|
|
|
2016-06-17 18:20:11 +02:00
|
|
|
/**
|
|
|
|
* Invoke a method call in the #EventLoop.
|
|
|
|
*
|
|
|
|
* This class is thread-safe.
|
|
|
|
*/
|
2017-11-12 17:34:06 +01:00
|
|
|
class DeferEvent final {
|
|
|
|
friend class EventLoop;
|
|
|
|
|
|
|
|
typedef boost::intrusive::list_member_hook<> ListHook;
|
|
|
|
ListHook list_hook;
|
|
|
|
|
|
|
|
EventLoop &loop;
|
|
|
|
|
2017-11-10 20:55:24 +01:00
|
|
|
typedef BoundMethod<void() noexcept> Callback;
|
2016-06-17 18:20:11 +02:00
|
|
|
const Callback callback;
|
|
|
|
|
|
|
|
public:
|
2017-11-12 17:34:06 +01:00
|
|
|
DeferEvent(EventLoop &_loop, Callback _callback) noexcept
|
|
|
|
:loop(_loop), callback(_callback) {}
|
2016-06-17 18:20:11 +02:00
|
|
|
|
2017-11-12 17:34:06 +01:00
|
|
|
~DeferEvent() noexcept {
|
|
|
|
Cancel();
|
|
|
|
}
|
|
|
|
|
2019-04-04 19:48:28 +02:00
|
|
|
EventLoop &GetEventLoop() const noexcept {
|
2017-11-12 17:34:06 +01:00
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Schedule() noexcept;
|
|
|
|
void Cancel() noexcept;
|
2016-06-17 18:20:11 +02:00
|
|
|
|
2017-09-21 22:40:25 +02:00
|
|
|
private:
|
2017-11-12 17:34:06 +01:00
|
|
|
bool IsPending() const noexcept {
|
|
|
|
return list_hook.is_linked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunDeferred() noexcept {
|
2016-06-17 18:20:11 +02:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|