LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
process.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "process.h"
10#include <QProcess>
11#include <QtDebug>
12#include <util/sll/visitor.h>
13
14namespace LC::Util
15{
16 QDebug operator<< (QDebug dbg, const ProcessFailedToStart& failed)
17 {
18 QDebugStateSaver saver { dbg };
19 dbg.nospace () << "ProcessFailedToStart { " << failed.Error_ << " }";
20 return dbg;
21 }
22
23 QDebug operator<< (QDebug dbg, const ProcessCrashed& crashed)
24 {
25 QDebugStateSaver saver { dbg };
26 dbg.nospace () << "ProcessCrashed { code: " << crashed.Code_ << " }";
27 return dbg;
28 }
29
30 QDebug operator<< (QDebug dbg, const ProcessExited& exited)
31 {
32 QDebugStateSaver saver { dbg };
33 dbg.nospace () << "ProcessExited { code: " << exited.Code_ << " }";
34 return dbg;
35 }
36
37 QDebug operator<< (QDebug dbg, const ProcessOutcome& outcome)
38 {
39 Visit (outcome, [&dbg] (const auto& alternative) { dbg << alternative; });
40 return dbg;
41 }
42}
43
44namespace LC::Util::detail
45{
46 bool ProcessAwaiter::await_ready () const noexcept
47 {
48 return Process_.state () == QProcess::NotRunning;
49 }
50
51 void ProcessAwaiter::await_suspend (std::coroutine_handle<> handle)
52 {
53 const auto resumeLater = [this, handle]
54 {
55 if (!await_ready ())
56 return;
57
58 QObject::disconnect (std::move (FinishedConn_).Release ());
59 QObject::disconnect (std::move (ErrorConn_).Release ());
60 QMetaObject::invokeMethod (&CoroResumeGuard_, handle, Qt::QueuedConnection);
61 };
62 FinishedConn_ = QObject::connect (&Process_, &QProcess::finished, resumeLater);
63 ErrorConn_ = QObject::connect (&Process_, &QProcess::errorOccurred, resumeLater);
64 }
65
67 {
68 if (Process_.error () == QProcess::FailedToStart)
69 return ProcessFailedToStart { Process_.errorString () };
70
71 if (Process_.exitStatus () == QProcess::CrashExit)
72 return ProcessCrashed { Process_.exitCode () };
73
74 return ProcessExited { Process_.exitCode () };
75 }
76}
std::variant< ProcessFailedToStart, ProcessCrashed, ProcessExited > ProcessOutcome
Definition process.h:45
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition either.h:189
QDebug operator<<(QDebug dbg, const CtString< N, Char > &str)
RaiiSignalConnection FinishedConn_
Definition process.h:60
void await_suspend(std::coroutine_handle<> handle)
Definition process.cpp:51
RaiiSignalConnection ErrorConn_
Definition process.h:61
ProcessOutcome await_resume() const
Definition process.cpp:66
bool await_ready() const noexcept
Definition process.cpp:46