mirror of
https://github.com/JakeHillion/object-introspection.git
synced 2024-11-09 21:24:14 +00:00
tbv2: improve equality for iterator
This commit is contained in:
parent
e7581ad915
commit
4563cbe713
@ -72,9 +72,17 @@ inline const result::Element* IntrospectionResult::const_iterator::operator->()
|
|||||||
|
|
||||||
inline bool IntrospectionResult::const_iterator::operator==(
|
inline bool IntrospectionResult::const_iterator::operator==(
|
||||||
const IntrospectionResult::const_iterator& that) const {
|
const IntrospectionResult::const_iterator& that) const {
|
||||||
return this->data_ == that.data_ && !this->next_.has_value() &&
|
// Case 1: The next data to read differs, thus the iterators are different.
|
||||||
!that.next_.has_value(); // TODO: is this sufficient? kind of hacky as
|
if (this->data_ != that.data_)
|
||||||
// this only works for comparing to .end()
|
return false;
|
||||||
|
// Case 2: Both iterators have no next value, thus they are both complete. It
|
||||||
|
// is insufficient to check increments as the number of increments is unknown
|
||||||
|
// when constructing `end()`.
|
||||||
|
if (!this->next_.has_value() && !that.next_.has_value())
|
||||||
|
return true;
|
||||||
|
// Case 3: The iterators are reading the same data. If they have produced the
|
||||||
|
// same number of elements they are equal, else they are not.
|
||||||
|
return this->increments_ == that.increments_;
|
||||||
}
|
}
|
||||||
inline bool IntrospectionResult::const_iterator::operator!=(
|
inline bool IntrospectionResult::const_iterator::operator!=(
|
||||||
const IntrospectionResult::const_iterator& that) const {
|
const IntrospectionResult::const_iterator& that) const {
|
||||||
|
@ -63,6 +63,12 @@ class IntrospectionResult {
|
|||||||
// improvement but it isn't copyable. A string type with size fixed at
|
// improvement but it isn't copyable. A string type with size fixed at
|
||||||
// construction would also be good.
|
// construction would also be good.
|
||||||
std::list<std::string> dynamic_type_path_;
|
std::list<std::string> dynamic_type_path_;
|
||||||
|
|
||||||
|
// We cannot track the position in the iteration solely by the underlying
|
||||||
|
// iterator as some fields do not extract data (for example, primitives).
|
||||||
|
// Track the number of increment operations as well to get an accurate
|
||||||
|
// equality check.
|
||||||
|
uint64_t increments_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
IntrospectionResult(std::vector<uint8_t> buf, exporters::inst::Inst inst);
|
IntrospectionResult(std::vector<uint8_t> buf, exporters::inst::Inst inst);
|
||||||
|
@ -30,9 +30,13 @@ namespace oi {
|
|||||||
IntrospectionResult::const_iterator&
|
IntrospectionResult::const_iterator&
|
||||||
IntrospectionResult::const_iterator::operator++() {
|
IntrospectionResult::const_iterator::operator++() {
|
||||||
if (stack_.empty()) {
|
if (stack_.empty()) {
|
||||||
next_ = std::nullopt;
|
if (next_ != std::nullopt) {
|
||||||
|
++increments_;
|
||||||
|
next_ = std::nullopt;
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
++increments_;
|
||||||
|
|
||||||
auto el = stack_.top();
|
auto el = stack_.top();
|
||||||
stack_.pop();
|
stack_.pop();
|
||||||
|
Loading…
Reference in New Issue
Block a user