1#ifndef SDL3PP_PROCESS_H_
2#define SDL3PP_PROCESS_H_
4#include <SDL3/SDL_process.h>
5#include "SDL3pp_iostream.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
60 constexpr explicit operator bool()
const {
return !!
value; }
112 SDL_PROCESS_STDIO_INHERITED;
116 SDL_PROCESS_STDIO_NULL;
142 constexpr Process(std::nullptr_t =
nullptr) noexcept
155 : m_resource(resource)
211 Process(
const char*
const* args,
bool pipe_stdio)
212 : m_resource(SDL_CreateProcess(args, pipe_stdio))
287 : m_resource(SDL_CreateProcessWithProperties(props))
297 std::swap(m_resource, other.m_resource);
313 m_resource =
nullptr;
321 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
324 constexpr operator ProcessParam() const noexcept {
return {m_resource}; }
428 if (data.
empty())
return {};
429 size_t sz = data.
size() /
sizeof(T);
500 void Kill(
bool force);
532 bool Wait(
bool block,
int* exitcode);
615 return Process(args, pipe_stdio);
693namespace prop::process {
695constexpr auto CREATE_ARGS_POINTER = SDL_PROP_PROCESS_CREATE_ARGS_POINTER;
697constexpr auto CREATE_ENVIRONMENT_POINTER =
698 SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER;
700#if SDL_VERSION_ATLEAST(3, 4, 0)
702constexpr auto CREATE_WORKING_DIRECTORY_STRING =
703 SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING;
707constexpr auto CREATE_STDIN_NUMBER = SDL_PROP_PROCESS_CREATE_STDIN_NUMBER;
709constexpr auto CREATE_STDIN_POINTER = SDL_PROP_PROCESS_CREATE_STDIN_POINTER;
711constexpr auto CREATE_STDOUT_NUMBER = SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER;
713constexpr auto CREATE_STDOUT_POINTER = SDL_PROP_PROCESS_CREATE_STDOUT_POINTER;
715constexpr auto CREATE_STDERR_NUMBER = SDL_PROP_PROCESS_CREATE_STDERR_NUMBER;
717constexpr auto CREATE_STDERR_POINTER = SDL_PROP_PROCESS_CREATE_STDERR_POINTER;
719constexpr auto CREATE_STDERR_TO_STDOUT_BOOLEAN =
720 SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN;
722constexpr auto CREATE_BACKGROUND_BOOLEAN =
723 SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN;
725#if SDL_VERSION_ATLEAST(3, 4, 0)
727constexpr auto CREATE_CMDLINE_STRING = SDL_PROP_PROCESS_CREATE_CMDLINE_STRING;
731constexpr auto PID_NUMBER = SDL_PROP_PROCESS_PID_NUMBER;
733constexpr auto STDIN_POINTER = SDL_PROP_PROCESS_STDIN_POINTER;
735constexpr auto STDOUT_POINTER = SDL_PROP_PROCESS_STDOUT_POINTER;
737constexpr auto STDERR_POINTER = SDL_PROP_PROCESS_STDERR_POINTER;
739constexpr auto BACKGROUND_BOOLEAN = SDL_PROP_PROCESS_BACKGROUND_BOOLEAN;
774 return {
CheckError(SDL_GetProcessProperties(process))};
812 auto data =
static_cast<char*
>(SDL_ReadProcess(process, &size, exitcode));
847 return {SDL_GetProcessInput(process)};
879 return {SDL_GetProcessOutput(process)};
946 return SDL_WaitProcess(process, block, exitcode);
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:163
Process(const char *const *args, bool pipe_stdio)
Create a new process.
Definition: SDL3pp_process.h:211
constexpr ProcessRaw release() noexcept
Retrieves underlying ProcessRaw and clear this.
Definition: SDL3pp_process.h:310
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:295
constexpr ProcessRaw get() const noexcept
Retrieves underlying ProcessRaw.
Definition: SDL3pp_process.h:307
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:286
OwnArray< T > ReadAs(int *exitcode=nullptr)
Read all the output from a process.
Definition: SDL3pp_process.h:425
constexpr Process(const Process &other)=delete
Copy constructor.
~Process()
Destructor.
Definition: SDL3pp_process.h:292
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:882
void KillProcess(ProcessParam process, bool force)
Stop a process.
Definition: SDL3pp_process.h:906
void Destroy()
Destroy a previously created process object.
Definition: SDL3pp_process.h:972
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:877
StringResult ReadProcess(ProcessParam process, int *exitcode=nullptr)
Read all the output from a process.
Definition: SDL3pp_process.h:809
PropertiesRef GetProperties() const
Get the properties associated with a process.
Definition: SDL3pp_process.h:777
IOStreamRef GetProcessInput(ProcessParam process)
Get the IOStream associated with process standard input.
Definition: SDL3pp_process.h:845
Process CreateProcess(const char *const *args, bool pipe_stdio)
Create a new process.
Definition: SDL3pp_process.h:613
Process CreateProcessWithProperties(PropertiesParam props)
Create a new process with the specified properties.
Definition: SDL3pp_process.h:688
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:949
void Kill(bool force)
Stop a process.
Definition: SDL3pp_process.h:911
bool WaitProcess(ProcessParam process, bool block, int *exitcode)
Wait for a process to finish.
Definition: SDL3pp_process.h:944
PropertiesRef GetProcessProperties(ProcessParam process)
Get the properties associated with a process.
Definition: SDL3pp_process.h:772
void DestroyProcess(ProcessRaw process)
Destroy a previously created process object.
Definition: SDL3pp_process.h:970
IOStreamRef GetInput()
Get the IOStream associated with process standard input.
Definition: SDL3pp_process.h:850
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:816
Main include header for the SDL3pp library.
Semi-safe reference for IOStream.
Definition: SDL3pp_iostream.h:1655
Safely wrap Process for non owning parameters.
Definition: SDL3pp_process.h:44
constexpr ProcessParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_process.h:54
constexpr ProcessParam(ProcessRaw value)
Constructs from ProcessRaw.
Definition: SDL3pp_process.h:48
constexpr auto operator<=>(const ProcessParam &other) const =default
Comparison.
ProcessRaw value
parameter's ProcessRaw
Definition: SDL3pp_process.h:45
Semi-safe reference for Process.
Definition: SDL3pp_process.h:537
ProcessRef(const ProcessRef &other) noexcept
Copy constructor.
Definition: SDL3pp_process.h:565
~ProcessRef()
Destructor.
Definition: SDL3pp_process.h:571
ProcessRef(ProcessRaw resource) noexcept
Constructs from ProcessParam.
Definition: SDL3pp_process.h:559
ProcessRef(ProcessParam resource) noexcept
Constructs from ProcessParam.
Definition: SDL3pp_process.h:547
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:53
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:711
A simple std::string-like interface for SDL allocated strings.
Definition: SDL3pp_strings.h:153