//////////////////////////////////////////////////////////////////////////////// /// \file proxy_iterator.hpp /// /// \brief This internal header provides the definition of an iterator wrapper /// type used for uniqueness //////////////////////////////////////////////////////////////////////////////// /* The MIT License (MIT) Copyright (c) 2020 Matthew Rodusek All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef BPSTD_DETAIL_PROXY_ITERATOR_HPP #define BPSTD_DETAIL_PROXY_ITERATOR_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "config.hpp" #include BPSTD_COMPILER_DIAGNOSTIC_PREAMBLE namespace bpstd { namespace detail { //========================================================================== // class : proxy_iterator //========================================================================== /////////////////////////////////////////////////////////////////////////// /// \brief A thin wrapper around a different iterator type to add /// uniqueness /// /// This makes any regular Iterator type such as a pointer to be unique, /// and incomparable to other pointers. /// /// \tparam Iterator the pointer type to wrap /// \tparam U a type to make this iterator unique, and not comparable with /// other proxy_iterators /////////////////////////////////////////////////////////////////////////// template class proxy_iterator #if __cplusplus < 201703L : public std::iterator< typename std::iterator_traits::iterator_category, typename std::iterator_traits::value_type, typename std::iterator_traits::difference_type, typename std::iterator_traits::pointer, typename std::iterator_traits::reference > #endif { #if __cplusplus < 201703L using base_type = std::iterator< typename std::iterator_traits::iterator_category, typename std::iterator_traits::value_type, typename std::iterator_traits::difference_type, typename std::iterator_traits::pointer, typename std::iterator_traits::reference >; #endif //------------------------------------------------------------------------ // Public Member Types //------------------------------------------------------------------------ public: #if __cplusplus >= 201703L using iterator_category = typename std::iterator_traits::iterator_category; using value_type = typename std::iterator_traits::value_type; using pointer = typename std::iterator_traits::pointer; using reference = typename std::iterator_traits::reference; using difference_type = typename std::iterator_traits::difference_type; #else using iterator_category = typename base_type::iterator_category; using value_type = typename base_type::value_type; using pointer = typename base_type::pointer; using reference = typename base_type::reference; using difference_type = typename base_type::difference_type; #endif //------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------ public: /// \brief Default constructs this proxy_iterator by default-constructing /// the underlying iterator constexpr proxy_iterator() = default; /// \brief Constructs a proxy_iterator from a given pointer /// /// \param it the iterator to construct this proxy_iterator constexpr explicit proxy_iterator(const Iterator& it) noexcept; /// \brief Convert-constructs a proxy_iterator from a proxy iterator with /// the same tag /// /// \param it the iterator template constexpr proxy_iterator(const proxy_iterator& it) noexcept; /// \brief Copy-constructs a proxy_iterator /// /// \param other the iterator to copy constexpr proxy_iterator(const proxy_iterator& other) noexcept = default; /// \brief Move-constructs a proxy_iterator /// /// \param other the iterator to move constexpr proxy_iterator(proxy_iterator&& other) noexcept = default; /// \brief Copy-assigns a proxy_iterator /// /// \param it the iterator to copy template proxy_iterator& operator=(const proxy_iterator& it) noexcept; /// \brief Copy-assigns a proxy_iterator /// /// \param other the iterator to copy proxy_iterator& operator=(const proxy_iterator& other) noexcept = default; /// \brief Move-assigns a proxy_iterator /// /// \param other the iterator to move proxy_iterator& operator=(proxy_iterator&& other) noexcept = default; //------------------------------------------------------------------------ // Iteration //------------------------------------------------------------------------ public: BPSTD_CPP14_CONSTEXPR proxy_iterator& operator++() noexcept; BPSTD_CPP14_CONSTEXPR proxy_iterator operator++(int) noexcept; BPSTD_CPP14_CONSTEXPR proxy_iterator& operator--() noexcept; BPSTD_CPP14_CONSTEXPR proxy_iterator operator--(int) noexcept; //------------------------------------------------------------------------ // Random Access //------------------------------------------------------------------------ public: BPSTD_CPP14_CONSTEXPR proxy_iterator& operator+=(difference_type n) noexcept; BPSTD_CPP14_CONSTEXPR proxy_iterator& operator-=(difference_type n) noexcept; constexpr difference_type operator-(const proxy_iterator& rhs) const noexcept; constexpr reference operator[](difference_type index) const noexcept; //------------------------------------------------------------------------ // Observers //------------------------------------------------------------------------ public: constexpr pointer operator->() const noexcept; constexpr reference operator*() const noexcept; //------------------------------------------------------------------------ // Comparison //------------------------------------------------------------------------ public: constexpr bool operator==(const proxy_iterator& rhs) const noexcept; constexpr bool operator!=(const proxy_iterator& rhs) const noexcept; constexpr bool operator< (const proxy_iterator& rhs) const noexcept; constexpr bool operator<=(const proxy_iterator& rhs) const noexcept; constexpr bool operator> (const proxy_iterator& rhs) const noexcept; constexpr bool operator>=(const proxy_iterator& rhs) const noexcept; //------------------------------------------------------------------------ // Private Members //------------------------------------------------------------------------ private: Iterator m_iter; ///< The iterator used for iteration template friend class proxy_iterator; }; //========================================================================== // non-member functions : class : proxy_iterator //========================================================================== //-------------------------------------------------------------------------- // Random Access //-------------------------------------------------------------------------- template BPSTD_CPP14_CONSTEXPR proxy_iterator operator+(const proxy_iterator& lhs, typename proxy_iterator::difference_type rhs) noexcept; template BPSTD_CPP14_CONSTEXPR proxy_iterator operator+(typename proxy_iterator::difference_type lhs, const proxy_iterator& rhs) noexcept; template BPSTD_CPP14_CONSTEXPR proxy_iterator operator-(const proxy_iterator& lhs, typename proxy_iterator::difference_type rhs) noexcept; } // namespace detail } // namespace bpstd //============================================================================== // definitions : class : proxy_iterator //============================================================================== //------------------------------------------------------------------------------ // Constructors / Assignment //------------------------------------------------------------------------------ template inline constexpr bpstd::detail::proxy_iterator:: proxy_iterator(const Iterator& it) noexcept : m_iter{it} { } template template inline constexpr bpstd::detail::proxy_iterator ::proxy_iterator(const proxy_iterator& it) noexcept : m_iter{it.m_iter} { } template template inline bpstd::detail::proxy_iterator& bpstd::detail::proxy_iterator::operator=(const proxy_iterator& it) noexcept { m_iter = it.m_iter; return (*this); } //------------------------------------------------------------------------------ // Iteration //------------------------------------------------------------------------------ template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator& bpstd::detail::proxy_iterator::operator++() noexcept { ++m_iter; return (*this); } template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator bpstd::detail::proxy_iterator::operator++(int) noexcept { return proxy_iterator{m_iter++}; } //------------------------------------------------------------------------------ template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator& bpstd::detail::proxy_iterator::operator--() noexcept { --m_iter; return (*this); } template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator bpstd::detail::proxy_iterator::operator--(int) noexcept { return proxy_iterator{m_iter--}; } //------------------------------------------------------------------------------ // Random Access //------------------------------------------------------------------------------ template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator& bpstd::detail::proxy_iterator::operator+=(difference_type n) noexcept { m_iter += n; return (*this); } template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator& bpstd::detail::proxy_iterator::operator-=(difference_type n) noexcept { m_iter -= n; return (*this); } template inline constexpr typename bpstd::detail::proxy_iterator::difference_type bpstd::detail::proxy_iterator::operator-(const proxy_iterator& rhs) const noexcept { return m_iter - rhs.m_iter; } template inline constexpr typename bpstd::detail::proxy_iterator::reference bpstd::detail::proxy_iterator::operator[](difference_type index) const noexcept { return m_iter[index]; } //------------------------------------------------------------------------------ // Observers //------------------------------------------------------------------------------ template inline constexpr typename bpstd::detail::proxy_iterator::pointer bpstd::detail::proxy_iterator::operator->() const noexcept { return m_iter; } template inline constexpr typename bpstd::detail::proxy_iterator::reference bpstd::detail::proxy_iterator::operator*() const noexcept { return *m_iter; } //------------------------------------------------------------------------------ // Comparisons //------------------------------------------------------------------------------ template inline constexpr bool bpstd::detail::proxy_iterator::operator==(const proxy_iterator& rhs) const noexcept { return m_iter == rhs.m_iter; } template inline constexpr bool bpstd::detail::proxy_iterator::operator!=(const proxy_iterator& rhs) const noexcept { return m_iter != rhs.m_iter; } template inline constexpr bool bpstd::detail::proxy_iterator::operator<(const proxy_iterator& rhs) const noexcept { return m_iter < rhs.m_iter; } template inline constexpr bool bpstd::detail::proxy_iterator::operator<=(const proxy_iterator& rhs) const noexcept { return m_iter <= rhs.m_iter; } template inline constexpr bool bpstd::detail::proxy_iterator::operator>(const proxy_iterator& rhs) const noexcept { return m_iter > rhs.m_iter; } template inline constexpr bool bpstd::detail::proxy_iterator::operator>=(const proxy_iterator& rhs) const noexcept { return m_iter >= rhs.m_iter; } //============================================================================== // definitions : non-member functions : class : proxy_iterator //============================================================================== //------------------------------------------------------------------------------ // Random Access (free functions) //------------------------------------------------------------------------------ template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator bpstd::detail::operator+(const proxy_iterator& lhs, typename proxy_iterator::difference_type rhs) noexcept { return proxy_iterator{lhs} += rhs; } template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator bpstd::detail::operator+(typename proxy_iterator::difference_type lhs, const proxy_iterator& rhs) noexcept { return proxy_iterator{rhs} += lhs; } template inline BPSTD_CPP14_CONSTEXPR bpstd::detail::proxy_iterator bpstd::detail::operator-(const proxy_iterator& lhs, typename proxy_iterator::difference_type rhs) noexcept { return proxy_iterator{lhs} -= rhs; } BPSTD_COMPILER_DIAGNOSTIC_POSTAMBLE #endif /* BPSTD_DETAIL_PROXY_ITERATOR_HPP */