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
34// Forward decl
35struct Process;
36
38using ProcessRaw = SDL_Process*;
39
40// Forward decl
41struct ProcessRef;
42
45{
47
50 : value(value)
51 {
52 }
53
55 constexpr ProcessParam(std::nullptr_t _ = nullptr)
56 : value(nullptr)
57 {
58 }
59
61 constexpr explicit operator bool() const { return !!value; }
62
64 constexpr auto operator<=>(const ProcessParam& other) const = default;
65
67 constexpr operator ProcessRaw() const { return value; }
68};
69
112using ProcessIO = SDL_ProcessIO;
113
115 SDL_PROCESS_STDIO_INHERITED;
117
119 SDL_PROCESS_STDIO_NULL;
120
125constexpr ProcessIO PROCESS_STDIO_APP = SDL_PROCESS_STDIO_APP;
126
128constexpr ProcessIO PROCESS_STDIO_REDIRECT = SDL_PROCESS_STDIO_REDIRECT;
129
140{
141 ProcessRaw m_resource = nullptr;
142
143public:
145 constexpr Process() = default;
146
154 constexpr explicit Process(const ProcessRaw resource)
155 : m_resource(resource)
156 {
157 }
158
160 constexpr Process(const Process& other) = delete;
161
163 constexpr Process(Process&& other)
164 : Process(other.release())
165 {
166 }
167
168 constexpr Process(const ProcessRef& other) = delete;
169
170 constexpr Process(ProcessRef&& other) = delete;
171
212 Process(const char* const* args, bool pipe_stdio)
213 : m_resource(SDL_CreateProcess(args, pipe_stdio))
214 {
215 }
216
279 : m_resource(SDL_CreateProcessWithProperties(props))
280 {
281 }
282
284 ~Process() { SDL_DestroyProcess(m_resource); }
285
288 {
289 std::swap(m_resource, other.m_resource);
290 return *this;
291 }
292
294 constexpr ProcessRaw get() const { return m_resource; }
295
297 constexpr ProcessRaw release()
298 {
299 auto r = m_resource;
300 m_resource = nullptr;
301 return r;
302 }
303
305 constexpr auto operator<=>(const Process& other) const = default;
306
308 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
309
311 constexpr explicit operator bool() const { return !!m_resource; }
312
314 constexpr operator ProcessParam() const { return {m_resource}; }
315
332 void Destroy();
333
363
390 StringResult Read(int* exitcode = nullptr);
391
416 template<class T>
417 OwnArray<T> ReadAs(int* exitcode = nullptr)
418 {
419 OwnArray<std::byte> data = Read(exitcode);
420 if (data.empty()) return {};
421 size_t sz = data.size() / sizeof(T);
422 return OwnArray{static_cast<T*>(data.release()), sz};
423 }
424
449
472
492 void Kill(bool force);
493
524 bool Wait(bool block, int* exitcode);
525};
526
529{
538 : Process(resource.value)
539 {
540 }
541
543 ProcessRef(const ProcessRef& other)
544 : Process(other.get())
545 {
546 }
547
550};
551
592inline Process CreateProcess(const char* const* args, bool pipe_stdio)
593{
594 return Process(args, pipe_stdio);
595}
596
659{
660 return Process(props);
661}
662
663namespace prop::process {
664
665constexpr auto CREATE_ARGS_POINTER = SDL_PROP_PROCESS_CREATE_ARGS_POINTER;
666
667constexpr auto CREATE_ENVIRONMENT_POINTER =
668 SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER;
669
670constexpr auto CREATE_STDIN_NUMBER = SDL_PROP_PROCESS_CREATE_STDIN_NUMBER;
671
672constexpr auto CREATE_STDIN_POINTER = SDL_PROP_PROCESS_CREATE_STDIN_POINTER;
673
674constexpr auto CREATE_STDOUT_NUMBER = SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER;
675
676constexpr auto CREATE_STDOUT_POINTER = SDL_PROP_PROCESS_CREATE_STDOUT_POINTER;
677
678constexpr auto CREATE_STDERR_NUMBER = SDL_PROP_PROCESS_CREATE_STDERR_NUMBER;
679
680constexpr auto CREATE_STDERR_POINTER = SDL_PROP_PROCESS_CREATE_STDERR_POINTER;
681
682constexpr auto CREATE_STDERR_TO_STDOUT_BOOLEAN =
683 SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN;
684
685constexpr auto CREATE_BACKGROUND_BOOLEAN =
686 SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN;
687
688constexpr auto PID_NUMBER = SDL_PROP_PROCESS_PID_NUMBER;
689
690constexpr auto STDIN_POINTER = SDL_PROP_PROCESS_STDIN_POINTER;
691
692constexpr auto STDOUT_POINTER = SDL_PROP_PROCESS_STDOUT_POINTER;
693
694constexpr auto STDERR_POINTER = SDL_PROP_PROCESS_STDERR_POINTER;
695
696constexpr auto BACKGROUND_BOOLEAN = SDL_PROP_PROCESS_BACKGROUND_BOOLEAN;
697
698} // namespace prop::process
699
730{
731 return {CheckError(SDL_GetProcessProperties(process))};
732}
733
735{
736 return SDL::GetProcessProperties(m_resource);
737}
738
766inline StringResult ReadProcess(ProcessParam process, int* exitcode = nullptr)
767{
768 size_t size = 0;
769 auto data = static_cast<char*>(SDL_ReadProcess(process, &size, exitcode));
770 return StringResult(CheckError(data), size);
771}
772
773inline StringResult Process::Read(int* exitcode)
774{
775 return SDL::ReadProcess(m_resource, exitcode);
776}
777
803{
804 return {SDL_GetProcessInput(process)};
805}
806
808{
809 return SDL::GetProcessInput(m_resource);
810}
811
835{
836 return {SDL_GetProcessOutput(process)};
837}
838
840{
841 return SDL::GetProcessOutput(m_resource);
842}
843
864inline void KillProcess(ProcessParam process, bool force)
865{
866 CheckError(SDL_KillProcess(process, force));
867}
868
869inline void Process::Kill(bool force) { SDL::KillProcess(m_resource, force); }
870
902inline bool WaitProcess(ProcessParam process, bool block, int* exitcode)
903{
904 return SDL_WaitProcess(process, block, exitcode);
905}
906
907inline bool Process::Wait(bool block, int* exitcode)
908{
909 return SDL::WaitProcess(m_resource, block, exitcode);
910}
911
929inline void DestroyProcess(ProcessRaw process) { SDL_DestroyProcess(process); }
930
932
934
935} // namespace SDL
936
937#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:140
constexpr Process(Process &&other)
Move constructor.
Definition: SDL3pp_process.h:163
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_process.h:308
Process(const char *const *args, bool pipe_stdio)
Create a new process.
Definition: SDL3pp_process.h:212
constexpr ProcessRaw release()
Retrieves underlying ProcessRaw and clear this.
Definition: SDL3pp_process.h:297
constexpr ProcessRaw get() const
Retrieves underlying ProcessRaw.
Definition: SDL3pp_process.h:294
Process(PropertiesParam props)
Create a new process with the specified properties.
Definition: SDL3pp_process.h:278
constexpr auto operator<=>(const Process &other) const =default
Comparison.
OwnArray< T > ReadAs(int *exitcode=nullptr)
Read all the output from a process.
Definition: SDL3pp_process.h:417
constexpr Process(const Process &other)=delete
Copy constructor.
~Process()
Destructor.
Definition: SDL3pp_process.h:284
constexpr Process(const ProcessRaw resource)
Constructs from ProcessParam.
Definition: SDL3pp_process.h:154
constexpr Process()=default
Default ctor.
Process & operator=(Process other)
Assignment operator.
Definition: SDL3pp_process.h:287
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
IOStreamRef GetOutput()
Get the IOStream associated with process standard output.
Definition: SDL3pp_process.h:839
void KillProcess(ProcessParam process, bool force)
Stop a process.
Definition: SDL3pp_process.h:864
void Destroy()
Destroy a previously created process object.
Definition: SDL3pp_process.h:931
SDL_ProcessIO ProcessIO
Description of where standard I/O should be directed when creating a process.
Definition: SDL3pp_process.h:112
IOStreamRef GetProcessOutput(ProcessParam process)
Get the IOStream associated with process standard output.
Definition: SDL3pp_process.h:834
StringResult ReadProcess(ProcessParam process, int *exitcode=nullptr)
Read all the output from a process.
Definition: SDL3pp_process.h:766
PropertiesRef GetProperties() const
Get the properties associated with a process.
Definition: SDL3pp_process.h:734
IOStreamRef GetProcessInput(ProcessParam process)
Get the IOStream associated with process standard input.
Definition: SDL3pp_process.h:802
Process CreateProcess(const char *const *args, bool pipe_stdio)
Create a new process.
Definition: SDL3pp_process.h:592
Process CreateProcessWithProperties(PropertiesParam props)
Create a new process with the specified properties.
Definition: SDL3pp_process.h:658
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:125
constexpr ProcessIO PROCESS_STDIO_NULL
The I/O stream is ignored.
Definition: SDL3pp_process.h:118
bool Wait(bool block, int *exitcode)
Wait for a process to finish.
Definition: SDL3pp_process.h:907
void Kill(bool force)
Stop a process.
Definition: SDL3pp_process.h:869
bool WaitProcess(ProcessParam process, bool block, int *exitcode)
Wait for a process to finish.
Definition: SDL3pp_process.h:902
PropertiesRef GetProcessProperties(ProcessParam process)
Get the properties associated with a process.
Definition: SDL3pp_process.h:729
void DestroyProcess(ProcessRaw process)
Destroy a previously created process object.
Definition: SDL3pp_process.h:929
IOStreamRef GetInput()
Get the IOStream associated with process standard input.
Definition: SDL3pp_process.h:807
SDL_Process * ProcessRaw
Alias to raw representation for Process.
Definition: SDL3pp_process.h:38
constexpr ProcessIO PROCESS_STDIO_REDIRECT
The I/O stream is redirected to an existing IOStream.
Definition: SDL3pp_process.h:128
constexpr ProcessIO PROCESS_STDIO_INHERITED
The I/O stream is inherited from the application.
Definition: SDL3pp_process.h:114
StringResult Read(int *exitcode=nullptr)
Read all the output from a process.
Definition: SDL3pp_process.h:773
Main include header for the SDL3pp library.
Semi-safe reference for IOStream.
Definition: SDL3pp_iostream.h:1629
Safely wrap Process for non owning parameters.
Definition: SDL3pp_process.h:45
constexpr ProcessParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_process.h:55
constexpr ProcessParam(ProcessRaw value)
Constructs from ProcessRaw.
Definition: SDL3pp_process.h:49
constexpr auto operator<=>(const ProcessParam &other) const =default
Comparison.
ProcessRaw value
parameter's ProcessRaw
Definition: SDL3pp_process.h:46
Semi-safe reference for Process.
Definition: SDL3pp_process.h:529
~ProcessRef()
Destructor.
Definition: SDL3pp_process.h:549
ProcessRef(const ProcessRef &other)
Copy constructor.
Definition: SDL3pp_process.h:543
ProcessRef(ProcessParam resource)
Constructs from ProcessParam.
Definition: SDL3pp_process.h:537
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:52
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:701
A simple std::string-like interface for SDL allocated strings.
Definition: SDL3pp_strings.h:153