Tidy up closure implementation.

This commit is contained in:
John Maguire 2011-10-05 11:36:08 +02:00
parent 20a967a19f
commit fc34a244c6
3 changed files with 72 additions and 21 deletions

View File

@ -1,3 +1,20 @@
/* 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/>.
*/
#include "closure.h"
#include "core/logging.h"
@ -6,24 +23,30 @@ Closure::Closure(QObject* sender,
const char* signal,
QObject* receiver,
const char* slot,
const ClosureArgumentWrapper* val1,
const ClosureArgumentWrapper* val2)
const ClosureArgumentWrapper* val0,
const ClosureArgumentWrapper* val1)
: QObject(receiver),
val1_(val1),
val2_(val2) {
val0_(val0),
val1_(val1) {
const QMetaObject* meta_receiver = receiver->metaObject();
QByteArray normalised_slot = QMetaObject::normalizedSignature(slot + 1);
slot_ = meta_receiver->method(
meta_receiver->indexOfSlot(normalised_slot.constData()));
connect(sender, signal, this, SLOT(Invoked()), Qt::AutoConnection);
connect(sender, signal, SLOT(Invoked()));
connect(sender, SIGNAL(destroyed()), SLOT(Cleanup()));
}
void Closure::Invoked() {
qLog(Debug) << "Invoking:" << slot_.signature() << val1_->arg().name();
slot_.invoke(parent(), val1_->arg(), val2_->arg());
delete val1_;
delete val2_;
slot_.invoke(
parent(),
val0_ ? val0_->arg() : QGenericArgument(),
val1_ ? val1_->arg() : QGenericArgument());
deleteLater();
}
void Closure::Cleanup() {
disconnect();
deleteLater();
}

View File

@ -1,19 +1,35 @@
/* 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/>.
*/
#ifndef CLOSURE_H
#define CLOSURE_H
#include <QMetaMethod>
#include <QObject>
#include <boost/scoped_ptr.hpp>
#include "core/logging.h"
class ClosureArgumentWrapper {
public:
virtual ~ClosureArgumentWrapper() {}
virtual QGenericArgument arg() const {
qLog(Debug) << Q_FUNC_INFO;
return QGenericArgument();
}
virtual QGenericArgument arg() const = 0;
};
template<typename T>
@ -22,7 +38,6 @@ class ClosureArgument : public ClosureArgumentWrapper {
ClosureArgument(const T& data) : data_(data) {}
virtual QGenericArgument arg() const {
qLog(Debug) << Q_FUNC_INFO;
return Q_ARG(T, data_);
}
@ -36,17 +51,18 @@ class Closure : public QObject {
public:
Closure(QObject* sender, const char* signal,
QObject* receiver, const char* slot,
const ClosureArgumentWrapper* val1,
const ClosureArgumentWrapper* val2);
const ClosureArgumentWrapper* val0 = 0,
const ClosureArgumentWrapper* val1 = 0);
private slots:
void Invoked();
void Cleanup();
private:
QMetaMethod slot_;
const ClosureArgumentWrapper* val1_;
const ClosureArgumentWrapper* val2_;
boost::scoped_ptr<const ClosureArgumentWrapper> val0_;
boost::scoped_ptr<const ClosureArgumentWrapper> val1_;
};
#define C_ARG(type, data) new ClosureArgument<type>(data)

View File

@ -171,7 +171,7 @@ int GrooveSharkService::SearchAlbums(const QString& query) {
QNetworkReply* reply = CreateRequest("getAlbumSearchResults", parameters, false);
int id = next_pending_search_id_++;
const int id = next_pending_search_id_++;
new Closure(reply, SIGNAL(finished()),
this, SLOT(SearchAlbumsFinished(QNetworkReply*,int)),
@ -182,7 +182,19 @@ int GrooveSharkService::SearchAlbums(const QString& query) {
}
void GrooveSharkService::SearchAlbumsFinished(QNetworkReply* reply, int id) {
qLog(Debug) << reply << id;
reply->deleteLater();
QVariantMap result = ExtractResult(reply);
QVariantList albums = result["albums"].toList();
foreach (const QVariant& v, albums) {
QVariantMap album = v.toMap();
//quint64 album_id = album["AlbumID"].toULongLong();
QString album_name = album["AlbumName"].toString();
QString artist_name = album["ArtistName"].toString();
//QString cover_art = album["CoverArtFilename"].toString();
qLog(Debug) << "Found:" << album_name << artist_name;
}
}
void GrooveSharkService::DoSearch() {