AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
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
57class AProcessException : public AException {
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
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
191 [[deprecated("use auto process = AProcess::make(); process->run(); process->waitForExitCode()")]]
192 static int executeWaitForExit(
193 AString applicationFile, AString args = {}, APath workingDirectory = {},
194 ASubProcessExecutionFlags flags = ASubProcessExecutionFlags::DEFAULT);
195
196#if AUI_PLATFORM_WIN
197
205 static void executeAsAdministrator(
206 const AString& applicationFile, const AString& args = {}, const APath& workingDirectory = {});
207
208#endif
209
213 static AVector<_<AProcess>> all();
214
218 static _<AProcess> self();
219
224 static _<AProcess> findAnotherSelfInstance(const AString& yourProjectName);
225
229 static _<AProcess> fromPid(uint32_t pid);
230
231 void kill() const noexcept;
232};
233
237class API_AUI_CORE AChildProcess : public AProcess, public AObject {
238 friend class AProcess;
239
240public:
241 ~AChildProcess();
242
243 [[nodiscard]]
244 const auto& getApplicationFile() const {
245 return mInfo.executable;
246 }
247
248 [[nodiscard]]
249 const auto& getArgs() const {
250 return mInfo.args;
251 }
252
253 [[nodiscard]]
254 const auto& getWorkingDirectory() const {
255 return mInfo.workDir;
256 }
257
259 return getApplicationFile();
260 }
261
262 [[nodiscard]]
263 AOptional<int> exitCodeNonBlocking() const noexcept {
264 if (mExitCode.hasValue()) {
265 return *mExitCode;
266 }
267 return std::nullopt;
268 }
269
270 [[nodiscard]]
271 bool isFinished() const noexcept {
272 return mExitCode.hasValue();
273 }
274
278 void run(ASubProcessExecutionFlags flags = ASubProcessExecutionFlags::DEFAULT);
279
284 int waitForExitCode() override;
285
286 uint32_t getPid() const noexcept override;
287
288 size_t processMemory() const override;
289
290 APath getModuleName() override;
291
292 [[nodiscard]]
293 const _<IOutputStream>& getStdInStream() const {
294 return mStdInStream;
295 }
296
297signals:
298 emits<> finished;
299 emits<AByteBuffer> stdOut;
300 emits<AByteBuffer> stdErr;
301
302private:
303 AChildProcess() = default;
304 ProcessCreationInfo mInfo;
305
306 _<IOutputStream> mStdInStream;
307
308
309 AFuture<int> mExitCode;
310#if AUI_PLATFORM_WIN
311 PROCESS_INFORMATION mProcessInformation;
312 AWin32EventWait mExitEvent;
313 WinIoAsync mStdoutAsync;
314#else
315 pid_t mPid;
316 _<AThread> mWatchdog;
317 UnixIoAsync mStdoutAsync;
318#endif
319};
Definition AProcess.h:237
APath getPathToExecutable() override
Definition AProcess.h:258
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:107
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.
static _< AChildProcess > create(ProcessCreationInfo args)
Launches an executable.
Definition AProcess.cpp:27
An AVector with string-related functions.
Definition AStringVector.h:22
Represents a Unicode character string.
Definition AString.h:37
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:178
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:348
@ DEFAULT
There's no concrete input action. Let the OS decide which action is the most appropriate.
Definition ATextInputActionIcon.h:39
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
bool win32WrapWhitespaceArgumentsWithQuots
Definition AProcess.h:128
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