AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AProcess.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12//
13// Created by alex2 on 31.10.2020.
14//
15
16#pragma once
17
18#include <AUI/IO/APath.h>
19#include <AUI/Common/AException.h>
20#include <AUI/Common/AVector.h>
21#include <AUI/Common/AObject.h>
22#include <AUI/Common/ASignal.h>
23#include <AUI/IO/IInputStream.h>
24#include <AUI/IO/IOutputStream.h>
25#include <AUI/Thread/AFuture.h>
26
27#if AUI_PLATFORM_WIN
28#include <AUI/Platform/win32/AWin32EventWait.h>
29#include <AUI/Platform/win32/WinIoAsync.h>
30#include <windows.h>
31#include "Pipe.h"
32#else
33#include "AUI/Platform/unix/UnixIoAsync.h"
34#endif
35
36class AChildProcess;
37
42AUI_ENUM_FLAG(ASubProcessExecutionFlags) {
46 MERGE_STDOUT_STDERR = 0b001,
50 TIE_STDOUT = 0b010,
54 TIE_STDERR = 0b100, DEFAULT = 0
55};
56
58public:
59 AProcessException(const AString& message) : AException(message) {}
60};
61
65class API_AUI_CORE AProcess : public aui::noncopyable {
66public:
67 virtual ~AProcess() = default;
68
72 virtual APath getModuleName() = 0;
73
78
82 virtual uint32_t getPid() const noexcept = 0;
83
88 virtual int waitForExitCode() = 0;
89
93 virtual size_t processMemory() const = 0;
94
102 AString arg;
103 };
104
114
128 bool win32WrapWhitespaceArgumentsWithQuots = true;
129 };
130
139
152 std::variant<ArgSingleString, ArgStringList> args;
153
159 };
160
167
175 [[deprecated("use AProcess::create instead")]]
176 static _<AChildProcess> make(AString applicationFile, AString args = {}, APath workingDirectory = {}) {
177 return create(
178 { .executable = std::move(applicationFile),
179 .args = ArgSingleString { std::move(args) },
180 .workDir = std::move(workingDirectory) });
181 }
182
190 [[deprecated("use auto process = AProcess::make(); process->run(); process->waitForExitCode()")]]
191 static int executeWaitForExit(
192 AString applicationFile, AString args = {}, APath workingDirectory = {},
193 ASubProcessExecutionFlags flags = ASubProcessExecutionFlags::DEFAULT);
194
195#if AUI_PLATFORM_WIN
203 static void executeAsAdministrator(
204 const AString& applicationFile, const AString& args = {}, const APath& workingDirectory = {});
205#endif
206
210 static AVector<_<AProcess>> all();
211
215 static _<AProcess> self();
216
221 static _<AProcess> findAnotherSelfInstance(const AString& yourProjectName);
222
226 static _<AProcess> fromPid(uint32_t pid);
227
228 void kill() const noexcept;
229};
230
234class API_AUI_CORE AChildProcess : public AProcess, public AObject {
235 friend class AProcess;
236
237public:
239
240 [[nodiscard]]
241 const auto& getApplicationFile() const {
242 return mInfo.executable;
243 }
244
245 [[nodiscard]]
246 const auto& getArgs() const {
247 return mInfo.args;
248 }
249
250 [[nodiscard]]
251 const auto& getWorkingDirectory() const {
252 return mInfo.workDir;
253 }
254
256 return getApplicationFile();
257 }
258
259 [[nodiscard]]
260 AOptional<int> exitCodeNonBlocking() const noexcept {
261 if (mExitCode.hasValue()) {
262 return *mExitCode;
263 }
264 return std::nullopt;
265 }
266
267 [[nodiscard]]
268 bool isFinished() const noexcept {
269 return mExitCode.hasValue();
270 }
271
275 void run(ASubProcessExecutionFlags flags = ASubProcessExecutionFlags::DEFAULT);
276
281 int waitForExitCode() override;
282
283 uint32_t getPid() const noexcept override;
284
285 size_t processMemory() const override;
286
287 APath getModuleName() override;
288
289 [[nodiscard]]
290 const _<IOutputStream>& getStdInStream() const {
291 return mStdInStream;
292 }
293
294signals:
295 emits<> finished;
296 emits<AByteBuffer> stdOut;
297 emits<AByteBuffer> stdErr;
298
299private:
300 AChildProcess() = default;
301 ProcessCreationInfo mInfo;
302
303 _<IOutputStream> mStdInStream;
304
305
306 AFuture<int> mExitCode;
307#if AUI_PLATFORM_WIN
308 PROCESS_INFORMATION mProcessInformation;
309 AWin32EventWait mExitEvent;
310 WinIoAsync mStdoutAsync;
311#else
312 pid_t mPid;
313 _<AThread> mWatchdog;
314 UnixIoAsync mStdoutAsync;
315#endif
316};
Definition: AProcess.h:234
APath getPathToExecutable() override
Definition: AProcess.h:255
Abstract AUI exception.
Definition: AException.h:29
Represents a value that will be available at some point in the future.
Definition: AFuture.h:620
A base object class.
Definition: AObject.h:49
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
An add-on to AString with functions for working with the path.
Definition: APath.h:106
Definition: AProcess.h:57
Definition: AProcess.h:65
virtual uint32_t getPid() const noexcept=0
virtual APath getPathToExecutable()=0
virtual int waitForExitCode()=0
Wait for process to be finished and returns exit code.
static _< AChildProcess > make(AString applicationFile, AString args={}, APath workingDirectory={})
Launches an executable.
Definition: AProcess.h:176
virtual APath getModuleName()=0
virtual size_t processMemory() const =0
Obtain process memory usage.
An AVector with string-related functions.
Definition: AStringVector.h:22
Represents a Unicode character string.
Definition: AString.h:37
A std::vector with AUI extensions.
Definition: AVector.h:38
Encapsulates calls to RegisterWaitForSingleObject/UnregisterWait.
Definition: AWin32EventWait.h:21
Definition: IOutputStream.h:20
Definition: UnixIoAsync.h:18
Definition: WinIoAsync.h:19
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
API_AUI_CORE const ACommandLineArgs & args() noexcept
Definition: OSAndroid.cpp:29
AUI_ENUM_FLAG(ASubProcessExecutionFlags)
Flag enum for AChildProcess::run.
Definition: AProcess.h:42
@ DEFAULT
There's no concrete input action. Let the OS decide which action is the most appropriate.
Process arguments represented as a single string.
Definition: AProcess.h:101
Process arguments represented as array of strings.
Definition: AProcess.h:108
AStringVector list
Definition: AProcess.h:113
Process creation info.
Definition: AProcess.h:134
APath executable
Definition: AProcess.h:138
APath workDir
Definition: AProcess.h:158
std::variant< ArgSingleString, ArgStringList > args
Definition: AProcess.h:152
Forbids copy of your class.
Definition: values.h:40