Plasma Engine  2.0
Loading...
Searching...
No Matches
Process.h
1#pragma once
2
3#include <Foundation/Containers/HybridArray.h>
4#include <Foundation/Strings/String.h>
5#include <Foundation/Strings/StringView.h>
6#include <Foundation/Time/Time.h>
7#include <Foundation/Types/Bitflags.h>
8#include <Foundation/Types/Delegate.h>
9#include <Foundation/Types/UniquePtr.h>
10
11using plOsProcessHandle = void*;
12using plOsProcessID = plUInt32;
13
14#if PL_ENABLED(PL_SUPPORTS_PROCESSES)
15enum class plProcessState
16{
17 NotStarted,
18 Running,
19 Finished
20};
21
23struct PL_FOUNDATION_DLL plProcessOptions
24{
26 plString m_sProcess;
27
29 plString m_sWorkingDirectory;
30
33
35 bool m_bHideConsoleWindow = true;
36
38 plDelegate<void(plStringView)> m_onStdOut;
39
41 plDelegate<void(plStringView)> m_onStdError;
42
48 void AddArgument(const plFormatString& arg);
49
51 template <typename... ARGS>
52 void AddArgument(plStringView sFormat, ARGS&&... args)
53 {
54 AddArgument(plFormatStringImpl<ARGS...>(sFormat, std::forward<ARGS>(args)...));
55 }
56
60 void AddCommandLine(plStringView sCmdLine);
61
63 void BuildCommandLineString(plStringBuilder& out_sCmdLine) const;
64};
65
67struct plProcessLaunchFlags
68{
69 using StorageType = plUInt32;
70
71 enum Enum
72 {
73 None = 0,
74 Detached = PL_BIT(0),
75 Suspended = PL_BIT(1),
76 Default = None
77 };
78
79 struct Bits
80 {
81 StorageType Detached : 1;
82 StorageType Suspended : 1;
83 };
84};
85
86PL_DECLARE_FLAGS_OPERATORS(plProcessLaunchFlags);
87
89class PL_FOUNDATION_DLL plProcess
90{
91 PL_DISALLOW_COPY_AND_ASSIGN(plProcess);
92
93public:
94 plProcess();
95 plProcess(plProcess&& rhs);
96
103 ~plProcess();
104
106 static plResult Execute(const plProcessOptions& opt, plInt32* out_pExitCode = nullptr);
107
114 plResult Launch(const plProcessOptions& opt, plBitflags<plProcessLaunchFlags> launchFlags = plProcessLaunchFlags::None);
115
118 plResult ResumeSuspended();
119
126 plResult WaitToFinish(plTime timeout = plTime::MakeZero());
127
129 plResult Terminate();
130
132 plInt32 GetExitCode() const;
133
137 plProcessState GetState() const;
138
143 void Detach();
144
146 plOsProcessHandle GetProcessHandle() const;
147
149 plOsProcessID GetProcessID() const;
150
152 static plOsProcessID GetCurrentProcessID();
153
154private:
155 void BuildFullCommandLineString(const plProcessOptions& opt, plStringView sProcess, plStringBuilder& cmd) const;
156
158
159 // the default value is used by GetExitCode() to determine whether it has to be reevaluated
160 mutable plInt32 m_iExitCode = -0xFFFF;
161
162 plString m_sProcess;
163 plDelegate<void(plStringView)> m_OnStdOut;
164 plDelegate<void(plStringView)> m_OnStdError;
165 mutable plTime m_ProcessExited = plTime::MakeZero();
166};
167#endif
Implements formating of strings with placeholders and formatting options.
Definition FormatString.h:59
Definition FormatStringImpl.h:9
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
A generic delegate class which supports static functions and member functions.
Definition Delegate.h:76
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
PL_ALWAYS_INLINE static constexpr plTime MakeZero()
Creates an instance of plTime that was initialized with zero.
Definition Time.h:42