1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtCore module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
24 #include "qxtslotjob_p.h"
27 \class QxtSlotJob QxtSlotJob
31 \brief Execute an arbitary Slot on a QThread.
33 \warning It is essential to understand that the Qobject you pass is not safe to use untill done(); is emited or result() or join() is called.
37 execute \p slot from \p precv on \p thread detached \n
38 returns a QFuture which offers the functions required to get the result.\n
40 QxtFuture f= QxtSlotJob::detach(&thread,&q,SLOT(exec(QString)),Q_ARG(QString, "select NOW();"));
43 \warning keep your hands of \p recv until you called QFuture::result();
45 QxtFuture QxtSlotJob::detach(QThread * thread,QObject* recv, const char* slot,
57 QxtSlotJob * p= new QxtSlotJob(recv,slot,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10);
58 connect(p,SIGNAL(done()),p,SLOT(deleteLater()));
59 return p->exec(thread);
62 Construct a new Job Object that will run \p slot from \p precv with the specified arguments
64 QxtSlotJob::QxtSlotJob(QObject* recv, const char* slot,
76 qxt_d().f=QxtMetaObject::bind(recv,slot,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10);
77 qxt_d().receiver=recv;
78 qxt_d().orginalthread=QThread::currentThread();
80 connect(this,SIGNAL(done()),this,SLOT(pdone()));
83 asks for the result of the execution. \n
84 This calls QxtJob::join() means it will _block_ the current thread untill the Slot has finished execution
86 QVariant QxtSlotJob::result()
92 execute this job on \p thread \n
93 \warning keep your hands of the Object you passed until you called result() or join()
95 QxtFuture QxtSlotJob::exec(QThread *thread)
97 qxt_d().receiver->moveToThread(thread);
99 return QxtFuture(this);
102 void QxtSlotJob::run()
104 qxt_d().r=qVariantFromValue(qxt_d().f->invoke());
105 qxt_d().receiver->moveToThread(qxt_d().orginalthread);
109 void QxtSlotJob::pdone()
111 emit(done(qxt_d().r));
124 \class QxtFuture QxtFuture
128 \brief Reference to a future result of a QxtSlotJob
132 QxtFuture::QxtFuture(const QxtFuture& other):QObject()
135 connect(job,SIGNAL(done()),this,SIGNAL(done()));
136 connect(job,SIGNAL(done(QVariant)),this,SIGNAL(done(QVariant)));
137 waiter=new QxtSignalWaiter(job,SIGNAL(done()));
140 QxtFuture::QxtFuture(QxtSlotJob* j):QObject()
143 connect(job,SIGNAL(done()),this,SIGNAL(done()));
144 connect(job,SIGNAL(done(QVariant)),this,SIGNAL(done(QVariant)));
145 waiter=new QxtSignalWaiter(job,SIGNAL(done()));
148 QxtFuture::~QxtFuture()
153 asks for the result of the execution. \n
154 This calls QxtJob::join() means it will _block_ the current thread untill the Slot has finished execution
156 QVariant QxtFuture::joinedResult()
158 return job->result();
164 asks for the result of the execution. \n
165 waits until the done() signal occured
166 or return a QVariant() if the timout ocures earlier \n
167 This uses QxtSignalWaiter so it will _not_ block your current thread.
168 \warning this function is not reentrant. You have been warned
174 QVariant QxtFuture::delayedResult(int msec)
176 if(!waiter->wait(msec,false))
178 return job->result();