Clementine-audio-player-Mac.../src/core/closure.cpp

72 lines
2.1 KiB
C++
Raw Normal View History

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
#include "closure.h"
#include "core/logging.h"
Closure::Closure(QObject* sender,
const char* signal,
QObject* receiver,
const char* slot,
2011-10-05 11:36:08 +02:00
const ClosureArgumentWrapper* val0,
const ClosureArgumentWrapper* val1)
2011-10-04 18:26:40 +02:00
: QObject(receiver),
callback_(NULL),
2011-10-05 11:36:08 +02:00
val0_(val0),
val1_(val1) {
2011-10-04 18:26:40 +02:00
const QMetaObject* meta_receiver = receiver->metaObject();
QByteArray normalised_slot = QMetaObject::normalizedSignature(slot + 1);
slot_ = meta_receiver->method(
meta_receiver->indexOfSlot(normalised_slot.constData()));
2011-10-05 11:36:08 +02:00
connect(sender, signal, SLOT(Invoked()));
connect(sender, SIGNAL(destroyed()), SLOT(Cleanup()));
2011-10-04 18:26:40 +02:00
}
Closure::Closure(QObject* sender,
const char* signal,
std::tr1::function<void()> callback)
: callback_(callback) {
connect(sender, signal, SLOT(Invoked()));
connect(sender, SIGNAL(destroyed()), SLOT(Cleanup()));
}
2011-10-04 18:26:40 +02:00
void Closure::Invoked() {
if (callback_) {
callback_();
} else {
slot_.invoke(
parent(),
val0_ ? val0_->arg() : QGenericArgument(),
val1_ ? val1_->arg() : QGenericArgument());
}
2011-10-05 11:36:08 +02:00
deleteLater();
}
2011-10-04 18:26:40 +02:00
2011-10-05 11:36:08 +02:00
void Closure::Cleanup() {
disconnect();
deleteLater();
2011-10-04 18:26:40 +02:00
}
2011-10-06 13:11:18 +02:00
Closure* NewClosure(
QObject* sender, const char* signal,
QObject* receiver, const char* slot) {
return new Closure(sender, signal, receiver, slot);
}