StormByte C++ Library: System module 1.0.1.9999
StormByte-System is the C++26 process and environment module of the StormByte suite.
Loading...
Searching...
No Matches
process.hxx
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-System.
5 *
6 * StormByte-System is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte-System is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte-System. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
22#include <StormByte/system/visibility.h>
23
24#include <filesystem>
25#include <iostream>
26#include <memory>
27#include <thread>
28#ifdef WINDOWS
29#include <windows.h>
30#else
31#include <unistd.h>
32#endif
33#include <vector>
34
38namespace StormByte::System {
39 class Pipe;
40
45 struct {} typedef _EoF;
46
50 static constexpr const _EoF EoF = {};
51
60 class STORMBYTE_SYSTEM_PUBLIC Process {
61 public:
67 Process(const std::filesystem::path& prog, const std::vector<std::string>& args = std::vector<std::string>());
68
74 Process(std::filesystem::path&& prog, std::vector<std::string>&& args = std::vector<std::string>());
75
79 Process(const Process& proc) = delete;
80
84 Process(Process&& proc) noexcept;
85
89 Process& operator=(const Process& proc) = delete;
90
94 Process& operator=(Process&& proc) noexcept;
95
99 virtual ~Process() noexcept;
100
101 #ifdef UNIX
106 int Wait() noexcept;
107
112 pid_t Pid() noexcept;
113 #else
118 DWORD Wait() noexcept;
119
124 PROCESS_INFORMATION Pid();
125 #endif
126
130 void Suspend();
131
135 void Resume();
136
143
149 std::string& operator>>(std::string& str) const;
150
156 std::string& Stderr(std::string& str) const;
157
161 friend STORMBYTE_SYSTEM_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Process& proc);
162
168 Process& operator<<(const std::string& str);
169
174 void operator<<(const System::_EoF& eof);
175
180 enum class Status: unsigned short {
181 RUNNING,
182 SUSPENDED,
183 TERMINATED
184 };
185
186 protected:
188 #ifdef UNIX
189 pid_t m_pid;
190 #else
191 STARTUPINFOW m_siStartInfo;
192 PROCESS_INFORMATION m_piProcInfo;
193 #endif
194 std::unique_ptr<Pipe> m_pstdout;
195 std::unique_ptr<Pipe> m_pstdin;
196 std::unique_ptr<Pipe> m_pstderr;
197 std::filesystem::path m_program;
198 std::vector<std::string> m_arguments;
199 std::unique_ptr<std::thread> m_forwarder;
200
201 private:
206 void Send(const std::string& str);
207
211 void Run();
212
217 void ConsumeAndForward(Process& exec);
218
222 void ReleaseOwnership() noexcept;
223
224 #ifdef WINDOWS
229 std::wstring FullCommand() const;
230 #endif
231 };
232
239 STORMBYTE_SYSTEM_PUBLIC std::ostream& operator<<(std::ostream& ostream, const Process& proc);
240}
Runs an external program with piped stdin/stdout/stderr.
Definition process.hxx:60
PROCESS_INFORMATION m_piProcInfo
Process info.
Definition process.hxx:192
Process & operator=(const Process &proc)=delete
Copy assignment (deleted).
void operator<<(const System::_EoF &eof)
Close process stdin (write end).
std::unique_ptr< std::thread > m_forwarder
Forwarder thread.
Definition process.hxx:199
Process & operator=(Process &&proc) noexcept
Move assignment (invalidates the source).
void Suspend()
Suspend the child process.
std::unique_ptr< Pipe > m_pstdout
stdout pipe
Definition process.hxx:194
Process(const std::filesystem::path &prog, const std::vector< std::string > &args=std::vector< std::string >())
Construct and start.
Process(const Process &proc)=delete
Copy constructor (deleted).
Process(Process &&proc) noexcept
Move constructor (invalidates the source).
void Resume()
Resume a suspended child process.
virtual ~Process() noexcept
Destructor (waits if still owning a child, then frees pipes).
DWORD Wait() noexcept
Block until the process exits (no timeout).
Process & operator<<(const std::string &str)
Write str to process stdin.
Process(std::filesystem::path &&prog, std::vector< std::string > &&args=std::vector< std::string >())
Construct and start (moved).
Status
Process lifecycle.
Definition process.hxx:180
std::vector< std::string > m_arguments
Arguments.
Definition process.hxx:198
std::unique_ptr< Pipe > m_pstdin
stdin pipe
Definition process.hxx:195
friend STORMBYTE_SYSTEM_PUBLIC std::ostream & operator<<(std::ostream &ostream, const Process &proc)
Stream process stdout to an ostream.
std::string & operator>>(std::string &str) const
Read remaining stdout into str.
std::string & Stderr(std::string &str) const
Read remaining stderr into str.
Process & operator>>(Process &proc)
Forward this process stdout to proc stdin (background thread).
std::unique_ptr< Pipe > m_pstderr
stderr pipe
Definition process.hxx:196
STARTUPINFOW m_siStartInfo
Startup info.
Definition process.hxx:191
std::filesystem::path m_program
Program path.
Definition process.hxx:197
Status m_status
Current status.
Definition process.hxx:187
System module of the StormByte suite.
Definition exception.hxx:30
STORMBYTE_SYSTEM_PUBLIC std::ostream & operator<<(std::ostream &ostream, const Process &proc)
Stream process stdout to an ostream.
Forward declaration.
Definition process.hxx:45