Skip to content

Commit

Permalink
Make container iterator operations defend against expected<any, T>
Browse files Browse the repository at this point in the history
  • Loading branch information
frederick-vs-ja committed Jul 24, 2024
1 parent 6c32079 commit 92da7d0
Show file tree
Hide file tree
Showing 10 changed files with 1,046 additions and 1 deletion.
77 changes: 77 additions & 0 deletions stl/inc/array
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ public:
return _Tmp;
}

_NODISCARD _CONSTEXPR17 ptrdiff_t operator-(const _Array_iterator& _Right) const noexcept {
return static_cast<const _Mybase&>(*this) - static_cast<const _Mybase&>(_Right);
}
_NODISCARD friend _CONSTEXPR17 ptrdiff_t operator-(const _Mybase& _Left, const _Array_iterator& _Right) noexcept {
return _Left - static_cast<const _Mybase&>(_Right);
}

_NODISCARD _CONSTEXPR17 reference operator[](const ptrdiff_t _Off) const noexcept {
return const_cast<reference>(_Mybase::operator[](_Off));
}
Expand All @@ -387,6 +394,76 @@ public:
_NODISCARD constexpr pointer _Unwrapped() const noexcept {
return const_cast<pointer>(_Mybase::_Unwrapped());
}

_NODISCARD _CONSTEXPR17 bool operator==(const _Array_iterator& _Right) const noexcept {
return static_cast<const _Mybase&>(*this) == static_cast<const _Mybase&>(_Right);
}
_NODISCARD _CONSTEXPR17 bool operator==(const _Mybase& _Right) const noexcept {
return static_cast<const _Mybase&>(*this) == _Right;
}

#if _HAS_CXX20
_NODISCARD constexpr strong_ordering operator<=>(const _Array_iterator& _Right) const noexcept {
return static_cast<const _Mybase&>(*this) <=> static_cast<const _Mybase&>(_Right);
}
_NODISCARD constexpr strong_ordering operator<=>(const _Mybase& _Right) const noexcept {
return static_cast<const _Mybase&>(*this) <=> _Right;
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
_NODISCARD friend _CONSTEXPR17 bool operator==(const _Mybase& _Left, const _Array_iterator& _Right) noexcept {
return _Left == static_cast<const _Mybase&>(_Right);
}

_NODISCARD _CONSTEXPR17 bool operator<(const _Array_iterator& _Right) const noexcept {
return static_cast<const _Mybase&>(*this) < static_cast<const _Mybase&>(_Right);
}
_NODISCARD _CONSTEXPR17 bool operator<(const _Mybase& _Right) const noexcept {
return static_cast<const _Mybase&>(*this) < _Right;
}
_NODISCARD friend _CONSTEXPR17 bool operator<(const _Mybase& _Left, const _Array_iterator& _Right) noexcept {
return _Left < static_cast<const _Mybase&>(_Right);
}

_NODISCARD _CONSTEXPR17 bool operator!=(const _Array_iterator& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD _CONSTEXPR17 bool operator!=(const _Mybase& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD friend _CONSTEXPR17 bool operator!=(const _Mybase& _Left, const _Array_iterator& _Right) noexcept {
return !(_Left == _Right);
}

_NODISCARD _CONSTEXPR17 bool operator>(const _Array_iterator& _Right) const noexcept {
return _Right < *this;
}
_NODISCARD _CONSTEXPR17 bool operator>(const _Mybase& _Right) const noexcept {
return _Right < *this;
}
_NODISCARD friend _CONSTEXPR17 bool operator>(const _Mybase& _Left, const _Array_iterator& _Right) noexcept {
return _Right < _Left;
}

_NODISCARD _CONSTEXPR17 bool operator<=(const _Array_iterator& _Right) const noexcept {
return !(_Right < *this);
}
_NODISCARD _CONSTEXPR17 bool operator<=(const _Mybase& _Right) const noexcept {
return !(_Right < *this);
}
_NODISCARD friend _CONSTEXPR17 bool operator<=(const _Mybase& _Left, const _Array_iterator& _Right) noexcept {
return !(_Right < _Left);
}

_NODISCARD _CONSTEXPR17 bool operator>=(const _Array_iterator& _Right) const noexcept {
return !(*this < _Right);
}
_NODISCARD _CONSTEXPR17 bool operator>=(const _Mybase& _Right) const noexcept {
return !(*this < _Right);
}
_NODISCARD friend _CONSTEXPR17 bool operator>=(const _Mybase& _Left, const _Array_iterator& _Right) noexcept {
return !(_Left < _Right);
}
#endif // ^^^ !_HAS_CXX20 ^^^
};

#if _HAS_CXX20
Expand Down
166 changes: 165 additions & 1 deletion stl/inc/deque
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,87 @@ public:
_NODISCARD difference_type operator-(const _Mybase& _Right) const noexcept {
return _Mybase::operator-(_Right);
}
_NODISCARD difference_type operator-(const _Deque_unchecked_iterator& _Right) const noexcept {
return _Mybase::operator-(_Right);
}
_NODISCARD friend difference_type operator-(
const _Mybase& _Left, const _Deque_unchecked_iterator& _Right) noexcept {
return _Left.operator-(_Right);
}

_NODISCARD reference operator[](const difference_type _Off) const noexcept {
return const_cast<reference>(_Mybase::operator[](_Off));
}

_NODISCARD bool operator==(const _Deque_unchecked_iterator& _Right) const noexcept {
return this->_Myoff == _Right._Myoff;
}
_NODISCARD bool operator==(const _Mybase& _Right) const noexcept {
return this->_Myoff == _Right._Myoff;
}

#if _HAS_CXX20
_NODISCARD strong_ordering operator<=>(const _Deque_unchecked_iterator& _Right) const noexcept {
return this->_Myoff <=> _Right._Myoff;
}
_NODISCARD strong_ordering operator<=>(const _Mybase& _Right) const noexcept {
return this->_Myoff <=> _Right._Myoff;
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
_NODISCARD friend bool operator==(const _Mybase& _Left, const _Deque_unchecked_iterator& _Right) noexcept {
return _Left._Myoff == _Right._Myoff;
}

_NODISCARD bool operator!=(const _Deque_unchecked_iterator& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD bool operator!=(const _Mybase& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD friend bool operator!=(const _Mybase& _Left, const _Deque_unchecked_iterator& _Right) noexcept {
return !(_Left == _Right);
}

_NODISCARD bool operator<(const _Deque_unchecked_iterator& _Right) const noexcept {
return this->_Myoff < _Right._Myoff;
}
_NODISCARD bool operator<(const _Mybase& _Right) const noexcept {
return this->_Myoff < _Right._Myoff;
}
_NODISCARD friend bool operator<(const _Mybase& _Left, const _Deque_unchecked_iterator& _Right) noexcept {
return _Left._Myoff < _Right._Myoff;
}

_NODISCARD bool operator>(const _Deque_unchecked_iterator& _Right) const noexcept {
return _Right < *this;
}
_NODISCARD bool operator>(const _Mybase& _Right) const noexcept {
return _Right < *this;
}
_NODISCARD friend bool operator>(const _Mybase& _Left, const _Deque_unchecked_iterator& _Right) noexcept {
return _Right < _Left;
}

_NODISCARD bool operator<=(const _Deque_unchecked_iterator& _Right) const noexcept {
return !(_Right < *this);
}
_NODISCARD bool operator<=(const _Mybase& _Right) const noexcept {
return !(_Right < *this);
}
_NODISCARD friend bool operator<=(const _Mybase& _Left, const _Deque_unchecked_iterator& _Right) noexcept {
return !(_Right < _Left);
}

_NODISCARD bool operator>=(const _Deque_unchecked_iterator& _Right) const noexcept {
return !(*this < _Right);
}
_NODISCARD bool operator>=(const _Mybase& _Right) const noexcept {
return !(*this < _Right);
}
_NODISCARD friend bool operator>=(const _Mybase& _Left, const _Deque_unchecked_iterator& _Right) noexcept {
return !(_Left < _Right);
}
#endif // ^^^ !_HAS_CXX20 ^^^
};

template <class _Mydeque>
Expand Down Expand Up @@ -338,7 +415,6 @@ public:
_Tmp -= _Off;
return _Tmp;
}

_NODISCARD difference_type operator-(const _Deque_const_iterator& _Right) const noexcept {
_Compat(_Right);
return static_cast<difference_type>(this->_Myoff - _Right._Myoff);
Expand Down Expand Up @@ -496,6 +572,16 @@ public:

using _Mybase::operator-;

_NODISCARD difference_type operator-(const _Deque_iterator& _Right) const noexcept {
_Mybase::_Compat(_Right);
return static_cast<difference_type>(this->_Myoff - _Right._Myoff);
}

_NODISCARD friend difference_type operator-(const _Mybase& _Left, const _Deque_iterator& _Right) noexcept {
_Left._Compat(_Right);
return static_cast<difference_type>(_Left._Myoff - _Right._Myoff);
}

_NODISCARD _Deque_iterator operator-(const difference_type _Off) const noexcept {
_Deque_iterator _Tmp = *this;
_Tmp -= _Off;
Expand All @@ -506,6 +592,84 @@ public:
return const_cast<reference>(_Mybase::operator[](_Off));
}

_NODISCARD bool operator==(const _Deque_iterator& _Right) const noexcept {
_Mybase::_Compat(_Right);
return this->_Myoff == _Right._Myoff;
}
_NODISCARD bool operator==(const _Mybase& _Right) const noexcept {
_Mybase::_Compat(_Right);
return this->_Myoff == _Right._Myoff;
}

#if _HAS_CXX20
_NODISCARD strong_ordering operator<=>(const _Deque_iterator& _Right) const noexcept {
_Mybase::_Compat(_Right);
return this->_Myoff <=> _Right._Myoff;
}
_NODISCARD strong_ordering operator<=>(const _Mybase& _Right) const noexcept {
_Mybase::_Compat(_Right);
return this->_Myoff <=> _Right._Myoff;
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
_NODISCARD friend bool operator==(const _Mybase& _Left, const _Deque_iterator& _Right) noexcept {
_Left._Compat(_Right);
return _Left._Myoff == _Right._Myoff;
}

_NODISCARD bool operator!=(const _Deque_iterator& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD bool operator!=(const _Mybase& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD friend bool operator!=(const _Mybase& _Left, const _Deque_iterator& _Right) noexcept {
return !(_Left == _Right);
}

_NODISCARD bool operator<(const _Deque_iterator& _Right) const noexcept {
_Mybase::_Compat(_Right);
return this->_Myoff < _Right._Myoff;
}
_NODISCARD bool operator<(const _Mybase& _Right) const noexcept {
_Mybase::_Compat(_Right);
return this->_Myoff < _Right._Myoff;
}
_NODISCARD friend bool operator<(const _Mybase& _Left, const _Deque_iterator& _Right) noexcept {
_Left._Compat(_Right);
return _Left._Myoff < _Right._Myoff;
}

_NODISCARD bool operator>(const _Deque_iterator& _Right) const noexcept {
return _Right < *this;
}
_NODISCARD bool operator>(const _Mybase& _Right) const noexcept {
return _Right < *this;
}
_NODISCARD friend bool operator>(const _Mybase& _Left, const _Deque_iterator& _Right) noexcept {
return _Right < _Left;
}

_NODISCARD bool operator<=(const _Deque_iterator& _Right) const noexcept {
return !(_Right < *this);
}
_NODISCARD bool operator<=(const _Mybase& _Right) const noexcept {
return !(_Right < *this);
}
_NODISCARD friend bool operator<=(const _Mybase& _Left, const _Deque_iterator& _Right) noexcept {
return !(_Right < _Left);
}

_NODISCARD bool operator>=(const _Deque_iterator& _Right) const noexcept {
return !(*this < _Right);
}
_NODISCARD bool operator>=(const _Mybase& _Right) const noexcept {
return !(*this < _Right);
}
_NODISCARD friend bool operator>=(const _Mybase& _Left, const _Deque_iterator& _Right) noexcept {
return !(_Left < _Right);
}
#endif // ^^^ !_HAS_CXX20 ^^^

using _Prevent_inheriting_unwrap = _Deque_iterator;

_NODISCARD _Deque_unchecked_iterator<_Mydeque> _Unwrapped() const noexcept {
Expand Down
64 changes: 64 additions & 0 deletions stl/inc/forward_list
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,35 @@ public:
_Mybase::operator++();
return _Tmp;
}

_NODISCARD bool operator==(const _Flist_unchecked_iterator& _Right) const noexcept {
return this->_Ptr == _Right._Ptr;
}
_NODISCARD bool operator==(const _Mybase& _Right) const noexcept {
return this->_Ptr == _Right._Ptr;
}
_NODISCARD bool operator==(_Default_sentinel) const noexcept {
return this->_Ptr == nullptr;
}

#if !_HAS_CXX20
_NODISCARD friend bool operator==(const _Mybase& _Left, const _Flist_unchecked_iterator& _Right) noexcept {
return _Left._Ptr == _Right._Ptr;
}

_NODISCARD bool operator!=(const _Flist_unchecked_iterator& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD bool operator!=(const _Mybase& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD friend bool operator!=(const _Mybase& _Left, const _Flist_unchecked_iterator& _Right) noexcept {
return !(_Left == _Right);
}
_NODISCARD bool operator!=(_Default_sentinel) const noexcept {
return this->_Ptr != nullptr;
}
#endif // !_HAS_CXX20
};

template <class _Mylist>
Expand Down Expand Up @@ -218,6 +247,41 @@ public:
return _Tmp;
}

_NODISCARD bool operator==(const _Flist_iterator& _Right) const noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(this->_Getcont() == _Right._Getcont(), "forward_list iterators incompatible");
#endif // _ITERATOR_DEBUG_LEVEL == 2

return this->_Ptr == _Right._Ptr;
}
_NODISCARD bool operator==(const _Mybase& _Right) const noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(this->_Getcont() == _Right._Getcont(), "forward_list iterators incompatible");
#endif // _ITERATOR_DEBUG_LEVEL == 2

return this->_Ptr == _Right._Ptr;
}

#if !_HAS_CXX20
_NODISCARD friend bool operator==(const _Mybase& _Left, const _Flist_iterator& _Right) noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(_Left._Getcont() == _Right._Getcont(), "forward_list iterators incompatible");
#endif // _ITERATOR_DEBUG_LEVEL == 2

return _Left._Ptr == _Right._Ptr;
}

_NODISCARD bool operator!=(const _Flist_iterator& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD bool operator!=(const _Mybase& _Right) const noexcept {
return !(*this == _Right);
}
_NODISCARD friend bool operator!=(const _Mybase& _Left, const _Flist_iterator& _Right) noexcept {
return !(_Left == _Right);
}
#endif // !_HAS_CXX20

using _Prevent_inheriting_unwrap = _Flist_iterator;

_NODISCARD _Flist_unchecked_iterator<_Mylist> _Unwrapped() const noexcept {
Expand Down
Loading

0 comments on commit 92da7d0

Please sign in to comment.