File indexing completed on 2024-05-12 17:08:33
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Ivan Cukic <ivan.cukic(at)kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 // 0008 // W A R N I N G 0009 // ------------- 0010 // 0011 // This file is not part of the AsynQt API. It exists purely as an 0012 // implementation detail. This header file may change from version to 0013 // version without notice, or even be removed. 0014 // 0015 // We mean it. 0016 // 0017 0018 #include <asynqt/operations/transform.h> 0019 0020 0021 namespace AsynQt 0022 { 0023 namespace detail 0024 { 0025 template<typename _Result, typename _Function> 0026 class ProcessFutureInterface : public QObject, public QFutureInterface<_Result> 0027 { 0028 public: 0029 ProcessFutureInterface(QProcess *process, _Function map) 0030 : m_process(process) 0031 , m_map(map) 0032 { 0033 } 0034 0035 QFuture<_Result> start() 0036 { 0037 m_running = true; 0038 auto onProcessFinished = [this]() { 0039 this->finished(); 0040 }; 0041 QObject::connect(m_process, 0042 // Pretty new Qt connect syntax :) 0043 (void (QProcess::*)(int, QProcess::ExitStatus)) & QProcess::finished, 0044 this, 0045 onProcessFinished, 0046 Qt::QueuedConnection); 0047 0048 QObject::connect(m_process, &QProcess::errorOccurred, this, onProcessFinished, Qt::QueuedConnection); 0049 0050 this->reportStarted(); 0051 0052 m_process->start(); 0053 0054 return this->future(); 0055 } 0056 0057 void finished(); 0058 0059 private: 0060 QProcess *m_process; 0061 _Function m_map; 0062 bool m_running; 0063 }; 0064 0065 template<typename _Result, typename _Function> 0066 void ProcessFutureInterface<_Result, _Function>::finished() 0067 { 0068 if (m_running) { 0069 m_running = false; 0070 this->reportResult(m_map(m_process)); 0071 this->reportFinished(); 0072 } 0073 } 0074 0075 } // namespace detail 0076 } // namespace AsynQt