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-2025 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#include "AUI/Common/IStringable.h"
   27
   28#if AUI_PLATFORM_WIN
   29#include <AUI/Platform/win32/AWin32EventWait.h>
   30#include <AUI/Platform/win32/WinIoAsync.h>
   31#include <windows.h>
   32#include "Pipe.h"
   33#else
   34#include "AUI/Platform/unix/UnixIoAsync.h"
   35#endif
   36
   37class AChildProcess;
   38
   43AUI_ENUM_FLAG(ASubProcessExecutionFlags) {
   47    MERGE_STDOUT_STDERR = 0b001,
   48
   52    TIE_STDOUT = 0b010,
   53
   57    TIE_STDERR = 0b100, DEFAULT = 0,
   58
   65    DETACHED = 0b1000,
   66};
   67
   68class AProcessException : public AException {
   69public:
   70    AProcessException(const AString& message) : AException(message) {}
   71};
   72
   88class API_AUI_CORE AProcess : public aui::noncopyable {
   89public:
   90    virtual ~AProcess() = default;
   91
   95    virtual APath getModuleName() = 0;
   96
  100    virtual APath getPathToExecutable() = 0;
  101
  105    virtual uint32_t getPid() const noexcept = 0;
  106
  111    virtual int waitForExitCode() = 0;
  112
  116    virtual size_t processMemory() const = 0;
  117
  124    struct ArgSingleString {
  125        AString arg;
  126    };
  127
  131    struct ArgStringList {
  136        AStringVector list;
  137
  153    };
  154
  162        APath executable;
  163
  177        std::variant<ArgSingleString, ArgStringList> args;
  178
  183        APath workDir;
  184    };
  185
  192
  200    [[deprecated("use AProcess::create instead")]]
  201    static _<AChildProcess> make(AString applicationFile, AString args = {}, APath workingDirectory = {}) {
  202        return create(
  203            { .executable = std::move(applicationFile),
  204              .args = ArgSingleString { std::move(args) },
  205              .workDir = std::move(workingDirectory) });
  206    }
  207
  216    [[deprecated("use auto process = AProcess::make(); process->run(); process->waitForExitCode()")]]
  217    static int executeWaitForExit(
  218        AString applicationFile, AString args = {}, APath workingDirectory = {},
  219        ASubProcessExecutionFlags flags = ASubProcessExecutionFlags::DEFAULT);
  220
  221#if AUI_PLATFORM_WIN
  222
  231    static void executeAsAdministrator(
  232        const AString& applicationFile, const AString& args = {}, const APath& workingDirectory = {});
  233
  234#endif
  235
  239    static AVector<_<AProcess>> all();
  240
  244    static _<AProcess> self();
  245
  250    static _<AProcess> findAnotherSelfInstance(const AString& yourProjectName);
  251
  260    static _<AProcess> fromPid(uint32_t pid);
  261
  262    void kill() const noexcept;
  263};
  264
  268class API_AUI_CORE AChildProcess : public AProcess, public AObject, public IStringable {
  269    friend class AProcess;
  270
  271public:
  272    ~AChildProcess();
  273
  274    [[nodiscard]]
  275    const auto& getApplicationFile() const {
  276        return mInfo.executable;
  277    }
  278
  279    [[nodiscard]]
  280    const auto& getArgs() const {
  281        return mInfo.args;
  282    }
  283
  284    [[nodiscard]]
  285    const auto& getWorkingDirectory() const {
  286        return mInfo.workDir;
  287    }
  288
  289    APath getPathToExecutable() override {
  290        return getApplicationFile();
  291    }
  292
  293    [[nodiscard]]
  294    AOptional<int> exitCodeNonBlocking() const noexcept {
  295        if (mExitCode.hasValue()) {
  296            return *mExitCode;
  297        }
  298        return std::nullopt;
  299    }
  300
  301    [[nodiscard]]
  302    bool isFinished() const noexcept {
  303        return mExitCode.hasValue();
  304    }
  305
  309    void run(ASubProcessExecutionFlags flags = ASubProcessExecutionFlags::DEFAULT);
  310
  315    int waitForExitCode() override;
  316
  317    uint32_t getPid() const noexcept override;
  318
  319    size_t processMemory() const override;
  320
  321    APath getModuleName() override;
  322
  323    [[nodiscard]]
  324    const _<IOutputStream>& getStdInStream() const {
  325        return mStdInStream;
  326    }
  327    AString toString() const override;
  328
  329signals:
  330    emits<> finished;
  331    emits<AByteBuffer> stdOut;
  332    emits<AByteBuffer> stdErr;
  333
  334private:
  335    AChildProcess() = default;
  336    ProcessCreationInfo mInfo;
  337
  338    _<IOutputStream> mStdInStream;
  339
  340
  341    AFuture<int> mExitCode;
  342#if AUI_PLATFORM_WIN
  343    PROCESS_INFORMATION mProcessInformation;
  344    AWin32EventWait mExitEvent;
  345    WinIoAsync mStdoutAsync;
  346#else
  347    pid_t mPid;
  348    _<AThread> mWatchdog;
  349    UnixIoAsync mStdoutAsync;
  350#endif
  351};
Definition AProcess.h:268
AString toString() const override
size_t processMemory() const override
Obtain process memory usage.
void run(ASubProcessExecutionFlags flags=ASubProcessExecutionFlags::DEFAULT)
Launches process.
APath getModuleName() override
int waitForExitCode() override
Wait for process to be finished.
uint32_t getPid() const noexcept override
APath getPathToExecutable() override
Definition AProcess.h:289
Represents a value that will be available at some point in the future.
Definition AFuture.h:621
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
An add-on to AString with functions for working with the path.
Definition APath.h:128
Retrieves information about processes.
Definition AProcess.h:88
virtual uint32_t getPid() const noexcept=0
virtual APath getPathToExecutable()=0
static _< AProcess > self()
virtual int waitForExitCode()=0
Wait for process to be finished and returns exit code.
static int executeWaitForExit(AString applicationFile, AString args={}, APath workingDirectory={}, ASubProcessExecutionFlags flags=ASubProcessExecutionFlags::DEFAULT)
Launches executable.
static _< AChildProcess > make(AString applicationFile, AString args={}, APath workingDirectory={})
Launches an executable.
Definition AProcess.h:201
static AVector< _< AProcess > > all()
static _< AChildProcess > create(ProcessCreationInfo args)
Launches an executable.
static _< AProcess > findAnotherSelfInstance(const AString &yourProjectName)
tempFileName file name which will be used as lock
virtual APath getModuleName()=0
virtual size_t processMemory() const =0
Obtain process memory usage.
static void executeAsAdministrator(const AString &applicationFile, const AString &args={}, const APath &workingDirectory={})
Launches executable with administrator rights. (Windows only)
static _< AProcess > fromPid(uint32_t pid)
An AVector with string-related functions.
Definition AStringVector.h:22
Represents a Unicode character string.
Definition AString.h:38
A std::vector with AUI extensions.
Definition AVector.h:39
Encapsulates calls to RegisterWaitForSingleObject/UnregisterWait.
Definition AWin32EventWait.h:21
Definition IOutputStream.h:18
Object that can be converted to string.
Definition IStringable.h:29
Definition UnixIoAsync.h:18
Definition WinIoAsync.h:19
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
#define AUI_ENUM_FLAG(name)
Make a bitfield-style enum class.
Definition AEnumerate.h:227
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:572
@ DEFAULT
There's no concrete input action. Let the OS decide which action is the most appropriate.
Definition ATextInputActionIcon.h:36
Process arguments represented as a single string.
Definition AProcess.h:124
Process arguments represented as array of strings.
Definition AProcess.h:131
AStringVector list
Definition AProcess.h:136
bool win32WrapWhitespaceArgumentsWithQuots
Definition AProcess.h:152
Process creation info.
Definition AProcess.h:158
APath executable
Definition AProcess.h:162
APath workDir
Definition AProcess.h:183
std::variant< ArgSingleString, ArgStringList > args
Definition AProcess.h:177
Forbids copy of your class.
Definition values.h:45