Skip to content

Commit

Permalink
Improve relational operators on ArrayWrapper1D.
Browse files Browse the repository at this point in the history
  • Loading branch information
asuessenbach committed Jun 5, 2024
1 parent a64aeb6 commit 920012f
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 165 deletions.
105 changes: 56 additions & 49 deletions snippets/ArrayWrapper1D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,66 +57,72 @@ class ArrayWrapper1D : public std::array<T, N>
}
#endif

#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
std::strong_ordering operator<=>( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) <=> *static_cast<std::array<char, N> const *>( &rhs );
}
#else
template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator<( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) < *static_cast<std::array<char, N> const *>( &rhs );
}

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator<=( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) <= *static_cast<std::array<char, N> const *>( &rhs );
}

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator>( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) > *static_cast<std::array<char, N> const *>( &rhs );
}

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator>=( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) >= *static_cast<std::array<char, N> const *>( &rhs );
}
#endif

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator==( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) == *static_cast<std::array<char, N> const *>( &rhs );
}

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator!=( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) != *static_cast<std::array<char, N> const *>( &rhs );
}

private:
VULKAN_HPP_CONSTEXPR_14 void copy( char const * data, size_t len ) VULKAN_HPP_NOEXCEPT
{
size_t n = std::min( N, len );
size_t n = std::min( N - 1, len );
for ( size_t i = 0; i < n; ++i )
{
( *this )[i] = data[i];
}
for ( size_t i = n; i < N; ++i )
{
( *this )[i] = 0;
}
( *this )[n] = 0;
}
};

// relational operators between ArrayWrapper1D of chars with potentially different sizes
#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
template <size_t N, size_t M>
std::strong_ordering operator<=>( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
int result = strcmp( lhs.data(), rhs.data() );
return ( result < 0 ) ? std::strong_ordering::less : ( ( result > 0 ) ? std::strong_ordering::greater : std::strong_ordering::equal );
}
#else
template <size_t N, size_t M>
bool operator<( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) < 0;
}

template <size_t N, size_t M>
bool operator<=( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) <= 0;
}

template <size_t N, size_t M>
bool operator>( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) > 0;
}

template <size_t N, size_t M>
bool operator>=( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) >= 0;
}
#endif

template <size_t N, size_t M>
bool operator==( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) == 0;
}

template <size_t N, size_t M>
bool operator!=( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) != 0;
}

// specialization of relational operators between std::string and arrays of chars
#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
template <size_t N>
std::strong_ordering operator<=>( std::string const & lhs, ArrayWrapper1D<char, N> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return lhs <=> rhs.data();
}
#else
template <size_t N>
bool operator<( std::string const & lhs, ArrayWrapper1D<char, N> const & rhs ) VULKAN_HPP_NOEXCEPT
{
Expand All @@ -140,6 +146,7 @@ bool operator>=( std::string const & lhs, ArrayWrapper1D<char, N> const & rhs )
{
return lhs >= rhs.data();
}
#endif

template <size_t N>
bool operator==( std::string const & lhs, ArrayWrapper1D<char, N> const & rhs ) VULKAN_HPP_NOEXCEPT
Expand Down
37 changes: 21 additions & 16 deletions tests/ArrayWrapper/ArrayWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,44 @@ void f( std::string const & s )

int main( int /*argc*/, char ** /*argv*/ )
{
vk::ArrayWrapper1D<char, 10> aw1( { 'f', 'o', 'o', 'b', 'a', 'h' } );
std::string s1 = aw1;
vk::ArrayWrapper1D<char, 10> awc1( { 'f', 'o', 'o', 'b', 'a', 'h' } );
std::string s1 = awc1;
assert( s1.length() == 6 );
std::cout << "<" << aw1 << ">" << std::endl;
std::cout << "<" << awc1 << ">" << std::endl;

// s1 = aw1; // 'operator =' is ambiguous
// s1 = awc1; // 'operator =' is ambiguous

std::string foobah = "foobah";

vk::ArrayWrapper1D<char, 20> aw2( foobah );
f( aw2 );
vk::ArrayWrapper1D<char, 20> awc2( foobah );
f( awc2 );

vk::ArrayWrapper1D<char, 5> aw3( { 'f', 'o', 'o', 'b', 'a', 'h' } );
std::string s3 = aw3;
assert( s3.length() == 5 );
vk::ArrayWrapper1D<char, 5> awc3( { 'f', 'o', 'o', 'b', 'a', 'h' } );
std::string s3 = awc3;
assert( s3.length() == 4 );
std::cout << "<" << s3 << ">" << std::endl;

vk::ArrayWrapper1D<char, 5> aw4( foobah );
std::string s4 = aw4;
assert( s4.length() == 5 );
vk::ArrayWrapper1D<char, 5> awc4( foobah );
std::string s4 = awc4;
assert( s4.length() == 4 );

#if 17 <= VULKAN_HPP_CPP_VERSION
std::cout << std::boolalpha << std::is_convertible_v<vk::ArrayWrapper1D<char, 10>, std::string_view> << std::endl;

std::string_view sv1 = aw1;
std::string_view sv1 = awc1;
assert( sv1.size() == 6 );
sv1 = aw2;
sv1 = awc2;
assert( sv1.size() == 6 );
sv1 = aw3;
sv1 = awc3;
assert( sv1.size() == 5 );

vk::ArrayWrapper1D<char, 8> aw5( sv1 );
vk::ArrayWrapper1D<char, 8> awc5( sv1 );
#endif

assert( awc1 == awc2 );
assert( awc3 == awc4 );
assert( foobah == awc2 );
assert( foobah > awc4 );

return 0;
}
107 changes: 57 additions & 50 deletions vulkan/vulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,66 +146,72 @@ namespace VULKAN_HPP_NAMESPACE
}
#endif

#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
std::strong_ordering operator<=>( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) <=> *static_cast<std::array<char, N> const *>( &rhs );
}
#else
template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator<( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) < *static_cast<std::array<char, N> const *>( &rhs );
}

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator<=( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) <= *static_cast<std::array<char, N> const *>( &rhs );
}

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator>( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) > *static_cast<std::array<char, N> const *>( &rhs );
}

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator>=( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) >= *static_cast<std::array<char, N> const *>( &rhs );
}
#endif

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator==( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) == *static_cast<std::array<char, N> const *>( &rhs );
}

template <typename B = T, typename std::enable_if<std::is_same<B, char>::value, int>::type = 0>
bool operator!=( ArrayWrapper1D<char, N> const & rhs ) const VULKAN_HPP_NOEXCEPT
{
return *static_cast<std::array<char, N> const *>( this ) != *static_cast<std::array<char, N> const *>( &rhs );
}

private:
VULKAN_HPP_CONSTEXPR_14 void copy( char const * data, size_t len ) VULKAN_HPP_NOEXCEPT
{
size_t n = std::min( N, len );
size_t n = std::min( N - 1, len );
for ( size_t i = 0; i < n; ++i )
{
( *this )[i] = data[i];
}
for ( size_t i = n; i < N; ++i )
{
( *this )[i] = 0;
}
( *this )[n] = 0;
}
};

// specialization of relational operators between std::string and arrays of chars
// relational operators between ArrayWrapper1D of chars with potentially different sizes
#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
template <size_t N, size_t M>
std::strong_ordering operator<=>( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
int result = strcmp( lhs.data(), rhs.data() );
return ( result < 0 ) ? std::strong_ordering::less : ( ( result > 0 ) ? std::strong_ordering::greater : std::strong_ordering::equal );
}
#else
template <size_t N, size_t M>
bool operator<( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) < 0;
}

template <size_t N, size_t M>
bool operator<=( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) <= 0;
}

template <size_t N, size_t M>
bool operator>( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) > 0;
}

template <size_t N, size_t M>
bool operator>=( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) >= 0;
}
#endif

template <size_t N, size_t M>
bool operator==( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) == 0;
}

template <size_t N, size_t M>
bool operator!=( ArrayWrapper1D<char, N> const & lhs, ArrayWrapper1D<char, M> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return strcmp( lhs.data(), rhs.data() ) != 0;
}

// specialization of relational operators between std::string and arrays of chars
#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
template <size_t N>
std::strong_ordering operator<=>( std::string const & lhs, ArrayWrapper1D<char, N> const & rhs ) VULKAN_HPP_NOEXCEPT
{
return lhs <=> rhs.data();
}
#else
template <size_t N>
bool operator<( std::string const & lhs, ArrayWrapper1D<char, N> const & rhs ) VULKAN_HPP_NOEXCEPT
{
Expand All @@ -229,6 +235,7 @@ namespace VULKAN_HPP_NAMESPACE
{
return lhs >= rhs.data();
}
#endif

template <size_t N>
bool operator==( std::string const & lhs, ArrayWrapper1D<char, N> const & rhs ) VULKAN_HPP_NOEXCEPT
Expand Down
Loading

0 comments on commit 920012f

Please sign in to comment.