a/util: iterator cleanups to reduce diffs, using a self typedef.

This commit is contained in:
Ryan Pavlik 2022-02-07 11:27:34 -06:00 committed by Jakob Bornecrantz
parent 6908486022
commit c079eabfdf
2 changed files with 32 additions and 28 deletions

View file

@ -29,6 +29,7 @@ namespace detail {
friend class HistoryBufIterator<T, MaxSize>; friend class HistoryBufIterator<T, MaxSize>;
public: public:
using Self = HistoryBufConstIterator<T, MaxSize>;
using container_type = const HistoryBuffer<T, MaxSize>; using container_type = const HistoryBuffer<T, MaxSize>;
using typename base::difference_type; using typename base::difference_type;
using typename base::iterator_category; using typename base::iterator_category;
@ -79,50 +80,50 @@ namespace detail {
operator->() const noexcept; operator->() const noexcept;
//! Pre-increment: Advance, then return self. //! Pre-increment: Advance, then return self.
HistoryBufConstIterator & Self &
operator++(); operator++();
//! Post-increment: return a copy of initial state after incrementing self //! Post-increment: return a copy of initial state after incrementing self
HistoryBufConstIterator Self
operator++(int); operator++(int);
//! Pre-decrement: Subtract, then return self. //! Pre-decrement: Subtract, then return self.
HistoryBufConstIterator & Self &
operator--(); operator--();
//! Post-decrement: return a copy of initial state after decrementing self //! Post-decrement: return a copy of initial state after decrementing self
HistoryBufConstIterator Self
operator--(int); operator--(int);
// Use the base class implementation of subtracting one iterator from another // Use the base class implementation of subtracting one iterator from another
using base::operator-; using base::operator-;
//! Increment by an arbitrary amount. //! Increment by an arbitrary amount.
HistoryBufConstIterator & Self &
operator+=(std::ptrdiff_t n) noexcept; operator+=(std::ptrdiff_t n) noexcept;
//! Decrement by an arbitrary amount. //! Decrement by an arbitrary amount.
HistoryBufConstIterator & Self &
operator-=(std::ptrdiff_t n) noexcept; operator-=(std::ptrdiff_t n) noexcept;
//! Increment a copy of the iterator by an arbitrary amount. //! Increment a copy of the iterator by an arbitrary amount.
HistoryBufConstIterator Self
operator+(std::ptrdiff_t n) const noexcept; operator+(std::ptrdiff_t n) const noexcept;
//! Decrement a copy of the iterator by an arbitrary amount. //! Decrement a copy of the iterator by an arbitrary amount.
HistoryBufConstIterator Self
operator-(std::ptrdiff_t n) const noexcept; operator-(std::ptrdiff_t n) const noexcept;
private: private:
//! Factory for a "begin" iterator from a container and its helper: mostly for internal use. //! Factory for a "begin" iterator from a container and its helper: mostly for internal use.
static HistoryBufConstIterator static Self
begin(container_type &container, const RingBufferHelper<MaxSize> &helper) begin(container_type &container, const RingBufferHelper<MaxSize> &helper)
{ {
return {&container, std::move(base::begin(helper))}; return {&container, std::move(base::begin(helper))};
} }
//! Construct the "past the end" iterator that can be decremented safely //! Construct the "past the end" iterator that can be decremented safely
static HistoryBufConstIterator static Self
end(container_type &container, const RingBufferHelper<MaxSize> &helper) end(container_type &container, const RingBufferHelper<MaxSize> &helper)
{ {
return {&container, std::move(base::end(helper))}; return {&container, std::move(base::end(helper))};
@ -182,7 +183,7 @@ namespace detail {
inline HistoryBufConstIterator<T, MaxSize> inline HistoryBufConstIterator<T, MaxSize>
HistoryBufConstIterator<T, MaxSize>::operator--(int) HistoryBufConstIterator<T, MaxSize>::operator--(int)
{ {
HistoryBufConstIterator tmp = *this; Self tmp = *this;
this->decrement_n(1); this->decrement_n(1);
return tmp; return tmp;
} }
@ -207,7 +208,7 @@ namespace detail {
inline HistoryBufConstIterator<T, MaxSize> inline HistoryBufConstIterator<T, MaxSize>
HistoryBufConstIterator<T, MaxSize>::operator+(std::ptrdiff_t n) const noexcept HistoryBufConstIterator<T, MaxSize>::operator+(std::ptrdiff_t n) const noexcept
{ {
HistoryBufConstIterator ret(*this); Self ret(*this);
ret += n; ret += n;
return ret; return ret;
} }
@ -216,12 +217,13 @@ namespace detail {
inline HistoryBufConstIterator<T, MaxSize> inline HistoryBufConstIterator<T, MaxSize>
HistoryBufConstIterator<T, MaxSize>::operator-(std::ptrdiff_t n) const noexcept HistoryBufConstIterator<T, MaxSize>::operator-(std::ptrdiff_t n) const noexcept
{ {
HistoryBufConstIterator ret(*this); Self ret(*this);
ret -= n; ret -= n;
return ret; return ret;
} }
} // namespace detail } // namespace detail
// HistoryBuffer method implementations that depend on const_iterator availability
template <typename T, size_t MaxSize> template <typename T, size_t MaxSize>
inline typename HistoryBuffer<T, MaxSize>::const_iterator inline typename HistoryBuffer<T, MaxSize>::const_iterator

View file

@ -28,6 +28,7 @@ namespace detail {
friend class HistoryBuffer<T, MaxSize>; friend class HistoryBuffer<T, MaxSize>;
public: public:
using Self = HistoryBufIterator<T, MaxSize>;
using container_type = HistoryBuffer<T, MaxSize>; using container_type = HistoryBuffer<T, MaxSize>;
using typename base::difference_type; using typename base::difference_type;
using typename base::iterator_category; using typename base::iterator_category;
@ -75,53 +76,53 @@ namespace detail {
operator->() const noexcept; operator->() const noexcept;
//! Pre-increment: Advance, then return self. //! Pre-increment: Advance, then return self.
HistoryBufIterator & Self &
operator++(); operator++();
//! Post-increment: return a copy of initial state after incrementing self //! Post-increment: return a copy of initial state after incrementing self
HistoryBufIterator Self
operator++(int); operator++(int);
//! Pre-decrement: Subtract, then return self. //! Pre-decrement: Subtract, then return self.
HistoryBufIterator & Self &
operator--(); operator--();
//! Post-decrement: return a copy of initial state after decrementing self //! Post-decrement: return a copy of initial state after decrementing self
HistoryBufIterator Self
operator--(int); operator--(int);
// Use the base class implementation of subtracting one iterator from another // Use the base class implementation of subtracting one iterator from another
using base::operator-; using base::operator-;
//! Increment by an arbitrary amount. //! Increment by an arbitrary amount.
HistoryBufIterator & Self &
operator+=(std::ptrdiff_t n) noexcept; operator+=(std::ptrdiff_t n) noexcept;
//! Decrement by an arbitrary amount. //! Decrement by an arbitrary amount.
HistoryBufIterator & Self &
operator-=(std::ptrdiff_t n) noexcept; operator-=(std::ptrdiff_t n) noexcept;
//! Increment a copy of the iterator by an arbitrary amount. //! Increment a copy of the iterator by an arbitrary amount.
HistoryBufIterator Self
operator+(std::ptrdiff_t n) const noexcept; operator+(std::ptrdiff_t n) const noexcept;
//! Decrement a copy of the iterator by an arbitrary amount. //! Decrement a copy of the iterator by an arbitrary amount.
HistoryBufIterator Self
operator-(std::ptrdiff_t n) const noexcept; operator-(std::ptrdiff_t n) const noexcept;
private: private:
//! Factory for a "begin" iterator from a container and its helper: mostly for internal use. //! Factory for a "begin" iterator from a container and its helper: mostly for internal use.
static HistoryBufIterator static Self
begin(container_type &container, const RingBufferHelper<MaxSize> &helper) begin(container_type &container, const RingBufferHelper<MaxSize> &helper)
{ {
return HistoryBufIterator{&container, std::move(base::begin(helper))}; return {&container, std::move(base::begin(helper))};
} }
//! Construct the "past the end" iterator that can be decremented safely //! Construct the "past the end" iterator that can be decremented safely
static HistoryBufIterator static Self
end(container_type &container, const RingBufferHelper<MaxSize> &helper) end(container_type &container, const RingBufferHelper<MaxSize> &helper)
{ {
return HistoryBufIterator{&container, std::move(base::end(helper))}; return {&container, std::move(base::end(helper))};
} }
// for use internally // for use internally
@ -178,7 +179,7 @@ namespace detail {
inline HistoryBufIterator<T, MaxSize> inline HistoryBufIterator<T, MaxSize>
HistoryBufIterator<T, MaxSize>::operator--(int) HistoryBufIterator<T, MaxSize>::operator--(int)
{ {
HistoryBufIterator tmp = *this; Self tmp = *this;
this->decrement_n(1); this->decrement_n(1);
return tmp; return tmp;
} }
@ -203,7 +204,7 @@ namespace detail {
inline HistoryBufIterator<T, MaxSize> inline HistoryBufIterator<T, MaxSize>
HistoryBufIterator<T, MaxSize>::operator+(std::ptrdiff_t n) const noexcept HistoryBufIterator<T, MaxSize>::operator+(std::ptrdiff_t n) const noexcept
{ {
HistoryBufIterator ret(*this); Self ret(*this);
ret += n; ret += n;
return ret; return ret;
} }
@ -212,7 +213,7 @@ namespace detail {
inline HistoryBufIterator<T, MaxSize> inline HistoryBufIterator<T, MaxSize>
HistoryBufIterator<T, MaxSize>::operator-(std::ptrdiff_t n) const noexcept HistoryBufIterator<T, MaxSize>::operator-(std::ptrdiff_t n) const noexcept
{ {
HistoryBufIterator ret(*this); Self ret(*this);
ret -= n; ret -= n;
return ret; return ret;
} }
@ -224,6 +225,7 @@ namespace detail {
{} {}
} // namespace detail } // namespace detail
// HistoryBuffer method implementations that depend on iterator availability
template <typename T, size_t MaxSize> template <typename T, size_t MaxSize>
inline typename HistoryBuffer<T, MaxSize>::iterator inline typename HistoryBuffer<T, MaxSize>::iterator