// // Created by fly on 2022/4/21. // #ifndef FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP #define FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP #include #include class algorithm_wrap : public algorithm { public: explicit algorithm_wrap(std::shared_ptr algo) { algorithm_ = std::move(algo); } bool enable() const { return enable_; } void set_enable(bool flag) { enable_ = flag; } int duration_period() const { return duration_period_; } void set_duration_period(int durationPeriod) { duration_period_ = durationPeriod; } void set_config(nlohmann::json const &config) override { if (algorithm_) { algorithm_->set_config(config); if (config.contains("enable") && config["enable"].is_boolean()) { set_enable(config["enable"].get()); } } } nlohmann::json config() override { nlohmann::json config; config["enable"] = enable(); if (algorithm_) { return algorithm_->config(); } return config; } double eval(double value) override { if (algorithm_ && enable_ && duration_period_) { if (duration_period_ > 0) { --duration_period_; } return algorithm_->eval(value); } return value; } private: std::shared_ptr algorithm_; bool enable_ = false; int duration_period_ = -1; }; #endif //FAULT_SIMULATION_ALGORITHM_ALGORITHM_WRAP_HPP