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 kill();
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 m_process = SDL_CreateProcess(cmd_line.data(), false);
45 if (!m_process)
46 {
47 GALAXY_LOG(GALAXY_ERROR, "Failed to launch subprocess: {0}.", process);
48 }
49 }
50
51 bool Subprocess::wait(const bool block) noexcept
52 {
53 if (m_process)
54 {
55 return SDL_WaitProcess(m_process, block, nullptr);
56 }
57
58 return false;
59 }
60
61 void Subprocess::kill() noexcept
62 {
63 if (m_process)
64 {
65 SDL_KillProcess(m_process, false);
66 SDL_DestroyProcess(m_process);
67
68 m_process = nullptr;
69 }
70 }
71
72 SDL_PropertiesID Subprocess::get_properties() const noexcept
73 {
74 if (m_process)
75 {
76 return SDL_GetProcessProperties(m_process);
77 }
78
79 return 0;
80 }
81} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
SDL_PropertiesID get_properties() const noexcept
Get the properties associated with a process.
SDL_Process * m_process
Process information and handles.
~Subprocess() noexcept
Destructor.
Subprocess() noexcept
Constructor.
void kill() noexcept
Terminates process.
void create(std::string_view process, std::span< std::string > args={})
Launch a subprocess.
bool wait(const bool block) noexcept
Wait for a process to finish execution.
Application.hpp galaxy.