#pragma once /** * @file condition_monitor.hpp * @brief * @author your name (you@domain.com) * @version 0.1 * @date 2025-12-15 * * Copyright: Baosight Co. Ltd. * DO NOT COPY/USE WITHOUT PERMISSION * */ class ConditionMonitor { public: /** * 构造函数 * @param threshold 连续不满足条件的次数阈值N */ explicit ConditionMonitor(int threshold) : threshold_n_(threshold), consecutive_count_(0), is_triggered_(false) {} /** * @brief 状态更新 * @param condition_a 外部条件 * @return true * @return false */ bool update(bool condition_a) { if (!condition_a) { consecutive_count_++; // 检查是否达到阈值且本轮尚未触发 if (consecutive_count_ >= threshold_n_ && !is_triggered_) { resetCount(); is_triggered_ = true; return true; } } else { // 条件满足,重置状态 resetCount(); is_triggered_ = false; } return false; } /// 清空计数器 void resetCount() { consecutive_count_ = 0; } /// 获取当前连续不满足条件的计数 int getCurrentCount() const { return consecutive_count_; } /// 获取阈值 int getThreshold() const { return threshold_n_; } /** * @brief Set the Threshold object * @param th 阈值 * @return int */ int setThreshold(int th) { threshold_n_ = th; return threshold_n_; } /** * @brief Get the Current State object * @return true * @return false */ bool getCurrentState() const { return is_triggered_; } private: int threshold_n_; // 阈值N int consecutive_count_; // 连续不满足计数 bool is_triggered_; // 防止重复触发的标志 };