galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Subprocess.cpp
Go to the documentation of this file.
1
7
9
10#include "Subprocess.hpp"
11
12namespace galaxy
13{
15 : m_process {nullptr}
16 {
17 }
18
19 Subprocess::Subprocess(std::string_view process, std::span<std::string> args)
20 : m_process {nullptr}
21 {
22 create(process, args);
23 }
24
26 {
27 terminate();
28 }
29
30 void Subprocess::create(std::string_view process, std::span<std::string> args)
31 {
32 const auto sp_to_run = std::filesystem::absolute(process).replace_extension("").string();
33
34 std::vector<const char*> cmd_line;
35 cmd_line.reserve(args.size() + 2);
36
37 cmd_line.push_back(sp_to_run.c_str());
38 for (const auto& arg : args)
39 {
40 cmd_line.push_back(arg.c_str());
41 }
42 cmd_line.push_back(nullptr);
43
44 if (subprocess_create(cmd_line.data(), subprocess_option_inherit_environment, &m_process) != 0)
45 {
46 GALAXY_LOG(GALAXY_ERROR, "Failed to launch subprocess: {0}.", process);
47 }
48 }
49
50 int Subprocess::join() noexcept
51 {
52 if (alive())
53 {
54 int code = -1;
55 if (subprocess_join(&m_process, &code) != 0)
56 {
57 GALAXY_LOG(GALAXY_ERROR, "Failed to join subprocess.");
58 }
59
60 return code;
61 }
62
63 return -1;
64 }
65
66 void Subprocess::terminate() noexcept
67 {
68 if (alive())
69 {
70 if (subprocess_terminate(&m_process) != 0)
71 {
72 GALAXY_LOG(GALAXY_ERROR, "Failed to terminate subprocess.");
73 }
74 }
75 }
76
77 void Subprocess::destroy() noexcept
78 {
79 if (alive())
80 {
81 if (subprocess_destroy(&m_process) != 0)
82 {
83 GALAXY_LOG(GALAXY_ERROR, "Failed to destroy subprocess.");
84 }
85 }
86 }
87
88 bool Subprocess::alive() noexcept
89 {
90 return subprocess_alive(&m_process) != 0 ? true : false;
91 }
92} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
void terminate() noexcept
Terminate process, killing if alive.
~Subprocess() noexcept
Destructor.
Subprocess() noexcept
Constructor.
int join() noexcept
Wait for a process to finish execution.
subprocess m_process
Process information and handles.
bool alive() noexcept
Check if subprocess is still alive and executing.
void create(std::string_view process, std::span< std::string > args={})
Launch a subprocess.
void destroy() noexcept
Destroy process, preserving if alive.
Application.hpp galaxy.