From c760250dbed1ed0a55965f75698fd1699b18a03a Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sun, 15 Sep 2024 19:35:18 +0800 Subject: [PATCH] tmp update --- example/process_ex.cpp | 3 -- lib/utility/process_monitor.cpp | 10 +++++-- lib/utility/process_monitor.h | 52 +++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/example/process_ex.cpp b/example/process_ex.cpp index 38d11ea..060dc2f 100644 --- a/example/process_ex.cpp +++ b/example/process_ex.cpp @@ -49,9 +49,6 @@ public: set_end_process(); return; } - -private: - /* data */ }; int main(int argc, char const *argv[]) try diff --git a/lib/utility/process_monitor.cpp b/lib/utility/process_monitor.cpp index d67e6eb..d99c595 100644 --- a/lib/utility/process_monitor.cpp +++ b/lib/utility/process_monitor.cpp @@ -81,12 +81,13 @@ void gctl::process_monitor::start_monitoring() return; } -void gctl::process_monitor::wait_for_kerboard() +void gctl::process_monitor::wait_for_kerboard(int sec) { - std::clog << "Press 'p' to pause. Continue in 1 seconds ..." << std::endl; // 键盘事件受到monitor_input的监控 + // 键盘事件受到 keyboard_actions 的监控 + std::cout << "Press 'p' to pause. Continue in " << sec << " seconds ..." << std::endl; std::unique_lock lck(mtx_); // 设置锁 等待监控函数的响应 如果超时则继续 - if (cv_.wait_for(lck, std::chrono::seconds(1)) == std::cv_status::timeout) + if (cv_.wait_for(lck, std::chrono::seconds(sec)) == std::cv_status::timeout) { std::clog << "\x1b[1A\r\x1b[K"; } @@ -102,9 +103,12 @@ void gctl::process_monitor::keyboard_actions() if (key_value == 112) // p的键值为112 { std::unique_lock lck(mtx_); + + // 这里可以设置额外的条件和命令 std::cout << "\rInput 'q' to quit: "; std::cin >> key_str; + // 执行更多的命令 if (key_str == "q") set_end_process(true); cv_.notify_one(); } diff --git a/lib/utility/process_monitor.h b/lib/utility/process_monitor.h index a47bbc8..b5649e4 100644 --- a/lib/utility/process_monitor.h +++ b/lib/utility/process_monitor.h @@ -42,19 +42,67 @@ namespace gctl process_monitor(); virtual ~process_monitor(); + /** + * @brief Implement the process that you want to be monitored here. + * This is just an API here. A template is shown as bellow: + * + * void process() + * { + * while (1) + * { + * // do something here + * .... + * .... + * + * wait_for_kerboard(); + * if (end_process()) break; + * } + * + * set_end_process(); + * return; + * } + */ virtual void process() = 0; + + /** + * @brief Start the monitoring process. + * + */ virtual void start_monitoring(); - virtual void wait_for_kerboard(); + + /** + * @brief Waiting the keyboard input to pause. + * + * @param sec wait time. + */ + virtual void wait_for_kerboard(int sec = 1); + + /** + * @brief Implement excepted actions here when 'p' is pressed. + * + */ virtual void keyboard_actions(); + + /** + * @brief Check to see if the process is set to end. + * + */ virtual bool end_process(); + + /** + * @brief Set the end process object to true. + * + * @param ask Ask before setting the end process object to true. + */ void set_end_process(bool ask = false); + private: + std::thread process_wapper() { return std::thread(&process_monitor::process, this); } - private: bool end_process_; std::mutex mtx_; std::condition_variable cv_;