CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
process.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_SYSTEM_PROCESS_H
10#define CPPCOMMON_SYSTEM_PROCESS_H
11
12#include "errors/exceptions.h"
13#include "system/pipe.h"
14#include "time/timestamp.h"
15
16#include <limits>
17#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21
23
26#define __PROCESS__ CppCommon::Process::CurrentProcessId()
27
28namespace CppCommon {
29
31
38{
39public:
40 Process(const Process& process);
41 Process(Process&& process) noexcept;
42 ~Process();
43
44 Process& operator=(const Process& process);
45 Process& operator=(Process&& process) noexcept;
46
48 uint64_t pid() const noexcept;
49
51 bool IsRunning() const;
52
54 void Kill();
55
57
62 int Wait();
63
65
71 int WaitFor(const Timespan& timespan);
73
79 int WaitUntil(const UtcTimestamp& timestamp)
80 { return WaitFor(timestamp - UtcTimestamp()); }
81
83
86 static uint64_t CurrentProcessId() noexcept;
88
91 static uint64_t ParentProcessId() noexcept;
92
94
97 static Process CurrentProcess();
99
102 static Process ParentProcess();
103
105
108 static void Exit(int result);
109
111
125 static Process Execute(const std::string& command, const std::vector<std::string>* arguments = nullptr, const std::map<std::string, std::string>* envars = nullptr, const std::string* directory = nullptr, Pipe* input = nullptr, Pipe* output = nullptr, Pipe* error = nullptr);
126
128 void swap(Process& process) noexcept;
129 friend void swap(Process& process1, Process& process2) noexcept;
130
131private:
132 class Impl;
133
134 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
135 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
136
137 static const size_t StorageSize = 16;
138#if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
139 static const size_t StorageAlign = 4;
140#else
141 static const size_t StorageAlign = 8;
142#endif
143 alignas(StorageAlign) std::byte _storage[StorageSize];
144
145 Process();
146 Process(uint64_t pid);
147};
148
152} // namespace CppCommon
153
154#include "process.inl"
155
156#endif // CPPCOMMON_SYSTEM_PROCESS_H
Pipe.
Definition pipe.h:31
Process abstraction.
Definition process.h:38
friend void swap(Process &process1, Process &process2) noexcept
Definition process.inl:21
static Process ParentProcess()
Get the parent process.
Definition process.inl:16
static Process Execute(const std::string &command, const std::vector< std::string > *arguments=nullptr, const std::map< std::string, std::string > *envars=nullptr, const std::string *directory=nullptr, Pipe *input=nullptr, Pipe *output=nullptr, Pipe *error=nullptr)
Execute a new process.
Definition process.cpp:549
static Process CurrentProcess()
Get the current process.
Definition process.inl:11
void Kill()
Kill the process.
Definition process.cpp:540
static uint64_t ParentProcessId() noexcept
Get the parent process Id.
Definition process.cpp:545
int WaitUntil(const UtcTimestamp &timestamp)
Wait the process to exit until the given timestamp.
Definition process.h:79
int Wait()
Wait the process to exit.
Definition process.cpp:541
bool IsRunning() const
Is the process is running?
Definition process.cpp:538
static uint64_t CurrentProcessId() noexcept
Get the current process Id.
Definition process.cpp:544
Process & operator=(const Process &process)
Definition process.cpp:524
static void Exit(int result)
Exit the current process.
Definition process.cpp:547
Process(const Process &process)
Definition process.cpp:502
uint64_t pid() const noexcept
Get the process Id.
Definition process.cpp:536
int WaitFor(const Timespan &timespan)
Wait the process to exit for the given timespan.
Definition process.cpp:542
Exceptions definition.
C++ Common project definitions.
Pipe definition.
Process inline implementation.
Timestamp definition.