Vulkan-Hpp/snippets/ArrayProxyNoTemporaries.hpp
2024-06-25 17:43:55 +02:00

104 lines
3.1 KiB
C++

template <typename T>
class ArrayProxyNoTemporaries
{
public:
VULKAN_HPP_CONSTEXPR ArrayProxyNoTemporaries() VULKAN_HPP_NOEXCEPT
: m_count( 0 )
, m_ptr( nullptr )
{
}
VULKAN_HPP_CONSTEXPR ArrayProxyNoTemporaries( std::nullptr_t ) VULKAN_HPP_NOEXCEPT
: m_count( 0 )
, m_ptr( nullptr )
{
}
template <typename B = T, typename std::enable_if<std::is_convertible<B, T>::value && std::is_lvalue_reference<B>::value, int>::type = 0>
ArrayProxyNoTemporaries( B && value ) VULKAN_HPP_NOEXCEPT
: m_count( 1 )
, m_ptr( &value )
{
}
ArrayProxyNoTemporaries( uint32_t count, T * ptr ) VULKAN_HPP_NOEXCEPT
: m_count( count )
, m_ptr( ptr )
{
}
template <std::size_t C>
ArrayProxyNoTemporaries( T ( &ptr )[C] ) VULKAN_HPP_NOEXCEPT
: m_count( C )
, m_ptr( ptr )
{
}
template <std::size_t C>
ArrayProxyNoTemporaries( T ( &&ptr )[C] ) = delete;
// Any l-value reference with a .data() return type implicitly convertible to T*, and a .size() return type implicitly convertible to size_t.
template <typename V,
typename std::enable_if<!std::is_convertible<decltype( std::declval<V>().begin() ), T *>::value &&
std::is_convertible<decltype( std::declval<V>().data() ), T *>::value &&
std::is_convertible<decltype( std::declval<V>().size() ), std::size_t>::value && std::is_lvalue_reference<V>::value,
int>::type = 0>
ArrayProxyNoTemporaries( V && v ) VULKAN_HPP_NOEXCEPT
: m_count( static_cast<uint32_t>( v.size() ) )
, m_ptr( v.data() )
{
}
// Any l-value reference with a .begin() return type implicitly convertible to T*, and a .size() return type implicitly convertible to size_t.
template <typename V,
typename std::enable_if<std::is_convertible<decltype( std::declval<V>().begin() ), T *>::value &&
std::is_convertible<decltype( std::declval<V>().size() ), std::size_t>::value && std::is_lvalue_reference<V>::value,
int>::type = 0>
ArrayProxyNoTemporaries( V && v ) VULKAN_HPP_NOEXCEPT
: m_count( static_cast<uint32_t>( v.size() ) )
, m_ptr( v.begin() )
{
}
const T * begin() const VULKAN_HPP_NOEXCEPT
{
return m_ptr;
}
const T * end() const VULKAN_HPP_NOEXCEPT
{
return m_ptr + m_count;
}
const T & front() const VULKAN_HPP_NOEXCEPT
{
VULKAN_HPP_ASSERT( m_count && m_ptr );
return *m_ptr;
}
const T & back() const VULKAN_HPP_NOEXCEPT
{
VULKAN_HPP_ASSERT( m_count && m_ptr );
return *( m_ptr + m_count - 1 );
}
bool empty() const VULKAN_HPP_NOEXCEPT
{
return ( m_count == 0 );
}
uint32_t size() const VULKAN_HPP_NOEXCEPT
{
return m_count;
}
T * data() const VULKAN_HPP_NOEXCEPT
{
return m_ptr;
}
private:
uint32_t m_count;
T * m_ptr;
};