StormByte C++ Library: System module 0.0.9999
StormByte-System is a StormByte library module for encapsulating different process handling over different operating system's complexities
Loading...
Searching...
No Matches
process.hxx
1#pragma once
2
3#include <StormByte/system/visibility.h>
4
5#include <filesystem>
6#include <iostream>
7#include <thread>
8#ifdef LINUX
9#include <unistd.h>
10#else
11#include <windows.h>
12#endif
13#include <vector>
14
19namespace StormByte::System {
20 class Pipe;
25 struct {} typedef _EoF;
26
30 static constexpr const _EoF EoF = {};
31
37 class STORMBYTE_SYSTEM_PUBLIC Process {
38 public:
44 Process(const std::filesystem::path& prog, const std::vector<std::string>& args = std::vector<std::string>());
45
51 Process(std::filesystem::path&& prog, std::vector<std::string>&& args = std::vector<std::string>());
52
57 Process(const Process& proc) = delete;
58
63 Process(Process&& proc) noexcept;
64
69 Process& operator=(const Process& proc) = delete;
70
75 Process& operator=(Process&& proc) noexcept;
76
80 virtual ~Process() noexcept;
81 #ifdef LINUX
86 int Wait() noexcept;
87
92 pid_t Pid() noexcept;
93 #else
98 DWORD Wait() noexcept;
99
104 PROCESS_INFORMATION Pid();
105 #endif
109 void Suspend();
110
114 void Resume();
115
122
128 std::string& operator>>(std::string& str) const;
129
136 friend STORMBYTE_SYSTEM_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Process& proc);
137
143 Process& operator<<(const std::string& str);
144
149 void operator<<(const System::_EoF& eof);
150
156 enum class Status:unsigned short {
157 RUNNING,
158 SUSPENDED,
159 TERMINATED
160 };
161
162 protected:
164 #ifdef LINUX
165 pid_t m_pid;
166 #else
167 STARTUPINFO m_siStartInfo;
168 PROCESS_INFORMATION m_piProcInfo;
169 #endif
170 Pipe* m_pstdout;
171 Pipe* m_pstdin;
172 Pipe* m_pstderr;
173 std::filesystem::path m_program;
174 std::vector<std::string> m_arguments;
175 std::unique_ptr<std::thread> m_forwarder;
176
177 private:
182 void Send(const std::string& str);
183
187 void Run();
188
193 void ConsumeAndForward(Process&);
194 #ifdef WINDOWS
199 std::wstring FullCommand() const;
200 #endif
201 };
208 STORMBYTE_SYSTEM_PUBLIC std::ostream& operator<<(std::ostream&, const Process&);
209}
Process class for running external programs They will run immediately after creation.
Definition process.hxx:37
PROCESS_INFORMATION m_piProcInfo
Process information.
Definition process.hxx:168
Process & operator=(const Process &proc)=delete
void operator<<(const System::_EoF &eof)
std::unique_ptr< std::thread > m_forwarder
Forwarder thread.
Definition process.hxx:175
Process & operator=(Process &&proc) noexcept
Process(const std::filesystem::path &prog, const std::vector< std::string > &args=std::vector< std::string >())
Process(const Process &proc)=delete
Pipe * m_pstderr
Standard error pipe.
Definition process.hxx:172
Process(Process &&proc) noexcept
virtual ~Process() noexcept
Process & operator<<(const std::string &str)
Process(std::filesystem::path &&prog, std::vector< std::string > &&args=std::vector< std::string >())
Status
Process status.
Definition process.hxx:156
std::vector< std::string > m_arguments
Program arguments.
Definition process.hxx:174
friend STORMBYTE_SYSTEM_PUBLIC std::ostream & operator<<(std::ostream &ostream, const Process &proc)
std::string & operator>>(std::string &str) const
Process & operator>>(Process &proc)
Pipe * m_pstdout
Standard output pipe.
Definition process.hxx:170
std::filesystem::path m_program
Program path.
Definition process.hxx:173
Status m_status
Process status.
Definition process.hxx:163
STARTUPINFO m_siStartInfo
Startup information.
Definition process.hxx:167
Pipe * m_pstdin
Standard input pipe.
Definition process.hxx:171
Forward declaration of Pipe class.
Definition process.hxx:25