Skip to content

Commit

Permalink
feat: Make measurement iterator a forward iterator
Browse files Browse the repository at this point in the history
At this point in time, the iterator for `MeasurementContainer` does not
match the `std::forward_iterator` concept which SonarCloud really wants
me to use. In this commit, I make sure that the iterator satisfies the
concept.

Unfortunately, this requires a default-initializer, because
`std::forward_iterator` requires `std::incrementable` which requires
`std::regular`, which requires `std::semiregular` which in turn requires
`std::default_initializable` which I think is silly.
  • Loading branch information
stephenswat committed Sep 30, 2024
1 parent c93ff9f commit d00ec45
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Examples/Framework/include/ActsExamples/EventData/Measurement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <concepts>
#include <cstddef>
#include <iosfwd>
#include <iterator>
#include <type_traits>
#include <variant>
#include <vector>
Expand Down Expand Up @@ -138,13 +139,29 @@ class MeasurementContainer {
using Container = std::conditional_t<Const, const MeasurementContainer,
MeasurementContainer>;

IteratorImpl() : m_container(nullptr), m_index(0) {}

IteratorImpl(Container& container, std::size_t index)
: m_container(container), m_index(index) {}
: m_container(&container), m_index(index) {}

template <bool OtherConst>
explicit IteratorImpl(const IteratorImpl<OtherConst>& o)
: m_container(o.m_container), m_index(o.m_index) {}

reference operator*() const { return m_container.getMeasurement(m_index); }
reference operator*() const {
assert(m_container != nullptr);
return m_container->getMeasurement(m_index);
}

pointer operator->() const { return &operator*(); }

template <bool OtherConst>
IteratorImpl& operator=(const IteratorImpl<OtherConst>& o) {
m_container = o.m_container;
m_index = o.m_index;
return *this;
}

IteratorImpl& operator++() {
++m_index;
return *this;
Expand All @@ -161,7 +178,7 @@ class MeasurementContainer {
}

private:
Container& m_container;
Container* m_container;
Index m_index;
};

Expand Down

0 comments on commit d00ec45

Please sign in to comment.