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