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 ProcessRef;
36
37// Forward decl
38struct Process;
39
49
59
101using ProcessIO = SDL_ProcessIO;
102
104 SDL_PROCESS_STDIO_INHERITED;
106
108 SDL_PROCESS_STDIO_NULL;
109
114constexpr ProcessIO PROCESS_STDIO_APP = SDL_PROCESS_STDIO_APP;
115
119constexpr ProcessIO PROCESS_STDIO_REDIRECT = SDL_PROCESS_STDIO_REDIRECT;
120
131struct ProcessRef : Resource<SDL_Process*>
132{
133 using Resource::Resource;
134
164 {
165 return CheckError(SDL_GetProcessProperties(get()));
166 }
167
193 StringResult Read(int* exitcode = nullptr)
194 {
195 size_t size = 0;
196 auto data = static_cast<char*>(SDL_ReadProcess(get(), &size, exitcode));
197 return StringResult(CheckError(data), size);
198 }
199
224 template<class T>
225 OwnArray<T> ReadAs(int* exitcode = nullptr)
226 {
227 OwnArray<std::byte> data = Read(exitcode);
228 if (data.empty()) return {};
229 size_t sz = data.size() / sizeof(T);
230 return OwnArray{static_cast<T*>(data.release()), sz};
231 }
232
256 IOStreamRef GetInput() { return SDL_GetProcessInput(get()); }
257
279 IOStreamRef GetOutput() { return SDL_GetProcessOutput(get()); }
280
300 void Kill(bool force) { CheckError(SDL_KillProcess(get(), force)); }
301
332 bool Wait(bool block, int* exitcode)
333 {
334 return SDL_WaitProcess(get(), block, exitcode);
335 }
336
354 static void reset(SDL_Process* resource) { SDL_DestroyProcess(resource); }
355};
356
364struct Process : ResourceUnique<ProcessRef>
365{
367
408 static Process Create(const char* const* args, bool pipe_stdio)
409 {
410 return Process(SDL_CreateProcess(args, pipe_stdio));
411 }
412
475 {
476 return Process(SDL_CreateProcessWithProperties(props));
477 }
478
495 void Destroy() { reset(); }
500
501};
502
503
505{
506 return ProcessShared(std::move(*this));
507}
508
518struct ProcessUnsafe : ResourceUnsafe<ProcessRef>
519{
521
525 constexpr explicit ProcessUnsafe(Process&& other)
526 : ProcessUnsafe(other.release())
527 {
528 }
529};
530
531namespace prop::process {
532
533constexpr auto CREATE_ARGS_POINTER = SDL_PROP_PROCESS_CREATE_ARGS_POINTER;
534
535constexpr auto CREATE_ENVIRONMENT_POINTER =
536 SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER;
537
538constexpr auto CREATE_STDIN_NUMBER = SDL_PROP_PROCESS_CREATE_STDIN_NUMBER;
539
540constexpr auto CREATE_STDIN_POINTER = SDL_PROP_PROCESS_CREATE_STDIN_POINTER;
541
542constexpr auto CREATE_STDOUT_NUMBER = SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER;
543
544constexpr auto CREATE_STDOUT_POINTER = SDL_PROP_PROCESS_CREATE_STDOUT_POINTER;
545
546constexpr auto CREATE_STDERR_NUMBER = SDL_PROP_PROCESS_CREATE_STDERR_NUMBER;
547
548constexpr auto CREATE_STDERR_POINTER = SDL_PROP_PROCESS_CREATE_STDERR_POINTER;
549
550constexpr auto CREATE_STDERR_TO_STDOUT_BOOLEAN =
551 SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN;
552
553constexpr auto CREATE_BACKGROUND_BOOLEAN =
554 SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN;
555
556constexpr auto PID_NUMBER = SDL_PROP_PROCESS_PID_NUMBER;
557
558constexpr auto STDIN_POINTER = SDL_PROP_PROCESS_STDIN_POINTER;
559
560constexpr auto STDOUT_POINTER = SDL_PROP_PROCESS_STDOUT_POINTER;
561
562constexpr auto STDERR_POINTER = SDL_PROP_PROCESS_STDERR_POINTER;
563
564constexpr auto BACKGROUND_BOOLEAN = SDL_PROP_PROCESS_BACKGROUND_BOOLEAN;
565
566} // namespace prop::process
567
569} // namespace SDL
570
571#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
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr SDL_Process * get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
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:101
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:114
ResourceShared< Process > ProcessShared
Handle to a shared process.
Definition SDL3pp_process.h:48
constexpr ProcessIO PROCESS_STDIO_NULL
The I/O stream is ignored.
Definition SDL3pp_process.h:107
ProcessShared share()
Move this process into a ProcessShared.
Definition SDL3pp_process.h:504
constexpr ProcessIO PROCESS_STDIO_REDIRECT
The I/O stream is redirected to an existing IOStream.
Definition SDL3pp_process.h:119
constexpr ProcessIO PROCESS_STDIO_INHERITED
The I/O stream is inherited from the application.
Definition SDL3pp_process.h:103
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
The read/write operation structure.
Definition SDL3pp_iostream.h:123
An opaque handle representing a system process.
Definition SDL3pp_process.h:132
IOStreamRef GetOutput()
Get the IOStreamRef associated with process standard output.
Definition SDL3pp_process.h:279
StringResult Read(int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:193
IOStreamRef GetInput()
Get the IOStreamRef associated with process standard input.
Definition SDL3pp_process.h:256
static void reset(SDL_Process *resource)
Destroy a previously created process object.
Definition SDL3pp_process.h:354
void Kill(bool force)
Stop a process.
Definition SDL3pp_process.h:300
PropertiesRef GetProperties() const
Get the properties associated with a process.
Definition SDL3pp_process.h:163
OwnArray< T > ReadAs(int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:225
bool Wait(bool block, int *exitcode)
Wait for a process to finish.
Definition SDL3pp_process.h:332
Unsafe Handle to process.
Definition SDL3pp_process.h:519
constexpr ProcessUnsafe(Process &&other)
Constructs ProcessUnsafe from Process.
Definition SDL3pp_process.h:525
Handle to an owned process.
Definition SDL3pp_process.h:365
void Destroy()
Destroy a previously created process object.
Definition SDL3pp_process.h:495
static Process CreateWithProperties(PropertiesRef props)
Create a new process with the specified properties.
Definition SDL3pp_process.h:474
static Process Create(const char *const *args, bool pipe_stdio)
Create a new process.
Definition SDL3pp_process.h:408
SDL properties ID.
Definition SDL3pp_properties.h:209
A simple std::string-like interface for SDL allocated strings.
Definition SDL3pp_strings.h:153