From 3e2f46acde6c70e6c21007e6a663326570b01130 Mon Sep 17 00:00:00 2001 From: wxnzb <3127484862@qq.com> Date: Sat, 18 Jan 2025 20:16:50 +0800 Subject: [PATCH] variable-length-configuration --- src/config.cc | 23 +++++++++++++---------- src/config.h | 3 ++- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/config.cc b/src/config.cc index 37190fa9..a23fe758 100644 --- a/src/config.cc +++ b/src/config.cc @@ -71,18 +71,21 @@ Status StringValue::SetValue(const std::string& value) { return Status::OK(); } -Status StringValueArray::SetValue(const std::string& value) { +Status StringValueArray::SetValue(const std::string& value) { return SetValue(value, false); } + +Status StringValueArray::SetValue(const std::string& value, bool check) { auto values = SplitString(value, delimiter_); - if (!values_.empty()) { // if the value_ is not empty, check the number of parameters - if (values.size() < values_.size()) { - return Status::InvalidArgument("The number of parameters is less than required."); + if (check) { + if (values.size() != values_.size()) { + return Status::InvalidArgument("The number of parameters does not match."); + } + } else { + if (values_.empty()) { + values_.resize(values.size()); + } + for (size_t i = 0; i < values.size(); i++) { + values_[i] = std::move(values[i]); } - } else { // if the value_ is empty, resize the value_ to the size of the values - values_.resize(values.size()); - } - - for (int i = 0; i < values.size(); i++) { - values_[i] = std::move(values[i]); } return Status::OK(); } diff --git a/src/config.h b/src/config.h index dad56b87..96b5256a 100644 --- a/src/config.h +++ b/src/config.h @@ -84,9 +84,10 @@ class StringValueArray : public BaseValue { std::string Value() const override { return pstd::StringConcat(values_, delimiter_); }; + Status SetValue(const std::string& value, bool check); + private: Status SetValue(const std::string& value) override; - std::vector values_; char delimiter_ = 0; };