Add interface to modify annotation elements by index

For the adaption of type system in ts2abc, we need provide interface to modify annotation elements by index

Issue: #I5E22F
Signed-off-by: qiuyu <qiuyu22@huawei.com>
Change-Id: I86487106e5f71e0bf7f93588ec02838989cfb86e
This commit is contained in:
qiuyu
2022-06-22 23:01:09 +08:00
parent 271fab796a
commit e7fc0256b6
2 changed files with 26 additions and 0 deletions
+20
View File
@@ -60,6 +60,26 @@ public:
elements_.push_back(std::forward<AnnotationElement>(element));
}
void SetOrAddElementByIndex(size_t ele_idx, AnnotationElement &&element)
{
// did not find a simple way to replace element directly because of circular dependency of class definition
auto len = elements_.size();
ASSERT(ele_idx <= len);
if (ele_idx == len) {
AddElement(std::move(element));
return;
}
std::vector<AnnotationElement> eles;
for (size_t i = 0; i < ele_idx; i++) {
eles.push_back(std::forward<AnnotationElement>(elements_[i]));
}
eles.push_back(std::forward<AnnotationElement>(element));
for (size_t i = ele_idx + 1; i < len; i++) {
eles.push_back(std::forward<AnnotationElement>(elements_[i]));
}
elements_ = std::forward<std::vector<AnnotationElement>>(eles);
}
private:
std::string record_name_;
std::vector<AnnotationElement> elements_;
+6
View File
@@ -207,6 +207,12 @@ public:
annotations_.insert(annotations_.end(), annotations.begin(), annotations.end());
}
void SetOrAddAnnotationElementByIndex(size_t anno_idx, size_t ele_idx, AnnotationElement &&element)
{
ASSERT(anno_idx < annotations_.size());
annotations_[anno_idx].SetOrAddElementByIndex(ele_idx, std::forward<AnnotationElement>(element));
}
std::optional<Error> ValidateData() override;
protected: