medfall

A super great game engine
Log | Files | Refs

commit a5cbef0b759cd10929d4ed9678281f83624974e1
parent 9f7069f632d04362cf2834fb6d3d629f02e9885b
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat May 13 15:30:54 +0300

StaticArray shouldn't need initialising

Fixes stuff like `StaticArray< int, 4 > * a = malloc( ... )`.

Diffstat:
array.h | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 80 insertions(+), 6 deletions(-)
diff --git a/array.h b/array.h @@ -161,18 +161,92 @@ private: T * elems; }; +// TODO: sucky duplication template< typename T, size_t N > -class StaticArray : public array< T > { +class StaticArray { public: - NONCOPYABLE( StaticArray ); + T & operator[]( size_t i ) { + ASSERT( i < N ); + return elems[ i ]; + } + + const T & operator[]( size_t i ) const { + ASSERT( i < N ); + return elems[ i ]; + } + + array< T > operator+( size_t i ) { + ASSERT( i <= N ); + return array< T >( elems + i, N - i ); + } + + const array< T > operator+( size_t i ) const { + ASSERT( i <= N ); + return array< T >( elems + i, N - i ); + } + + bool in_range( size_t i ) const { + return i < N; + } + + T * ptr() { + return elems; + } + + const T * ptr() const { + return elems; + } + + size_t num_bytes() const { + return sizeof( T ) * N; + } + + T * begin() { + return elems; + } + + T * end() { + return elems + N; + } + + const T * begin() const { + return elems; + } + + const T * end() const { + return elems + N; + } + + array< T > slice( size_t start, size_t one_past_end ) { + ASSERT( start <= one_past_end ); + ASSERT( one_past_end <= N ); + return array< T >( elems + start, one_past_end - start ); + } + + const array< T > slice( size_t start, size_t one_past_end ) const { + ASSERT( start <= one_past_end ); + ASSERT( one_past_end <= N ); + return array< T >( elems + start, one_past_end - start ); + } + + template< typename S > + array< S > cast() { + ASSERT( num_bytes() % sizeof( S ) == 0 ); + return array< S >( ( S * ) ptr(), num_bytes() / sizeof( S ) ); + } + + template< typename S > + const array< S > cast() const { + ASSERT( num_bytes() % sizeof( S ) == 0 ); + return array< S >( ( S * ) ptr(), num_bytes() / sizeof( S ) ); + } - StaticArray() : array< T >() { - array< T >::elems = elems_memory; - array< T >::n = N; + operator array< T >() { + return array< T >( elems, N ); } private: - T elems_memory[ N ]; + T elems[ N ]; }; // TODO: pluggable allocators