SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_process.h
1#ifndef SDL3PP_PROCESS_H_
2#define SDL3PP_PROCESS_H_
3
4#include <SDL3/SDL_process.h>
5#include "SDL3pp_iostream.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
8
9namespace SDL {
10
33// Forward decl
34struct Process;
35
37using ProcessRaw = SDL_Process*;
38
39// Forward decl
40struct ProcessRef;
41
44{
46
49 : value(value)
50 {
51 }
52
54 constexpr ProcessParam(std::nullptr_t = nullptr)
55 : value(nullptr)
56 {
57 }
58
60 constexpr explicit operator bool() const { return !!value; }
61
63 constexpr auto operator<=>(const ProcessParam& other) const = default;
64
66 constexpr operator ProcessRaw() const { return value; }
67};
68
109using ProcessIO = SDL_ProcessIO;
110
112 SDL_PROCESS_STDIO_INHERITED;
114
116 SDL_PROCESS_STDIO_NULL;
117
122constexpr ProcessIO PROCESS_STDIO_APP = SDL_PROCESS_STDIO_APP;
123
125constexpr ProcessIO PROCESS_STDIO_REDIRECT = SDL_PROCESS_STDIO_REDIRECT;
126
137{
138 ProcessRaw m_resource = nullptr;
139
140public:
142 constexpr Process(std::nullptr_t = nullptr) noexcept
143 : m_resource(0)
144 {
145 }
146
154 constexpr explicit Process(const ProcessRaw resource) noexcept
155 : m_resource(resource)
156 {
157 }
158
159protected:
161 constexpr Process(const Process& other) noexcept = default;
162
163public:
165 constexpr Process(Process&& other) noexcept
166 : Process(other.release())
167 {
168 }
169
170 constexpr Process(const ProcessRef& other) = delete;
171
172 constexpr Process(ProcessRef&& other) = delete;
173
213 Process(const char* const* args, bool pipe_stdio)
214 : m_resource(SDL_CreateProcess(args, pipe_stdio))
215 {
216 }
217
289 : m_resource(SDL_CreateProcessWithProperties(props))
290 {
291 }
292
294 ~Process() { SDL_DestroyProcess(m_resource); }
295
297 constexpr Process& operator=(Process&& other) noexcept
298 {
299 std::swap(m_resource, other.m_resource);
300 return *this;
301 }
302
303protected:
305 constexpr Process& operator=(const Process& other) noexcept = default;
306
307public:
309 constexpr ProcessRaw get() const noexcept { return m_resource; }
310
312 constexpr ProcessRaw release() noexcept
313 {
314 auto r = m_resource;
315 m_resource = nullptr;
316 return r;
317 }
318
320 constexpr auto operator<=>(const Process& other) const noexcept = default;
321
323 constexpr explicit operator bool() const noexcept { return !!m_resource; }
324
326 constexpr operator ProcessParam() const noexcept { return {m_resource}; }
327
342 void Destroy();
343
373
400 StringResult Read(int* exitcode = nullptr);
401
426 template<class T>
427 OwnArray<T> ReadAs(int* exitcode = nullptr)
428 {
429 StringResult data = Read(exitcode);
430 if (data.empty()) return {};
431 size_t sz = data.size() / sizeof(T);
432 return OwnArray{static_cast<T*>(data.release()), sz};
433 }
434
459
482
502 void Kill(bool force);
503
534 bool Wait(bool block, int* exitcode);
535};
536
539{
540 using Process::Process;
541
549 ProcessRef(ProcessParam resource) noexcept
550 : Process(resource.value)
551 {
552 }
553
561 ProcessRef(ProcessRaw resource) noexcept
562 : Process(resource)
563 {
564 }
565
567 constexpr ProcessRef(const ProcessRef& other) noexcept = default;
568
571};
572
612inline Process CreateProcess(const char* const* args, bool pipe_stdio)
613{
614 return Process(args, pipe_stdio);
615}
616
688{
689 return Process(props);
690}
691
692namespace prop::process {
693
694constexpr auto CREATE_ARGS_POINTER = SDL_PROP_PROCESS_CREATE_ARGS_POINTER;
695
696constexpr auto CREATE_ENVIRONMENT_POINTER =
697 SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER;
698
699#if SDL_VERSION_ATLEAST(3, 4, 0)
700
701constexpr auto CREATE_WORKING_DIRECTORY_STRING =
702 SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING;
703
704#endif // SDL_VERSION_ATLEAST(3, 4, 0)
705
706constexpr auto CREATE_STDIN_NUMBER = SDL_PROP_PROCESS_CREATE_STDIN_NUMBER;
707
708constexpr auto CREATE_STDIN_POINTER = SDL_PROP_PROCESS_CREATE_STDIN_POINTER;
709
710constexpr auto CREATE_STDOUT_NUMBER = SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER;
711
712constexpr auto CREATE_STDOUT_POINTER = SDL_PROP_PROCESS_CREATE_STDOUT_POINTER;
713
714constexpr auto CREATE_STDERR_NUMBER = SDL_PROP_PROCESS_CREATE_STDERR_NUMBER;
715
716constexpr auto CREATE_STDERR_POINTER = SDL_PROP_PROCESS_CREATE_STDERR_POINTER;
717
718constexpr auto CREATE_STDERR_TO_STDOUT_BOOLEAN =
719 SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN;
720
721constexpr auto CREATE_BACKGROUND_BOOLEAN =
722 SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN;
723
724#if SDL_VERSION_ATLEAST(3, 4, 0)
725
726constexpr auto CREATE_CMDLINE_STRING = SDL_PROP_PROCESS_CREATE_CMDLINE_STRING;
727
728#endif // SDL_VERSION_ATLEAST(3, 4, 0)
729
730constexpr auto PID_NUMBER = SDL_PROP_PROCESS_PID_NUMBER;
731
732constexpr auto STDIN_POINTER = SDL_PROP_PROCESS_STDIN_POINTER;
733
734constexpr auto STDOUT_POINTER = SDL_PROP_PROCESS_STDOUT_POINTER;
735
736constexpr auto STDERR_POINTER = SDL_PROP_PROCESS_STDERR_POINTER;
737
738constexpr auto BACKGROUND_BOOLEAN = SDL_PROP_PROCESS_BACKGROUND_BOOLEAN;
739
740} // namespace prop::process
741
772{
773 return {CheckError(SDL_GetProcessProperties(process))};
774}
775
777{
778 return SDL::GetProcessProperties(m_resource);
779}
780
808inline StringResult ReadProcess(ProcessParam process, int* exitcode = nullptr)
809{
810 size_t size = 0;
811 auto data = static_cast<char*>(SDL_ReadProcess(process, &size, exitcode));
812 return StringResult(CheckError(data), size);
813}
814
815inline StringResult Process::Read(int* exitcode)
816{
817 return SDL::ReadProcess(m_resource, exitcode);
818}
819
845{
846 return {SDL_GetProcessInput(process)};
847}
848
850{
851 return SDL::GetProcessInput(m_resource);
852}
853
877{
878 return {SDL_GetProcessOutput(process)};
879}
880
882{
883 return SDL::GetProcessOutput(m_resource);
884}
885
905inline void KillProcess(ProcessParam process, bool force)
906{
907 CheckError(SDL_KillProcess(process, force));
908}
909
910inline void Process::Kill(bool force) { SDL::KillProcess(m_resource, force); }
911
943inline bool WaitProcess(ProcessParam process, bool block, int* exitcode)
944{
945 return SDL_WaitProcess(process, block, exitcode);
946}
947
948inline bool Process::Wait(bool block, int* exitcode)
949{
950 return SDL::WaitProcess(m_resource, block, exitcode);
951}
952
969inline void DestroyProcess(ProcessRaw process) { SDL_DestroyProcess(process); }
970
972
974
975} // namespace SDL
976
977#endif /* SDL3PP_PROCESS_H_ */
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
constexpr bool empty() const
True if size() == 0.
Definition: SDL3pp_ownPtr.h:74
T * release()
Release control on object.
Definition: SDL3pp_ownPtr.h:91
constexpr size_t size() const
Size.
Definition: SDL3pp_ownPtr.h:83
An opaque handle representing a system process.
Definition: SDL3pp_process.h:137
constexpr auto operator<=>(const Process &other) const noexcept=default
Comparison.
constexpr Process(Process &&other) noexcept
Move constructor.
Definition: SDL3pp_process.h:165
Process(const char *const *args, bool pipe_stdio)
Create a new process.
Definition: SDL3pp_process.h:213
constexpr ProcessRaw release() noexcept
Retrieves underlying ProcessRaw and clear this.
Definition: SDL3pp_process.h:312
constexpr Process & operator=(const Process &other) noexcept=default
Assignment operator.
constexpr Process(const ProcessRaw resource) noexcept
Constructs from ProcessParam.
Definition: SDL3pp_process.h:154
constexpr Process & operator=(Process &&other) noexcept
Assignment operator.
Definition: SDL3pp_process.h:297
constexpr ProcessRaw get() const noexcept
Retrieves underlying ProcessRaw.
Definition: SDL3pp_process.h:309
constexpr Process(const Process &other) noexcept=default
Copy constructor.
constexpr Process(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_process.h:142
Process(PropertiesParam props)
Create a new process with the specified properties.
Definition: SDL3pp_process.h:288
OwnArray< T > ReadAs(int *exitcode=nullptr)
Read all the output from a process.
Definition: SDL3pp_process.h:427
~Process()
Destructor.
Definition: SDL3pp_process.h:294
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
IOStreamRef GetOutput()
Get the IOStream associated with process standard output.
Definition: SDL3pp_process.h:881
void KillProcess(ProcessParam process, bool force)
Stop a process.
Definition: SDL3pp_process.h:905
void Destroy()
Destroy a previously created process object.
Definition: SDL3pp_process.h:971
SDL_ProcessIO ProcessIO
Description of where standard I/O should be directed when creating a process.
Definition: SDL3pp_process.h:109
IOStreamRef GetProcessOutput(ProcessParam process)
Get the IOStream associated with process standard output.
Definition: SDL3pp_process.h:876
StringResult ReadProcess(ProcessParam process, int *exitcode=nullptr)
Read all the output from a process.
Definition: SDL3pp_process.h:808
PropertiesRef GetProperties() const
Get the properties associated with a process.
Definition: SDL3pp_process.h:776
IOStreamRef GetProcessInput(ProcessParam process)
Get the IOStream associated with process standard input.
Definition: SDL3pp_process.h:844
Process CreateProcess(const char *const *args, bool pipe_stdio)
Create a new process.
Definition: SDL3pp_process.h:612
Process CreateProcessWithProperties(PropertiesParam props)
Create a new process with the specified properties.
Definition: SDL3pp_process.h:687
constexpr ProcessIO PROCESS_STDIO_APP
The I/O stream is connected to a new IOStream that the application can read or write.
Definition: SDL3pp_process.h:122
constexpr ProcessIO PROCESS_STDIO_NULL
The I/O stream is ignored.
Definition: SDL3pp_process.h:115
bool Wait(bool block, int *exitcode)
Wait for a process to finish.
Definition: SDL3pp_process.h:948
void Kill(bool force)
Stop a process.
Definition: SDL3pp_process.h:910
bool WaitProcess(ProcessParam process, bool block, int *exitcode)
Wait for a process to finish.
Definition: SDL3pp_process.h:943
PropertiesRef GetProcessProperties(ProcessParam process)
Get the properties associated with a process.
Definition: SDL3pp_process.h:771
void DestroyProcess(ProcessRaw process)
Destroy a previously created process object.
Definition: SDL3pp_process.h:969
IOStreamRef GetInput()
Get the IOStream associated with process standard input.
Definition: SDL3pp_process.h:849
SDL_Process * ProcessRaw
Alias to raw representation for Process.
Definition: SDL3pp_process.h:37
constexpr ProcessIO PROCESS_STDIO_REDIRECT
The I/O stream is redirected to an existing IOStream.
Definition: SDL3pp_process.h:125
constexpr ProcessIO PROCESS_STDIO_INHERITED
The I/O stream is inherited from the application.
Definition: SDL3pp_process.h:111
StringResult Read(int *exitcode=nullptr)
Read all the output from a process.
Definition: SDL3pp_process.h:815
Main include header for the SDL3pp library.
Semi-safe reference for IOStream.
Definition: SDL3pp_iostream.h:1657
Safely wrap Process for non owning parameters.
Definition: SDL3pp_process.h:44
constexpr ProcessParam(ProcessRaw value)
Constructs from ProcessRaw.
Definition: SDL3pp_process.h:48
constexpr auto operator<=>(const ProcessParam &other) const =default
Comparison.
constexpr ProcessParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_process.h:54
ProcessRaw value
parameter's ProcessRaw
Definition: SDL3pp_process.h:45
Semi-safe reference for Process.
Definition: SDL3pp_process.h:539
~ProcessRef()
Destructor.
Definition: SDL3pp_process.h:570
constexpr ProcessRef(const ProcessRef &other) noexcept=default
Copy constructor.
ProcessRef(ProcessRaw resource) noexcept
Constructs from ProcessParam.
Definition: SDL3pp_process.h:561
ProcessRef(ProcessParam resource) noexcept
Constructs from ProcessParam.
Definition: SDL3pp_process.h:549
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:53
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:716
A simple std::string-like interface for SDL allocated strings.
Definition: SDL3pp_strings.h:153