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
35// Forward decl
36struct ProcessBase;
37
38// Forward decl
39struct ProcessRef;
40
41// Forward decl
42struct Process;
43
86using ProcessIO = SDL_ProcessIO;
87
89 SDL_PROCESS_STDIO_INHERITED;
91
93 SDL_PROCESS_STDIO_NULL;
94
99constexpr ProcessIO PROCESS_STDIO_APP = SDL_PROCESS_STDIO_APP;
100
104constexpr ProcessIO PROCESS_STDIO_REDIRECT = SDL_PROCESS_STDIO_REDIRECT;
105
118struct ProcessBase : Resource<SDL_Process*>
119{
120 using Resource::Resource;
121
162 ProcessBase(const char* const* args, bool pipe_stdio)
163 : Resource(CheckError(SDL_CreateProcess(args, pipe_stdio)))
164 {
165 }
166
228 : Resource(CheckError(SDL_CreateProcessWithProperties(props.get())))
229 {
230 }
231
260 {
261 return CheckError(SDL_GetProcessProperties(get()));
262 }
263
288 StringResult Read(int* exitcode = nullptr)
289 {
290 size_t size = 0;
291 auto data = static_cast<char*>(SDL_ReadProcess(get(), &size, exitcode));
292 return StringResult(CheckError(data), size);
293 }
294
319 template<class T>
320 OwnArray<T> ReadAs(int* exitcode = nullptr)
321 {
322 OwnArray<std::byte> data = Read(exitcode);
323 if (data.empty()) return {};
324 size_t sz = data.size() / sizeof(T);
325 return OwnArray{static_cast<T*>(data.release()), sz};
326 }
327
351 IOStreamRef GetInput() { return SDL_GetProcessInput(get()); }
352
374 IOStreamRef GetOutput() { return SDL_GetProcessOutput(get()); }
375
395 void Kill(bool force) { CheckError(SDL_KillProcess(get(), force)); }
396
427 bool Wait(bool block, int* exitcode)
428 {
429 return SDL_WaitProcess(get(), block, exitcode);
430 }
431};
432
442{
444
448 constexpr ProcessRef(const ProcessRef& other)
449 : ProcessBase(other.get())
450 {
451 }
452
456 constexpr ProcessRef(ProcessRef&& other)
457 : ProcessBase(other.release())
458 {
459 }
460
464 constexpr ~ProcessRef() = default;
465
470 {
471 release(other.release());
472 return *this;
473 }
474
490 void reset(SDL_Process* newResource = {})
491 {
492 SDL_DestroyProcess(release(newResource));
493 }
494};
495
505{
507
511 constexpr explicit Process(SDL_Process* resource = {})
512 : ProcessRef(resource)
513 {
514 }
515
516 constexpr Process(const Process& other) = delete;
517
521 constexpr Process(Process&& other) = default;
522
527
532 {
533 reset(other.release());
534 return *this;
535 }
536};
537
538namespace prop::process {
539
540constexpr auto CREATE_ARGS_POINTER = SDL_PROP_PROCESS_CREATE_ARGS_POINTER;
541
542constexpr auto CREATE_ENVIRONMENT_POINTER =
543 SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER;
544
545constexpr auto CREATE_STDIN_NUMBER = SDL_PROP_PROCESS_CREATE_STDIN_NUMBER;
546
547constexpr auto CREATE_STDIN_POINTER = SDL_PROP_PROCESS_CREATE_STDIN_POINTER;
548
549constexpr auto CREATE_STDOUT_NUMBER = SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER;
550
551constexpr auto CREATE_STDOUT_POINTER = SDL_PROP_PROCESS_CREATE_STDOUT_POINTER;
552
553constexpr auto CREATE_STDERR_NUMBER = SDL_PROP_PROCESS_CREATE_STDERR_NUMBER;
554
555constexpr auto CREATE_STDERR_POINTER = SDL_PROP_PROCESS_CREATE_STDERR_POINTER;
556
557constexpr auto CREATE_STDERR_TO_STDOUT_BOOLEAN =
558 SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN;
559
560constexpr auto CREATE_BACKGROUND_BOOLEAN =
561 SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN;
562
563constexpr auto PID_NUMBER = SDL_PROP_PROCESS_PID_NUMBER;
564
565constexpr auto STDIN_POINTER = SDL_PROP_PROCESS_STDIN_POINTER;
566
567constexpr auto STDOUT_POINTER = SDL_PROP_PROCESS_STDOUT_POINTER;
568
569constexpr auto STDERR_POINTER = SDL_PROP_PROCESS_STDERR_POINTER;
570
571constexpr auto BACKGROUND_BOOLEAN = SDL_PROP_PROCESS_BACKGROUND_BOOLEAN;
572
573} // namespace prop::process
574
576} // namespace SDL
577
578#endif /* SDL3PP_PROCESS_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
constexpr bool empty() const
True if size() == 0.
Definition SDL3pp_ownPtr.h:70
T * release()
Release control on object.
Definition SDL3pp_ownPtr.h:87
constexpr size_t size() const
Size.
Definition SDL3pp_ownPtr.h:79
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_Process * release(SDL_Process * newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr SDL_Process * get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
SDL_ProcessIO ProcessIO
Description of where standard I/O should be directed when creating a process.
Definition SDL3pp_process.h:86
constexpr ProcessIO PROCESS_STDIO_APP
The I/O stream is connected to a new IOStreamBase that the application can read or write.
Definition SDL3pp_process.h:99
constexpr ProcessIO PROCESS_STDIO_NULL
The I/O stream is ignored.
Definition SDL3pp_process.h:92
constexpr ProcessIO PROCESS_STDIO_REDIRECT
The I/O stream is redirected to an existing IOStreamBase.
Definition SDL3pp_process.h:104
constexpr ProcessIO PROCESS_STDIO_INHERITED
The I/O stream is inherited from the application.
Definition SDL3pp_process.h:88
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Handle to a non owned iOStream.
Definition SDL3pp_iostream.h:1505
An opaque handle representing a system process.
Definition SDL3pp_process.h:119
IOStreamRef GetInput()
Get the IOStreamBase associated with process standard input.
Definition SDL3pp_process.h:351
OwnArray< T > ReadAs(int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:320
ProcessBase(const char *const *args, bool pipe_stdio)
Create a new process.
Definition SDL3pp_process.h:162
StringResult Read(int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:288
ProcessBase(PropertiesBase &props)
Create a new process with the specified properties.
Definition SDL3pp_process.h:227
void Kill(bool force)
Stop a process.
Definition SDL3pp_process.h:395
PropertiesRef GetProperties() const
Get the properties associated with a process.
Definition SDL3pp_process.h:259
bool Wait(bool block, int *exitcode)
Wait for a process to finish.
Definition SDL3pp_process.h:427
IOStreamRef GetOutput()
Get the IOStreamBase associated with process standard output.
Definition SDL3pp_process.h:374
Handle to a non owned process.
Definition SDL3pp_process.h:442
void reset(SDL_Process *newResource={})
Destroy a previously created process object.
Definition SDL3pp_process.h:490
ProcessRef & operator=(ProcessRef other)
Assignment operator.
Definition SDL3pp_process.h:469
constexpr ProcessRef(const ProcessRef &other)
Copy constructor.
Definition SDL3pp_process.h:448
constexpr ProcessRef(ProcessRef &&other)
Move constructor.
Definition SDL3pp_process.h:456
constexpr ~ProcessRef()=default
Default constructor.
Handle to an owned process.
Definition SDL3pp_process.h:505
constexpr Process(SDL_Process *resource={})
Constructs from the underlying resource.
Definition SDL3pp_process.h:511
constexpr Process(Process &&other)=default
Move constructor.
~Process()
Frees up resource when object goes out of scope.
Definition SDL3pp_process.h:526
Process & operator=(Process other)
Assignment operator.
Definition SDL3pp_process.h:531
Wrap properties id.
Definition SDL3pp_properties.h:203
Handle to a non owned properties.
Definition SDL3pp_properties.h:693
A simple std::string-like interface for SDL allocated strings.
Definition SDL3pp_strings.h:153