Skip to content

Commit

Permalink
Updating ...
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Nov 7, 2023
2 parents 9ec048b + d6e0893 commit f0076ae
Showing 1 changed file with 28 additions and 100 deletions.
128 changes: 28 additions & 100 deletions src/tools/ranges/ViewBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,55 +40,60 @@ class ViewBase {
/**
* @brief Return whether or not the view is empty
*/
template < typename Range = Derived,
concepts::Requires< true, concepts::IsForwardIterator,
typename Range::iterator > = true >
constexpr bool empty() const noexcept {

static_assert(
concepts::IsForwardIterator< typename Derived::iterator >::value == true,
"the empty() method can only be made available for forward iterators" );

return this->derived().begin() == this->derived().end();
}

/**
* @brief Return whether or not the view is empty
*/
template < typename Range = Derived,
concepts::Requires< true, concepts::IsForwardIterator,
typename Range::iterator > = true >
constexpr explicit operator bool() const noexcept {

static_assert(
concepts::IsForwardIterator< typename Derived::iterator >::value == true,
"the operator bool() method can only be made available for forward iterators" );

return this->empty();
}

/**
* @brief Return the size of the iterator view
*/
template < typename Range = Derived,
concepts::Requires< true, concepts::IsForwardIterator,
typename Range::iterator > = true >
constexpr std::size_t size() const noexcept {

static_assert(
concepts::IsForwardIterator< typename Derived::iterator >::value == true,
"the size() method can only be made available for forward iterators" );

return std::distance( this->derived().begin(), this->derived().end() );
}

/**
* @brief Return the reference to the front element of the view
*/
template < typename Range = Derived,
concepts::Requires< true, concepts::IsForwardIterator,
typename Range::iterator > = true >
constexpr decltype(auto) front() const noexcept {

static_assert(
concepts::IsForwardIterator< typename Derived::iterator >::value == true,
"the front() method can only be made available for forward iterators" );

return *( this->derived().begin() );
}

/**
* @brief Return the reference to the back element of the view
*/
template < typename Range = Derived,
concepts::Requires< true, concepts::IsBidirectionalIterator,
typename Range::iterator > = true >
constexpr decltype(auto) back() const noexcept {

static_assert(
concepts::IsBidirectionalIterator< typename Derived::iterator >::value == true,
"the back() method can only be made available for bidirectional iterators" );

return *( std::prev( this->derived().end() ) );
}

Expand All @@ -99,11 +104,12 @@ class ViewBase {
*
* @param[in] i the index
*/
template < typename Range = Derived,
concepts::Requires< true, concepts::IsRandomAccessIterator,
typename Range::iterator > = true >
constexpr decltype(auto) operator[]( std::size_t i ) const noexcept {

static_assert(
concepts::IsRandomAccessIterator< typename Derived::iterator >::value == true,
"the operator[] method can only be made available for random access iterators" );

return *( std::next( this->derived().begin(), i ) );
}

Expand All @@ -112,11 +118,12 @@ class ViewBase {
*
* @param[in] i the index
*/
template < typename Range = Derived,
concepts::Requires< true, concepts::IsRandomAccessIterator,
typename Range::iterator > = true >
constexpr decltype(auto) at( std::size_t i ) const {

static_assert(
concepts::IsRandomAccessIterator< typename Derived::iterator >::value == true,
"the at() method can only be made available for random access iterators" );

if ( i >= this->size() ) {

throw std::out_of_range( "Index out of range: " +
Expand Down Expand Up @@ -154,85 +161,6 @@ class ViewBase {

return !this->operator==( other );
}

// the following code is added to make compiler errors more comprehensible

template < typename Range = Derived,
concepts::Requires< false, concepts::IsForwardIterator,
typename Range::iterator > = true >
constexpr bool empty() const noexcept {

static_assert(
concepts::IsForwardIterator< typename Range::iterator >::value == true,
"the empty() method can only be made available for forward iterators" );
return true;
}

template < typename Range = Derived,
concepts::Requires< false, concepts::IsForwardIterator,
typename Range::iterator > = true >
constexpr explicit operator bool() const noexcept {

static_assert(
concepts::IsForwardIterator< typename Range::iterator >::value == true,
"the operator bool() method can only be made available for forward iterators" );
return true;
}

template < typename Range = Derived,
concepts::Requires< false, concepts::IsForwardIterator,
typename Range::iterator > = true >
constexpr std::size_t size() const noexcept {

static_assert(
concepts::IsForwardIterator< typename Range::iterator >::value == true,
"the size() method can only be made available for forward iterators" );
return std::size_t{};
}

template < typename Range = Derived,
concepts::Requires< false, concepts::IsForwardIterator,
typename Range::iterator > = true >
constexpr decltype(auto) front() const noexcept {

static_assert(
concepts::IsBidirectionalIterator< typename Range::iterator >::value == true,
"the front() method can only be made available for bidirectional iterators" );
return typename Derived::value_type{};
}

template < typename Range = Derived,
concepts::Requires< false, concepts::IsBidirectionalIterator,
typename Range::iterator > = true >
constexpr decltype(auto) back() const noexcept {

static_assert(
concepts::IsBidirectionalIterator< typename Range::iterator >::value == true,
"the back() method can only be made available for bidirectional iterators" );
return typename Derived::value_type{};
}

template < typename Range = Derived,
concepts::Requires< false, concepts::IsRandomAccessIterator,
typename Range::iterator > = true >
constexpr decltype(auto) operator[]( std::size_t i ) const {

static_assert(
concepts::IsRandomAccessIterator< typename Range::iterator >::value == true,
"the operator[] method can only be made available for random access iterators" );
return typename Derived::value_type{};
}

template < typename Range = Derived,
concepts::Requires< false, concepts::IsRandomAccessIterator,
typename Range::iterator > = true >
constexpr decltype(auto) at( std::size_t i ) const {

static_assert(
concepts::IsRandomAccessIterator< typename Range::iterator >::value == true,
"the at() method can only be made available for random access iterators" );
return typename Derived::value_type{};
}
};

} // ranges namespace
Expand Down

0 comments on commit f0076ae

Please sign in to comment.