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 return SDL_WaitProcess(m_process, block, nullptr);
54 }
55
56 void Subprocess::kill() noexcept
57 {
58 if (m_process)
59 {
60 SDL_KillProcess(m_process, false);
61 SDL_DestroyProcess(m_process);
62
63 m_process = nullptr;
64 }
65 }
66} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_ERROR
Definition Log.hpp:24
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.
Timer.hpp galaxy.
Definition Timer.cpp:18