We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / core / qxtslotjob.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtCore module of the Qt eXTension library
6 **
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
9 ** IBM.
10 **
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.
15 **
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.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtslotjob_p.h"
25
26 /**
27 \class QxtSlotJob QxtSlotJob
28
29 \ingroup QxtCore
30
31 \brief Execute an arbitary Slot on a QThread.
32
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.
34 */
35
36 /*!
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
39 \code
40     QxtFuture f= QxtSlotJob::detach(&thread,&q,SLOT(exec(QString)),Q_ARG(QString, "select NOW();"));
41 \endcode
42
43 \warning keep your hands of \p recv until you called QFuture::result();
44 */
45 QxtFuture QxtSlotJob::detach(QThread * thread,QObject* recv, const char* slot,
46         QGenericArgument p1,
47         QGenericArgument p2,
48         QGenericArgument p3,
49         QGenericArgument p4,
50         QGenericArgument p5,
51         QGenericArgument p6,
52         QGenericArgument p7,
53         QGenericArgument p8,
54         QGenericArgument p9,
55         QGenericArgument p10) 
56     {
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);
60     }
61 /*!
62 Construct a new Job Object that will run \p slot from \p precv with the specified arguments
63 */
64 QxtSlotJob::QxtSlotJob(QObject* recv, const char* slot,
65         QGenericArgument p1,
66         QGenericArgument p2,
67         QGenericArgument p3,
68         QGenericArgument p4,
69         QGenericArgument p5,
70         QGenericArgument p6,
71         QGenericArgument p7,
72         QGenericArgument p8,
73         QGenericArgument p9,
74         QGenericArgument p10) 
75     {
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();
79
80         connect(this,SIGNAL(done()),this,SLOT(pdone()));
81     }
82 /*!
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
85 */
86 QVariant QxtSlotJob::result()
87     {
88         join();
89         return qxt_d().r;
90     }
91 /*!
92 execute this job on \p thread \n
93 \warning keep your hands of the Object you passed until you called result() or join()
94 */
95 QxtFuture QxtSlotJob::exec(QThread *thread)
96     {
97         qxt_d().receiver->moveToThread(thread);
98         QxtJob::exec(thread);
99         return QxtFuture(this);
100     }
101
102 void QxtSlotJob::run()
103     {
104         qxt_d().r=qVariantFromValue(qxt_d().f->invoke());
105         qxt_d().receiver->moveToThread(qxt_d().orginalthread);
106     }
107
108
109 void QxtSlotJob::pdone()
110 {
111     emit(done(qxt_d().r));
112 }
113
114
115
116
117
118
119
120
121
122
123 /**
124 \class QxtFuture QxtFuture
125
126 \ingroup QxtCore
127
128 \brief Reference to a future result of a QxtSlotJob
129
130 */
131
132 QxtFuture::QxtFuture(const QxtFuture& other):QObject()
133 {
134     job=other.job;
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()));
138 }
139
140 QxtFuture::QxtFuture(QxtSlotJob* j):QObject()
141 {
142     job=j;
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()));
146 }
147
148 QxtFuture::~QxtFuture()
149 {
150     delete waiter;
151 }
152 /*!
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
155 */
156 QVariant QxtFuture::joinedResult()
157 {
158     return job->result();
159 }
160
161
162
163 /*!
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
169
170 */
171
172
173
174 QVariant QxtFuture::delayedResult(int msec)
175 {
176     if(!waiter->wait(msec,false))
177         return QVariant();
178     return job->result();
179 }
180
181
182
183
184
185
186
187