diff options
author | crupest <crupest@outlook.com> | 2024-06-01 17:09:44 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-06-08 23:18:04 +0800 |
commit | 4725b4bc48722356fea4570ed7770137a0999491 (patch) | |
tree | 4d2ca789e2205496f2c64f3b50a371ed131c0074 /include/cru/common/SubProcess.h | |
parent | b165ebcfb9c278dc7481abb5287e85672731c005 (diff) | |
download | cru-4725b4bc48722356fea4570ed7770137a0999491.tar.gz cru-4725b4bc48722356fea4570ed7770137a0999491.tar.bz2 cru-4725b4bc48722356fea4570ed7770137a0999491.zip |
HALF WORK: develop SubProcess.
Diffstat (limited to 'include/cru/common/SubProcess.h')
-rw-r--r-- | include/cru/common/SubProcess.h | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/include/cru/common/SubProcess.h b/include/cru/common/SubProcess.h index 9a56b726..0f87b184 100644 --- a/include/cru/common/SubProcess.h +++ b/include/cru/common/SubProcess.h @@ -87,8 +87,10 @@ struct SubProcessExitResult { * process will be killed. */ class PlatformSubProcessBase : public Object { + CRU_DEFINE_CLASS_LOG_TAG(u"PlatformSubProcessBase") + public: - explicit PlatformSubProcessBase(const SubProcessStartInfo& start_info); + explicit PlatformSubProcessBase(SubProcessStartInfo start_info); ~PlatformSubProcessBase() override; @@ -141,6 +143,8 @@ class PlatformSubProcessBase : public Object { virtual io::Stream* GetStdoutStream() = 0; virtual io::Stream* GetStderrStream() = 0; + void SetDeleteSelfOnExit(bool enable); + protected: /** * @brief Create and start a real process. If the program can't be created or @@ -181,6 +185,8 @@ class PlatformSubProcessBase : public Object { private: SubProcessStatus status_; + bool delete_self_; + std::thread process_thread_; std::mutex process_mutex_; std::unique_lock<std::mutex> process_lock_; @@ -188,16 +194,38 @@ class PlatformSubProcessBase : public Object { }; class CRU_BASE_API SubProcess : public Object { + CRU_DEFINE_CLASS_LOG_TAG(u"SubProcess") + public: - SubProcess(); + SubProcess(SubProcessStartInfo start_info); CRU_DELETE_COPY(SubProcess) - SubProcess(SubProcess&& other); - SubProcess& operator=(SubProcess&& other); + SubProcess(SubProcess&& other) = default; + SubProcess& operator=(SubProcess&& other) = default; ~SubProcess(); + public: + void Wait(std::optional<std::chrono::milliseconds> wait_time); + void Kill(); + + SubProcessStatus GetStatus(); + SubProcessExitResult GetExitResult(); + + io::Stream* GetStdinStream(); + io::Stream* GetStdoutStream(); + io::Stream* GetStderrStream(); + + void Detach(); + + bool IsValid() const { return platform_process_ != nullptr; } + explicit operator bool() const { return IsValid(); } + + private: + void CheckValid() const; + private: + std::unique_ptr<PlatformSubProcessBase> platform_process_; }; } // namespace cru |