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
32
33// Forward decl
34struct Process;
35
37using ProcessRaw = SDL_Process*;
38
39// Forward decl
40struct ProcessRef;
41
82using ProcessIO = SDL_ProcessIO;
83
85 SDL_PROCESS_STDIO_INHERITED;
87
89 SDL_PROCESS_STDIO_NULL;
90
95constexpr ProcessIO PROCESS_STDIO_APP = SDL_PROCESS_STDIO_APP;
96
98constexpr ProcessIO PROCESS_STDIO_REDIRECT = SDL_PROCESS_STDIO_REDIRECT;
99
110{
111 ProcessRaw m_resource = nullptr;
112
113public:
115 constexpr Process(std::nullptr_t = nullptr) noexcept
116 : m_resource(nullptr)
117 {
118 }
119
127 constexpr explicit Process(ProcessRaw resource) noexcept
128 : m_resource(resource)
129 {
130 }
131
133 constexpr Process(const Process& other) noexcept = delete;
134
136 constexpr Process(Process&& other) noexcept
137 : Process(other.release())
138 {
139 }
140
141 constexpr Process(const ProcessRef& other) = delete;
142
143 constexpr Process(ProcessRef&& other) = delete;
144
184 Process(const char* const* args, bool pipe_stdio);
185
257
259 ~Process() { SDL_DestroyProcess(m_resource); }
260
262 constexpr Process& operator=(Process&& other) noexcept
263 {
264 std::swap(m_resource, other.m_resource);
265 return *this;
266 }
267
269 Process& operator=(const Process& other) = delete;
270
272 constexpr ProcessRaw get() const noexcept { return m_resource; }
273
275 constexpr ProcessRaw release() noexcept
276 {
277 auto r = m_resource;
278 m_resource = nullptr;
279 return r;
280 }
281
283 constexpr auto operator<=>(const Process& other) const noexcept = default;
284
286 constexpr explicit operator bool() const noexcept { return !!m_resource; }
287
302 void Destroy();
303
333
360 StringResult Read(int* exitcode = nullptr);
361
386 template<class T>
387 OwnArray<T> ReadAs(int* exitcode = nullptr)
388 {
389 StringResult data = Read(exitcode);
390 if (data.empty()) return {};
391 size_t sz = data.size() / sizeof(T);
392 return OwnArray{static_cast<T*>(data.release()), sz};
393 }
394
419
442
462 void Kill(bool force);
463
494 bool Wait(bool block, int* exitcode);
495};
496
503{
504 using Process::Process;
505
513 constexpr ProcessRef(ProcessRaw resource) noexcept
514 : Process(resource)
515 {
516 }
517
525 constexpr ProcessRef(const Process& resource) noexcept
526 : Process(resource.get())
527 {
528 }
529
537 constexpr ProcessRef(Process&& resource) noexcept
538 : Process(std::move(resource).release())
539 {
540 }
541
543 constexpr ProcessRef(const ProcessRef& other) noexcept
544 : Process(other.get())
545 {
546 }
547
549 constexpr ProcessRef(ProcessRef&& other) noexcept
550 : Process(other.get())
551 {
552 }
553
556
558 ProcessRef& operator=(const ProcessRef& other) noexcept
559 {
560 release();
561 Process::operator=(Process(other.get()));
562 return *this;
563 }
564
566 constexpr operator ProcessRaw() const noexcept { return get(); }
567};
568
608inline Process CreateProcess(const char* const* args, bool pipe_stdio)
609{
610 return Process(args, pipe_stdio);
611}
612
613inline Process::Process(const char* const* args, bool pipe_stdio)
614 : m_resource(SDL_CreateProcess(args, pipe_stdio))
615{
616}
617
619 : m_resource(SDL_CreateProcessWithProperties(props))
620{
621}
622
694{
695 return Process(props);
696}
697
698namespace prop::process {
699
700constexpr auto CREATE_ARGS_POINTER = SDL_PROP_PROCESS_CREATE_ARGS_POINTER;
701
702constexpr auto CREATE_ENVIRONMENT_POINTER =
703 SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER;
704
705#if SDL_VERSION_ATLEAST(3, 4, 0)
706
707constexpr auto CREATE_WORKING_DIRECTORY_STRING =
708 SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING;
709
710#endif // SDL_VERSION_ATLEAST(3, 4, 0)
711
712constexpr auto CREATE_STDIN_NUMBER = SDL_PROP_PROCESS_CREATE_STDIN_NUMBER;
713
714constexpr auto CREATE_STDIN_POINTER = SDL_PROP_PROCESS_CREATE_STDIN_POINTER;
715
716constexpr auto CREATE_STDOUT_NUMBER = SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER;
717
718constexpr auto CREATE_STDOUT_POINTER = SDL_PROP_PROCESS_CREATE_STDOUT_POINTER;
719
720constexpr auto CREATE_STDERR_NUMBER = SDL_PROP_PROCESS_CREATE_STDERR_NUMBER;
721
722constexpr auto CREATE_STDERR_POINTER = SDL_PROP_PROCESS_CREATE_STDERR_POINTER;
723
724constexpr auto CREATE_STDERR_TO_STDOUT_BOOLEAN =
725 SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN;
726
727constexpr auto CREATE_BACKGROUND_BOOLEAN =
728 SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN;
729
730#if SDL_VERSION_ATLEAST(3, 4, 0)
731
732constexpr auto CREATE_CMDLINE_STRING = SDL_PROP_PROCESS_CREATE_CMDLINE_STRING;
733
734#endif // SDL_VERSION_ATLEAST(3, 4, 0)
735
736constexpr auto PID_NUMBER = SDL_PROP_PROCESS_PID_NUMBER;
737
738constexpr auto STDIN_POINTER = SDL_PROP_PROCESS_STDIN_POINTER;
739
740constexpr auto STDOUT_POINTER = SDL_PROP_PROCESS_STDOUT_POINTER;
741
742constexpr auto STDERR_POINTER = SDL_PROP_PROCESS_STDERR_POINTER;
743
744constexpr auto BACKGROUND_BOOLEAN = SDL_PROP_PROCESS_BACKGROUND_BOOLEAN;
745
746} // namespace prop::process
747
778{
779 return {CheckError(SDL_GetProcessProperties(process))};
780}
781
783{
784 return SDL::GetProcessProperties(m_resource);
785}
786
814inline StringResult ReadProcess(ProcessRef process, int* exitcode = nullptr)
815{
816 size_t size = 0;
817 auto data = static_cast<char*>(SDL_ReadProcess(process, &size, exitcode));
818 return StringResult(CheckError(data), size);
819}
820
821inline StringResult Process::Read(int* exitcode)
822{
823 return SDL::ReadProcess(m_resource, exitcode);
824}
825
851{
852 return {SDL_GetProcessInput(process)};
853}
854
856{
857 return SDL::GetProcessInput(m_resource);
858}
859
883{
884 return {SDL_GetProcessOutput(process)};
885}
886
888{
889 return SDL::GetProcessOutput(m_resource);
890}
891
911inline void KillProcess(ProcessRef process, bool force)
912{
913 CheckError(SDL_KillProcess(process, force));
914}
915
916inline void Process::Kill(bool force) { SDL::KillProcess(m_resource, force); }
917
949inline bool WaitProcess(ProcessRef process, bool block, int* exitcode)
950{
951 return SDL_WaitProcess(process, block, exitcode);
952}
953
954inline bool Process::Wait(bool block, int* exitcode)
955{
956 return SDL::WaitProcess(m_resource, block, exitcode);
957}
958
975inline void DestroyProcess(ProcessRaw process) { SDL_DestroyProcess(process); }
976
978
980
981} // namespace SDL
982
983#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:110
constexpr auto operator<=>(const Process &other) const noexcept=default
Comparison.
constexpr Process(Process &&other) noexcept
Move constructor.
Definition SDL3pp_process.h:136
constexpr ProcessRaw release() noexcept
Retrieves underlying ProcessRaw and clear this.
Definition SDL3pp_process.h:275
Process & operator=(const Process &other)=delete
Assignment operator.
constexpr Process & operator=(Process &&other) noexcept
Assignment operator.
Definition SDL3pp_process.h:262
constexpr ProcessRaw get() const noexcept
Retrieves underlying ProcessRaw.
Definition SDL3pp_process.h:272
constexpr Process(const Process &other) noexcept=delete
Copy constructor.
constexpr Process(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition SDL3pp_process.h:115
OwnArray< T > ReadAs(int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:387
~Process()
Destructor.
Definition SDL3pp_process.h:259
constexpr Process(ProcessRaw resource) noexcept
Constructs from raw Process.
Definition SDL3pp_process.h:127
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
StringResult ReadProcess(ProcessRef process, int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:814
IOStreamRef GetOutput()
Get the IOStream associated with process standard output.
Definition SDL3pp_process.h:887
void Destroy()
Destroy a previously created process object.
Definition SDL3pp_process.h:977
SDL_ProcessIO ProcessIO
Description of where standard I/O should be directed when creating a process.
Definition SDL3pp_process.h:82
IOStreamRef GetProcessOutput(ProcessRef process)
Get the IOStream associated with process standard output.
Definition SDL3pp_process.h:882
PropertiesRef GetProperties() const
Get the properties associated with a process.
Definition SDL3pp_process.h:782
PropertiesRef GetProcessProperties(ProcessRef process)
Get the properties associated with a process.
Definition SDL3pp_process.h:777
Process CreateProcess(const char *const *args, bool pipe_stdio)
Create a new process.
Definition SDL3pp_process.h:608
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:95
constexpr ProcessIO PROCESS_STDIO_NULL
The I/O stream is ignored.
Definition SDL3pp_process.h:88
void KillProcess(ProcessRef process, bool force)
Stop a process.
Definition SDL3pp_process.h:911
bool Wait(bool block, int *exitcode)
Wait for a process to finish.
Definition SDL3pp_process.h:954
Process CreateProcessWithProperties(PropertiesRef props)
Create a new process with the specified properties.
Definition SDL3pp_process.h:693
bool WaitProcess(ProcessRef process, bool block, int *exitcode)
Wait for a process to finish.
Definition SDL3pp_process.h:949
void Kill(bool force)
Stop a process.
Definition SDL3pp_process.h:916
void DestroyProcess(ProcessRaw process)
Destroy a previously created process object.
Definition SDL3pp_process.h:975
SDL_Process * ProcessRaw
Alias to raw representation for Process.
Definition SDL3pp_process.h:37
IOStreamRef GetProcessInput(ProcessRef process)
Get the IOStream associated with process standard input.
Definition SDL3pp_process.h:850
IOStreamRef GetInput()
Get the IOStream associated with process standard input.
Definition SDL3pp_process.h:855
constexpr ProcessIO PROCESS_STDIO_REDIRECT
The I/O stream is redirected to an existing IOStream.
Definition SDL3pp_process.h:98
constexpr ProcessIO PROCESS_STDIO_INHERITED
The I/O stream is inherited from the application.
Definition SDL3pp_process.h:84
StringResult Read(int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:821
Main include header for the SDL3pp library.
Reference for IOStream.
Definition SDL3pp_iostream.h:1627
Reference for Process.
Definition SDL3pp_process.h:503
~ProcessRef()
Destructor.
Definition SDL3pp_process.h:555
constexpr ProcessRef(ProcessRaw resource) noexcept
Constructs from raw Process.
Definition SDL3pp_process.h:513
constexpr ProcessRef(Process &&resource) noexcept
Constructs from Process.
Definition SDL3pp_process.h:537
constexpr ProcessRef(ProcessRef &&other) noexcept
Move constructor.
Definition SDL3pp_process.h:549
constexpr ProcessRef(const ProcessRef &other) noexcept
Copy constructor.
Definition SDL3pp_process.h:543
constexpr Process(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition SDL3pp_process.h:115
ProcessRef & operator=(const ProcessRef &other) noexcept
Assignment operator.
Definition SDL3pp_process.h:558
constexpr ProcessRef(const Process &resource) noexcept
Constructs from Process.
Definition SDL3pp_process.h:525
Reference for Properties.
Definition SDL3pp_properties.h:687
A simple std::string-like interface for SDL allocated strings.
Definition SDL3pp_strings.h:147