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 ProcessBase;
35
36// Forward decl
37struct Process;
38
40using ProcessRaw = SDL_Process*;
41
48
88using ProcessIO = SDL_ProcessIO;
89
91 SDL_PROCESS_STDIO_INHERITED;
93
95 SDL_PROCESS_STDIO_NULL;
96
101constexpr ProcessIO PROCESS_STDIO_APP = SDL_PROCESS_STDIO_APP;
102
104constexpr ProcessIO PROCESS_STDIO_REDIRECT = SDL_PROCESS_STDIO_REDIRECT;
105
111struct ProcessBase : ResourceBaseT<ProcessRaw>
112{
114
129 void Destroy();
130
160
187 StringResult Read(int* exitcode = nullptr);
188
213 template<class T>
214 OwnArray<T> ReadAs(int* exitcode = nullptr)
215 {
216 StringResult data = Read(exitcode);
217 if (data.empty()) return {};
218 size_t sz = data.size() / sizeof(T);
219 return OwnArray{static_cast<T*>(data.release()), sz};
220 }
221
246
269
289 void Kill(bool force);
290
321 bool Wait(bool block, int* exitcode);
322};
323
334{
335 using ProcessBase::ProcessBase;
336
344 constexpr explicit Process(ProcessRaw resource) noexcept
345 : ProcessBase(resource)
346 {
347 }
348
350 constexpr Process(Process&& other) noexcept
351 : Process(other.release())
352 {
353 }
354
394 Process(const char* const* args, bool pipe_stdio);
395
466 Process(PropertiesRef props);
467
469 ~Process() { SDL_DestroyProcess(get()); }
470
472 constexpr Process& operator=(Process&& other) noexcept
473 {
474 swap(*this, other);
475 return *this;
476 }
477};
478
518inline Process CreateProcess(const char* const* args, bool pipe_stdio)
519{
520 return Process(args, pipe_stdio);
521}
522
523inline Process::Process(const char* const* args, bool pipe_stdio)
524 : Process(SDL_CreateProcess(args, pipe_stdio))
525{
526}
527
529 : Process(SDL_CreateProcessWithProperties(props))
530{
531}
532
603{
604 return Process(props);
605}
606
613
614constexpr auto ARGS_POINTER =
615 SDL_PROP_PROCESS_CREATE_ARGS_POINTER;
616
617constexpr auto ENVIRONMENT_POINTER =
618 SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER;
619
620#if SDL_VERSION_ATLEAST(3, 4, 0)
621
623 SDL_PROP_PROCESS_CREATE_WORKING_DIRECTORY_STRING;
625
626#endif // SDL_VERSION_ATLEAST(3, 4, 0)
627
628constexpr auto STDIN_NUMBER =
629 SDL_PROP_PROCESS_CREATE_STDIN_NUMBER;
630
631constexpr auto STDIN_POINTER =
632 SDL_PROP_PROCESS_CREATE_STDIN_POINTER;
633
634constexpr auto STDOUT_NUMBER =
635 SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER;
636
637constexpr auto STDOUT_POINTER =
638 SDL_PROP_PROCESS_CREATE_STDOUT_POINTER;
639
640constexpr auto STDERR_NUMBER =
641 SDL_PROP_PROCESS_CREATE_STDERR_NUMBER;
642
643constexpr auto STDERR_POINTER =
644 SDL_PROP_PROCESS_CREATE_STDERR_POINTER;
645
647 SDL_PROP_PROCESS_CREATE_STDERR_TO_STDOUT_BOOLEAN;
649
650constexpr auto BACKGROUND_BOOLEAN =
651 SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN;
652
653#if SDL_VERSION_ATLEAST(3, 4, 0)
654
655constexpr auto CMDLINE_STRING =
656 SDL_PROP_PROCESS_CREATE_CMDLINE_STRING;
657
658#endif // SDL_VERSION_ATLEAST(3, 4, 0)
659
660} // namespace prop::Process::Create
661
692{
693 return {CheckError(SDL_GetProcessProperties(process))};
694}
695
700
712namespace prop::Process {
713
714constexpr auto PID_NUMBER = SDL_PROP_PROCESS_PID_NUMBER;
715
716constexpr auto STDIN_POINTER =
717 SDL_PROP_PROCESS_STDIN_POINTER;
718
719constexpr auto STDOUT_POINTER =
720 SDL_PROP_PROCESS_STDOUT_POINTER;
721
722constexpr auto STDERR_POINTER =
723 SDL_PROP_PROCESS_STDERR_POINTER;
724
725constexpr auto BACKGROUND_BOOLEAN =
726 SDL_PROP_PROCESS_BACKGROUND_BOOLEAN;
727
728} // namespace prop::Process
729
757inline StringResult ReadProcess(ProcessRef process, int* exitcode = nullptr)
758{
759 size_t size = 0;
760 auto data = static_cast<char*>(SDL_ReadProcess(process, &size, exitcode));
761 return StringResult(CheckError(data), size);
762}
763
764inline StringResult ProcessBase::Read(int* exitcode)
765{
766 return SDL::ReadProcess(get(), exitcode);
767}
768
794{
795 return {SDL_GetProcessInput(process)};
796}
797
802
826{
827 return {SDL_GetProcessOutput(process)};
828}
829
834
854inline void KillProcess(ProcessRef process, bool force)
855{
856 CheckError(SDL_KillProcess(process, force));
857}
858
859inline void ProcessBase::Kill(bool force) { SDL::KillProcess(get(), force); }
860
892inline bool WaitProcess(ProcessRef process, bool block, int* exitcode)
893{
894 return SDL_WaitProcess(process, block, exitcode);
895}
896
897inline bool ProcessBase::Wait(bool block, int* exitcode)
898{
899 return SDL::WaitProcess(get(), block, exitcode);
900}
901
918inline void DestroyProcess(ProcessRaw process) { SDL_DestroyProcess(process); }
919
921
923
924} // namespace SDL
925
926#endif /* SDL3PP_PROCESS_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
T * release()
Release control on object.
Definition SDL3pp_ownPtr.h:91
constexpr bool empty() const
True if size() == 0.
Definition SDL3pp_ownPtr.h:74
constexpr size_t size() const
Size.
Definition SDL3pp_ownPtr.h:83
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
ResourceRefT< IOStreamBase > IOStreamRef
Reference for IOStream.
Definition SDL3pp_iostream.h:37
StringResult ReadProcess(ProcessRef process, int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:757
IOStreamRef GetInput()
Get the IOStream associated with process standard input.
Definition SDL3pp_process.h:798
SDL_ProcessIO ProcessIO
Description of where standard I/O should be directed when creating a process.
Definition SDL3pp_process.h:88
ResourceRefT< ProcessBase > ProcessRef
Reference for Process.
Definition SDL3pp_process.h:47
StringResult Read(int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:764
void Kill(bool force)
Stop a process.
Definition SDL3pp_process.h:859
IOStreamRef GetProcessOutput(ProcessRef process)
Get the IOStream associated with process standard output.
Definition SDL3pp_process.h:825
PropertiesRef GetProcessProperties(ProcessRef process)
Get the properties associated with a process.
Definition SDL3pp_process.h:691
Process CreateProcess(const char *const *args, bool pipe_stdio)
Create a new process.
Definition SDL3pp_process.h:518
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:101
PropertiesRef GetProperties() const
Get the properties associated with a process.
Definition SDL3pp_process.h:696
constexpr ProcessIO PROCESS_STDIO_NULL
The I/O stream is ignored.
Definition SDL3pp_process.h:94
void KillProcess(ProcessRef process, bool force)
Stop a process.
Definition SDL3pp_process.h:854
Process CreateProcessWithProperties(PropertiesRef props)
Create a new process with the specified properties.
Definition SDL3pp_process.h:602
bool WaitProcess(ProcessRef process, bool block, int *exitcode)
Wait for a process to finish.
Definition SDL3pp_process.h:892
void DestroyProcess(ProcessRaw process)
Destroy a previously created process object.
Definition SDL3pp_process.h:918
SDL_Process * ProcessRaw
Alias to raw representation for Process.
Definition SDL3pp_process.h:40
IOStreamRef GetProcessInput(ProcessRef process)
Get the IOStream associated with process standard input.
Definition SDL3pp_process.h:793
bool Wait(bool block, int *exitcode)
Wait for a process to finish.
Definition SDL3pp_process.h:897
constexpr ProcessIO PROCESS_STDIO_REDIRECT
The I/O stream is redirected to an existing IOStream.
Definition SDL3pp_process.h:104
constexpr ProcessIO PROCESS_STDIO_INHERITED
The I/O stream is inherited from the application.
Definition SDL3pp_process.h:90
IOStreamRef GetOutput()
Get the IOStream associated with process standard output.
Definition SDL3pp_process.h:830
void Destroy()
Destroy a previously created process object.
Definition SDL3pp_process.h:920
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:53
Properties for process creation.
Definition SDL3pp_process.h:612
constexpr auto CMDLINE_STRING
String for cmdline.
Definition SDL3pp_process.h:655
constexpr auto WORKING_DIRECTORY_STRING
String for working directory.
Definition SDL3pp_process.h:622
constexpr auto BACKGROUND_BOOLEAN
Enable background.
Definition SDL3pp_process.h:650
constexpr auto STDERR_NUMBER
Stderr number.
Definition SDL3pp_process.h:640
constexpr auto ENVIRONMENT_POINTER
Pointer to environment.
Definition SDL3pp_process.h:617
constexpr auto ARGS_POINTER
Pointer to args.
Definition SDL3pp_process.h:614
constexpr auto STDIN_POINTER
Pointer to stdin.
Definition SDL3pp_process.h:631
constexpr auto STDERR_TO_STDOUT_BOOLEAN
Enable stderr to stdout.
Definition SDL3pp_process.h:646
constexpr auto STDIN_NUMBER
Stdin number.
Definition SDL3pp_process.h:628
constexpr auto STDOUT_NUMBER
Stdout number.
Definition SDL3pp_process.h:634
constexpr auto STDOUT_POINTER
Pointer to stdout.
Definition SDL3pp_process.h:637
constexpr auto STDERR_POINTER
Pointer to stderr.
Definition SDL3pp_process.h:643
Properties for processes.
Definition SDL3pp_process.h:612
constexpr auto BACKGROUND_BOOLEAN
Background enabled.
Definition SDL3pp_process.h:725
constexpr auto STDOUT_POINTER
Pointer to stdout.
Definition SDL3pp_process.h:719
constexpr auto PID_NUMBER
Pid number.
Definition SDL3pp_process.h:714
constexpr auto STDIN_POINTER
Pointer to stdin.
Definition SDL3pp_process.h:716
constexpr auto STDERR_POINTER
Pointer to stderr.
Definition SDL3pp_process.h:722
Main include header for the SDL3pp library.
Base class to Process.
Definition SDL3pp_process.h:112
OwnArray< T > ReadAs(int *exitcode=nullptr)
Read all the output from a process.
Definition SDL3pp_process.h:214
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
An opaque handle representing a system process.
Definition SDL3pp_process.h:334
constexpr Process(Process &&other) noexcept
Move constructor.
Definition SDL3pp_process.h:350
constexpr Process & operator=(Process &&other) noexcept
Assignment operator.
Definition SDL3pp_process.h:472
~Process()
Destructor.
Definition SDL3pp_process.h:469
constexpr Process(ProcessRaw resource) noexcept
Constructs from raw Process.
Definition SDL3pp_process.h:344
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93
A simple std::string-like interface for SDL allocated strings.
Definition SDL3pp_strings.h:147