Add ReadList helper#285
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
f2a734a to
245f792
Compare
|
Forced push 245f792 to just rebase with master |
245f792 to
5d23f83
Compare
|
Forced push 5d23f83 applying @ryanofsky suggestion. It reads cleaner and dedup more code. |
| template <typename Input, typename ReadDest> | ||
| decltype(auto) CustomReadField(TypeList<std::vector<bool>>, | ||
| Priority<1>, | ||
| InvokeContext& invoke_context, | ||
| Input&& input, | ||
| ReadDest&& read_dest) | ||
| { | ||
| return read_dest.update([&](auto& value) { | ||
| auto data = input.get(); | ||
| value.clear(); | ||
| value.reserve(data.size()); | ||
| for (auto item : data) { | ||
| value.push_back(ReadField(TypeList<bool>(), invoke_context, Make<ValueField>(item), ReadDestTemp<bool>())); | ||
| } | ||
| }); | ||
| } | ||
| } // namespace mp |
There was a problem hiding this comment.
Does this work here?
diff --git a/include/mp/type-vector.h b/include/mp/type-vector.h
index 25e74a6..cd269cb 100644
--- a/include/mp/type-vector.h
+++ b/include/mp/type-vector.h
@@ -50,14 +50,16 @@ decltype(auto) CustomReadField(TypeList<std::vector<bool>>,
Input&& input,
ReadDest&& read_dest)
{
- return read_dest.update([&](auto& value) {
- auto data = input.get();
- value.clear();
- value.reserve(data.size());
- for (auto item : data) {
- value.push_back(ReadField(TypeList<bool>(), invoke_context, Make<ValueField>(item), ReadDestTemp<bool>()));
- }
- });
+ return ReadList(
+ TypeList<bool>(), invoke_context, input, read_dest,
+ [&](auto& value, size_t size) {
+ value.clear();
+ value.reserve(size);
+ },
+ [&](auto& value, auto&& item) {
+ value.push_back(item);
+ return value.back();
+ });
}
} // namespace mpThere was a problem hiding this comment.
oh I missed this case, thanks for pointing!
I tested your approach and it does compile and passes the tests but I think it is better to not add return value.back(); because bool is read via construct(), so the return value is always unused.
Also I think it is a good opportunity to add a comment to explain why there's this exception for std::vector<bool>.
What do you think?
diff --git a/include/mp/type-vector.h b/include/mp/type-vector.h
index 25e74a6..49ca3c2 100644
--- a/include/mp/type-vector.h
+++ b/include/mp/type-vector.h
@@ -43,6 +43,11 @@ decltype(auto) CustomReadField(TypeList<std::vector<LocalType>>,
});
}
+//! Overload CustomReadField for std::vector<bool>, which needs its own handler
+//! because its back() returns a proxy by value (std::vector<bool>::reference)
+//! rather than a reference, so it can't use the generic vector in-place emplace path
+//! that returns auto&. Not returning a reference is fine here: bool is read via
+//! construct(), not update(), so the return value is never used.
template <typename Input, typename ReadDest>
decltype(auto) CustomReadField(TypeList<std::vector<bool>>,
Priority<1>,
@@ -50,14 +55,15 @@ decltype(auto) CustomReadField(TypeList<std::vector<bool>>,
Input&& input,
ReadDest&& read_dest)
{
- return read_dest.update([&](auto& value) {
- auto data = input.get();
- value.clear();
- value.reserve(data.size());
- for (auto item : data) {
- value.push_back(ReadField(TypeList<bool>(), invoke_context, Make<ValueField>(item), ReadDestTemp<bool>()));
- }
- });
+ return ReadList(
+ TypeList<bool>(), invoke_context, input, read_dest,
+ [&](auto& value, size_t size) {
+ value.clear();
+ value.reserve(size);
+ },
+ [&](auto& value, auto&& item) {
+ value.push_back(item);
+ });
}
} // namespace mp
There was a problem hiding this comment.
Great! This is definitely better, and the comment is on point.
5d23f83 to
043b8a8
Compare
|
Forced push 043b8a8 applying @xyzconstant suggestion |
|
tACK 043b8a8 Thanks for the updates @ViniciusCestarii. This is a simple refactor that moves common code to The changes are clean and can be merged as-is. |
Follow up of #277 (review). This adds
ReadListhelper and uses it to dedup theCustomReadFieldimplementations forstd::map,std::set,std::unordered_set, andstd::vector, mirroring the existingBuildListhelper on the build side.I took the liberty of adding the commit f2a734a "type: reserve first when reading std::unordered_set" so it reserves the correct capacity first and then emplaces the values.