00001 /* 00002 * Copyright (c) 2011-2013 MLBA-Team. All rights reserved. 00003 * 00004 * @MLBA_OPEN_LICENSE_HEADER_START@ 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 * 00018 * @MLBA_OPEN_LICENSE_HEADER_END@ 00019 */ 00020 00021 00022 #ifndef QDISPATCH_SOURCE_H_ 00023 #define QDISPATCH_SOURCE_H_ 00024 00025 #include <QtCore/qobject.h> 00026 #include <QtCore/qscopedpointer.h> 00027 00028 #include "qdispatchglobal.h" 00029 #include "qblockrunnable.h" 00030 00036 QT_BEGIN_HEADER 00037 QT_BEGIN_NAMESPACE 00038 00039 class QRunnable; 00040 class QIODevice; 00041 class QNetworkAccessManager; 00042 class QDispatchQueue; 00043 class QNetworkReply; 00044 00045 QT_MODULE(Dispatch) 00046 00047 class QDispatchSource; 00048 00057 class Q_DISPATCH_EXPORT QDispatchSourceType : public QObject { 00058 00059 Q_OBJECT 00060 00061 protected: 00065 virtual void init(QThread*) = 0; 00066 00067 signals: 00077 void ready(QObject* = NULL); 00078 00079 friend class QDispatchSource; 00080 }; 00081 00090 class Q_DISPATCH_EXPORT QDispatchSourceTypeSignal : public QDispatchSourceType { 00091 00092 public: 00093 QDispatchSourceTypeSignal(QObject* sender, const char* signal); 00094 ~QDispatchSourceTypeSignal(); 00095 00096 protected: 00097 virtual void init(QThread*); 00098 00099 private: 00100 Q_DISABLE_COPY(QDispatchSourceTypeSignal) 00101 00102 }; 00103 00112 class Q_DISPATCH_EXPORT QDispatchSourceTypeIODevice : public QDispatchSourceType { 00113 00114 Q_OBJECT 00115 00116 public: 00117 QDispatchSourceTypeIODevice(QIODevice*); 00118 ~QDispatchSourceTypeIODevice(); 00119 00120 protected: 00121 virtual void init(QThread*); 00122 00123 private slots: 00124 void avail(); 00125 00126 private: 00127 Q_DISABLE_COPY(QDispatchSourceTypeIODevice) 00128 QIODevice* dev; 00129 }; 00130 00139 class Q_DISPATCH_EXPORT QDispatchSourceTypeIODeviceRead : public QDispatchSourceTypeIODevice { 00140 00141 Q_OBJECT 00142 00143 public: 00144 QDispatchSourceTypeIODeviceRead(QIODevice*); 00145 00146 private: 00147 Q_DISABLE_COPY(QDispatchSourceTypeIODeviceRead) 00148 }; 00149 00158 class Q_DISPATCH_EXPORT QDispatchSourceTypeIODeviceWrite : public QDispatchSourceType { 00159 00160 Q_OBJECT 00161 00162 public: 00163 QDispatchSourceTypeIODeviceWrite(QIODevice*); 00164 ~QDispatchSourceTypeIODeviceWrite(); 00165 00166 protected: 00167 virtual void init(QThread*); 00168 00169 private slots: 00170 void finished(qint64); 00171 00172 private: 00173 Q_DISABLE_COPY(QDispatchSourceTypeIODeviceWrite) 00174 QIODevice* dev; 00175 }; 00176 00185 class Q_DISPATCH_EXPORT QDispatchSourceTypeNetworkManager : public QDispatchSourceType { 00186 00187 Q_OBJECT 00188 00189 public: 00190 QDispatchSourceTypeNetworkManager(QNetworkAccessManager*); 00191 ~QDispatchSourceTypeNetworkManager(); 00192 00193 private slots: 00194 void finished(QNetworkReply*); 00195 00196 protected: 00197 void init(QThread*); 00198 00199 private: 00200 Q_DISABLE_COPY(QDispatchSourceTypeNetworkManager) 00201 QNetworkAccessManager* manager; 00202 }; 00203 00222 class Q_DISPATCH_EXPORT QDispatchSource : public QObject { 00223 00224 Q_OBJECT 00225 00226 public: 00231 QDispatchSource(QDispatchSourceType*); 00232 ~QDispatchSource(); 00240 void setHandler(QRunnable*); 00241 #if XDISPATCH_HAS_BLOCKS 00242 00249 inline void setHandler(dispatch_block_t b) { 00250 setHandler( new QBlockRunnable(b) ); 00251 } 00252 #endif 00253 #if XDISPATCH_HAS_FUNCTION 00254 00261 inline void setHandler(const xdispatch::lambda_function& b) { 00262 setHandler( new QLambdaRunnable(b) ); 00263 } 00264 #endif 00265 00269 QDispatchQueue targetQueue() const; 00274 void setTargetQueue(const QDispatchQueue&); 00291 template <typename T> static T* data(){ 00292 return qobject_cast<T*>(_data()); 00293 } 00294 00295 public slots: 00305 void resume(); 00306 void suspend(); 00307 00308 private slots: 00309 void signal(QObject* = NULL); 00310 00311 private: 00312 Q_DISABLE_COPY(QDispatchSource) 00313 class Private; 00314 QScopedPointer<Private> d; 00315 00316 static QObject* _data(); 00317 00318 }; 00319 00320 00321 QT_END_NAMESPACE 00322 QT_END_HEADER 00323 00326 #endif /* QDISPATCH_SOURCE_H_ */