Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:variable-length arguments in configuration(closes #146) #164

Open
wants to merge 4 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 (check) {
if (values.size() != values_.size()) {
return Status::InvalidArgument("The number of parameters does not match.");
}
} 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]);
} else {
if (values_.empty()) {
values_.resize(values.size());
}
for (size_t i = 0; i < values.size(); i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该在 value和value_ 长度不相等就需要调用 values_.resize(values.size());

比如有一种情况, value_的默认值是 ["config1", "config2", "config3"], 但是在配置文件中的配置是

["config_1", "config_2"] 如果这时不调用 resize 函数, 那么读取配置后, 内存中的实际配置是
["config_1", "config_2", "config3"] 不符合预期的

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好滴,我在改一下

values_[i] = std::move(values[i]);
}
}
return Status::OK();
}
Expand Down
3 changes: 2 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> values_;
char delimiter_ = 0;
};
Expand Down
Loading