2011-10-05 11:36:08 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-10-04 18:26:40 +02:00
|
|
|
#ifndef CLOSURE_H
|
|
|
|
#define CLOSURE_H
|
|
|
|
|
2015-07-20 15:35:13 +02:00
|
|
|
#include <chrono>
|
2013-10-28 17:30:52 +01:00
|
|
|
#include <functional>
|
2014-02-06 14:48:00 +01:00
|
|
|
#include <memory>
|
2012-11-26 10:10:20 +01:00
|
|
|
|
2015-11-26 19:34:41 +01:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureWatcher>
|
2011-10-04 18:26:40 +02:00
|
|
|
#include <QMetaMethod>
|
|
|
|
#include <QObject>
|
2012-03-19 19:36:52 +01:00
|
|
|
#include <QSharedPointer>
|
2015-07-20 15:35:13 +02:00
|
|
|
#include <QTimer>
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2012-03-20 15:14:44 +01:00
|
|
|
namespace _detail {
|
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
class ObjectHelper;
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
// Interface for ObjectHelper to call on signal emission.
|
2014-02-06 14:48:00 +01:00
|
|
|
class ClosureBase {
|
2011-10-04 18:26:40 +02:00
|
|
|
public:
|
2012-11-22 15:15:07 +01:00
|
|
|
virtual ~ClosureBase();
|
|
|
|
virtual void Invoke() = 0;
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
// Tests only.
|
|
|
|
ObjectHelper* helper() const;
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
protected:
|
|
|
|
explicit ClosureBase(ObjectHelper*);
|
|
|
|
ObjectHelper* helper_;
|
2014-02-06 14:48:00 +01:00
|
|
|
|
|
|
|
private:
|
2015-11-27 11:44:59 +01:00
|
|
|
Q_DISABLE_COPY(ClosureBase)
|
2011-10-04 18:26:40 +02:00
|
|
|
};
|
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
// QObject helper as templated QObjects do not work.
|
|
|
|
// Connects to the given signal and invokes the closure when called.
|
|
|
|
// Deletes itself and the Closure after being invoked.
|
|
|
|
class ObjectHelper : public QObject {
|
2011-10-04 18:26:40 +02:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2014-02-07 16:34:20 +01:00
|
|
|
ObjectHelper(QObject* parent, const char* signal, ClosureBase* closure);
|
2012-03-19 19:36:52 +01:00
|
|
|
|
2011-10-04 18:26:40 +02:00
|
|
|
private slots:
|
|
|
|
void Invoked();
|
|
|
|
|
|
|
|
private:
|
2014-02-06 14:48:00 +01:00
|
|
|
std::unique_ptr<ClosureBase> closure_;
|
2015-11-27 11:44:59 +01:00
|
|
|
Q_DISABLE_COPY(ObjectHelper)
|
2012-11-22 15:15:07 +01:00
|
|
|
};
|
2011-10-06 13:19:42 +02:00
|
|
|
|
2012-11-22 17:57:26 +01:00
|
|
|
// Helpers for unpacking a variadic template list.
|
|
|
|
|
|
|
|
// Base case of no arguments.
|
|
|
|
void Unpack(QList<QGenericArgument>*);
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2012-11-22 17:57:26 +01:00
|
|
|
template <typename Arg>
|
|
|
|
void Unpack(QList<QGenericArgument>* list, const Arg& arg) {
|
|
|
|
list->append(Q_ARG(Arg, arg));
|
|
|
|
}
|
2011-10-04 18:26:40 +02:00
|
|
|
|
2012-11-22 17:57:26 +01:00
|
|
|
template <typename Head, typename... Tail>
|
2014-02-07 16:34:20 +01:00
|
|
|
void Unpack(QList<QGenericArgument>* list, const Head& head,
|
|
|
|
const Tail&... tail) {
|
2012-11-22 17:57:26 +01:00
|
|
|
Unpack(list, head);
|
|
|
|
Unpack(list, tail...);
|
|
|
|
}
|
2012-03-19 19:36:52 +01:00
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
template <typename... Args>
|
|
|
|
class Closure : public ClosureBase {
|
2012-03-19 19:36:52 +01:00
|
|
|
public:
|
2014-02-07 16:34:20 +01:00
|
|
|
Closure(QObject* sender, const char* signal, QObject* receiver,
|
|
|
|
const char* slot, const Args&... args)
|
|
|
|
: ClosureBase(new ObjectHelper(sender, signal, this)),
|
|
|
|
// std::bind is the easiest way to store an argument list.
|
|
|
|
function_(std::bind(&Closure<Args...>::Call, this, args...)),
|
|
|
|
receiver_(receiver) {
|
2012-11-22 15:15:07 +01:00
|
|
|
const QMetaObject* meta_receiver = receiver->metaObject();
|
|
|
|
QByteArray normalised_slot = QMetaObject::normalizedSignature(slot + 1);
|
|
|
|
const int index = meta_receiver->indexOfSlot(normalised_slot.constData());
|
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
slot_ = meta_receiver->method(index);
|
2014-02-07 16:34:20 +01:00
|
|
|
QObject::connect(receiver_, SIGNAL(destroyed()), helper_,
|
|
|
|
SLOT(deleteLater()));
|
2012-03-19 19:36:52 +01:00
|
|
|
}
|
2012-03-19 21:17:57 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
virtual void Invoke() { function_(); }
|
2012-03-19 21:17:57 +01:00
|
|
|
|
2012-03-19 19:36:52 +01:00
|
|
|
private:
|
2012-11-22 15:15:07 +01:00
|
|
|
void Call(const Args&... args) {
|
|
|
|
QList<QGenericArgument> arg_list;
|
2012-11-22 17:57:26 +01:00
|
|
|
Unpack(&arg_list, args...);
|
2012-11-22 15:15:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
slot_.invoke(receiver_,
|
|
|
|
arg_list.size() > 0 ? arg_list[0] : QGenericArgument(),
|
|
|
|
arg_list.size() > 1 ? arg_list[1] : QGenericArgument(),
|
|
|
|
arg_list.size() > 2 ? arg_list[2] : QGenericArgument(),
|
|
|
|
arg_list.size() > 3 ? arg_list[3] : QGenericArgument(),
|
|
|
|
arg_list.size() > 4 ? arg_list[4] : QGenericArgument(),
|
|
|
|
arg_list.size() > 5 ? arg_list[5] : QGenericArgument(),
|
|
|
|
arg_list.size() > 6 ? arg_list[6] : QGenericArgument(),
|
|
|
|
arg_list.size() > 7 ? arg_list[7] : QGenericArgument(),
|
|
|
|
arg_list.size() > 8 ? arg_list[8] : QGenericArgument(),
|
|
|
|
arg_list.size() > 9 ? arg_list[9] : QGenericArgument());
|
2012-11-22 15:15:07 +01:00
|
|
|
}
|
2012-03-19 19:36:52 +01:00
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
std::function<void()> function_;
|
2012-11-22 15:15:07 +01:00
|
|
|
QObject* receiver_;
|
|
|
|
QMetaMethod slot_;
|
|
|
|
};
|
2012-03-19 19:36:52 +01:00
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
class SharedClosure : public Closure<Args...> {
|
2012-03-19 19:36:52 +01:00
|
|
|
public:
|
2014-02-07 16:34:20 +01:00
|
|
|
SharedClosure(QSharedPointer<T> sender, const char* signal, QObject* receiver,
|
|
|
|
const char* slot, const Args&... args)
|
|
|
|
: Closure<Args...>(sender.data(), signal, receiver, slot, args...),
|
|
|
|
data_(sender) {}
|
2012-03-19 19:36:52 +01:00
|
|
|
|
|
|
|
private:
|
2012-11-22 15:15:07 +01:00
|
|
|
QSharedPointer<T> data_;
|
2012-03-19 19:36:52 +01:00
|
|
|
};
|
|
|
|
|
2012-11-26 09:41:26 +01:00
|
|
|
class CallbackClosure : public ClosureBase {
|
|
|
|
public:
|
2014-02-07 16:34:20 +01:00
|
|
|
CallbackClosure(QObject* sender, const char* signal,
|
|
|
|
std::function<void()> callback);
|
2012-11-26 09:41:26 +01:00
|
|
|
|
|
|
|
virtual void Invoke();
|
|
|
|
|
|
|
|
private:
|
2014-02-06 14:48:00 +01:00
|
|
|
std::function<void()> callback_;
|
2012-11-26 09:41:26 +01:00
|
|
|
};
|
|
|
|
|
2012-03-20 15:14:44 +01:00
|
|
|
} // namespace _detail
|
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
template <typename... Args>
|
2014-02-07 16:34:20 +01:00
|
|
|
_detail::ClosureBase* NewClosure(QObject* sender, const char* signal,
|
|
|
|
QObject* receiver, const char* slot,
|
|
|
|
const Args&... args) {
|
|
|
|
return new _detail::Closure<Args...>(sender, signal, receiver, slot, args...);
|
2011-10-06 13:11:18 +02:00
|
|
|
}
|
|
|
|
|
2012-11-22 15:15:07 +01:00
|
|
|
// QSharedPointer variant
|
|
|
|
template <typename T, typename... Args>
|
2014-02-07 16:34:20 +01:00
|
|
|
_detail::ClosureBase* NewClosure(QSharedPointer<T> sender, const char* signal,
|
|
|
|
QObject* receiver, const char* slot,
|
|
|
|
const Args&... args) {
|
|
|
|
return new _detail::SharedClosure<T, Args...>(sender, signal, receiver, slot,
|
|
|
|
args...);
|
2012-03-09 13:15:24 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
_detail::ClosureBase* NewClosure(QObject* sender, const char* signal,
|
|
|
|
std::function<void()> callback);
|
2012-11-26 09:41:26 +01:00
|
|
|
|
2012-12-13 16:13:38 +01:00
|
|
|
template <typename... Args>
|
2014-02-07 16:34:20 +01:00
|
|
|
_detail::ClosureBase* NewClosure(QObject* sender, const char* signal,
|
|
|
|
std::function<void(Args...)> callback,
|
|
|
|
const Args&... args) {
|
2014-02-06 14:48:00 +01:00
|
|
|
return NewClosure(sender, signal, std::bind(callback, args...));
|
2012-12-13 16:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
2014-02-07 16:34:20 +01:00
|
|
|
_detail::ClosureBase* NewClosure(QObject* sender, const char* signal,
|
|
|
|
void (*callback)(Args...),
|
|
|
|
const Args&... args) {
|
2014-02-06 14:48:00 +01:00
|
|
|
return NewClosure(sender, signal, std::bind(callback, args...));
|
2012-12-13 16:13:38 +01:00
|
|
|
}
|
|
|
|
|
2012-12-13 16:55:23 +01:00
|
|
|
template <typename T, typename Unused, typename... Args>
|
2014-02-07 16:34:20 +01:00
|
|
|
_detail::ClosureBase* NewClosure(QObject* sender, const char* signal,
|
|
|
|
T* receiver, Unused (T::*callback)(Args...),
|
|
|
|
const Args&... args) {
|
2014-02-06 14:48:00 +01:00
|
|
|
return NewClosure(sender, signal, std::bind(callback, receiver, args...));
|
2012-12-13 16:27:00 +01:00
|
|
|
}
|
|
|
|
|
2015-11-26 19:34:41 +01:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
_detail::ClosureBase* NewClosure(QFuture<T> future, QObject* receiver,
|
|
|
|
const char* slot, const Args&... args) {
|
|
|
|
QFutureWatcher<T>* watcher = new QFutureWatcher<T>;
|
|
|
|
watcher->setFuture(future);
|
|
|
|
QObject::connect(watcher, SIGNAL(finished()), watcher, SLOT(deleteLater()));
|
2015-11-26 19:59:44 +01:00
|
|
|
return NewClosure(watcher, SIGNAL(finished()), receiver, slot, args...);
|
2015-11-26 19:34:41 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 15:22:59 +01:00
|
|
|
template <typename T, typename F, typename... Args>
|
|
|
|
_detail::ClosureBase* NewClosure(QFuture<T> future, const F& callback,
|
|
|
|
const Args&... args) {
|
|
|
|
QFutureWatcher<T>* watcher = new QFutureWatcher<T>;
|
|
|
|
watcher->setFuture(future);
|
|
|
|
QObject::connect(watcher, SIGNAL(finished()), watcher, SLOT(deleteLater()));
|
|
|
|
return NewClosure(watcher, SIGNAL(finished()), callback, args...);
|
|
|
|
}
|
|
|
|
|
2012-04-13 15:49:29 +02:00
|
|
|
void DoAfter(QObject* receiver, const char* slot, int msec);
|
2015-07-20 15:35:13 +02:00
|
|
|
void DoAfter(std::function<void()> callback, std::chrono::milliseconds msec);
|
2012-04-13 15:49:29 +02:00
|
|
|
void DoInAMinuteOrSo(QObject* receiver, const char* slot);
|
|
|
|
|
2015-07-20 15:35:13 +02:00
|
|
|
template <typename R, typename P>
|
2015-11-26 19:34:41 +01:00
|
|
|
void DoAfter(std::function<void()> callback,
|
|
|
|
std::chrono::duration<R, P> duration) {
|
2015-07-20 15:35:13 +02:00
|
|
|
QTimer* timer = new QTimer;
|
|
|
|
timer->setSingleShot(true);
|
|
|
|
NewClosure(timer, SIGNAL(timeout()), callback);
|
|
|
|
QObject::connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater()));
|
|
|
|
std::chrono::milliseconds msec =
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(duration);
|
|
|
|
timer->start(msec.count());
|
|
|
|
}
|
|
|
|
|
2011-10-04 18:26:40 +02:00
|
|
|
#endif // CLOSURE_H
|