From 4725b4bc48722356fea4570ed7770137a0999491 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 1 Jun 2024 17:09:44 +0800 Subject: HALF WORK: develop SubProcess. --- include/cru/common/SubProcess.h | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'include/cru/common/SubProcess.h') 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 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 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 platform_process_; }; } // namespace cru -- cgit v1.2.3