Arrays - std::initializer_list support for dynamic arrays

This commit is contained in:
Emmanuel BENOîT 2017-11-04 12:39:41 +01:00
parent 3745631e02
commit c35f4c6644
2 changed files with 36 additions and 1 deletions

View file

@ -105,6 +105,9 @@ class T_Array final
T_Array( MyType_&& source ) noexcept; T_Array( MyType_&& source ) noexcept;
MyType_& operator= ( MyType_&& other ) noexcept; MyType_& operator= ( MyType_&& other ) noexcept;
T_Array( std::initializer_list< T > list ) noexcept;
MyType_& operator= ( std::initializer_list< T > list ) noexcept;
template< typename TP > template< typename TP >
friend void swap( T_Array< TP >& lhs , friend void swap( T_Array< TP >& lhs ,
T_Array< TP >& rhs ) noexcept; T_Array< TP >& rhs ) noexcept;
@ -159,6 +162,7 @@ class T_Array final
MyType_& addAll( MyType_ const& other ) noexcept; MyType_& addAll( MyType_ const& other ) noexcept;
MyType_& addAll( MyType_&& other ) noexcept; MyType_& addAll( MyType_&& other ) noexcept;
MyType_& addAll( std::initializer_list< T > values ) noexcept;
// --------------------------------------------------------------------- // ---------------------------------------------------------------------

View file

@ -86,6 +86,24 @@ inline T_Array< T >& T_Array< T >::operator=(
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
template< typename T >
inline T_Array< T >::T_Array(
std::initializer_list< T > list ) noexcept
: T_Array( DEFAULT_GROWTH( ) )
{
addAll( std::move( list ) );
}
template< typename T >
inline T_Array< T >& T_Array< T >::operator= (
std::initializer_list< T > list ) noexcept
{
clear( );
return addAll( std::move( list ) );
}
/*----------------------------------------------------------------------------*/
template< typename T > template< typename T >
inline void swap( T_Array< T >& lhs , inline void swap( T_Array< T >& lhs ,
T_Array< T >& rhs ) noexcept T_Array< T >& rhs ) noexcept
@ -330,6 +348,19 @@ inline T_Array< T >& T_Array< T >::addAll(
return *this; return *this;
} }
template< typename T >
inline T_Array< T >& T_Array< T >::addAll(
std::initializer_list< T > other ) noexcept
{
ensureCapacity( size_ + other.size( ) );
auto i = size_;
for ( T const& v : other ) {
::new ( ( char* ) &( data_[ i ++ ] ) ) T( v );
}
size_ += other.size( );
return *this;
}
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
template< typename T > template< typename T >
@ -2125,7 +2156,7 @@ void T_AutoArray< T , S , G >::convertToDynamic_(
{ {
assert( isStatic( ) ); assert( isStatic( ) );
T_Static_& s( static_( ) ); T_Static_& s( static_( ) );
T_Dynamic_ d{ G }; T_Dynamic_ d( G );
d.ensureCapacity( std::max( s.capacity( ) , capacity ) ); d.ensureCapacity( std::max( s.capacity( ) , capacity ) );
auto const sz( s.size( ) ); auto const sz( s.size( ) );
for ( auto i = 0u ; i < sz ; i ++ ) { for ( auto i = 0u ; i < sz ; i ++ ) {