commit 8a9d2e56c0408658c3771741731b0cc7fce99f65 parent 975fa20da6521f4bbb7f3c6a9707007d0a4bf15c Author: Michael Savage <mikejsavage@gmail.com> Date: Thu Dec 22 23:01:16 +0200 ConstArrayIterator and array visitors Diffstat:
array.h | | | 42 | ++++++++++++++++++++++++++++++++++++++++++ |
diff --git a/array.h b/array.h @@ -28,6 +28,28 @@ private: }; template< typename T > +class ConstArrayIterator { +public: + ConstArrayIterator( T * ptr ) { + cursor = ptr; + } + + const T & operator*() { + return *cursor; + } + + bool operator!=( ConstArrayIterator< T > other ) { + return cursor != other.cursor; + } + + void operator++() { + cursor++; + } +private: + T * cursor; +}; + +template< typename T > class array { public: array() { @@ -84,6 +106,14 @@ public: return ArrayIterator< T >( elems + n ); } + ConstArrayIterator< T > begin() const { + return ConstArrayIterator< T >( elems ); + } + + ConstArrayIterator< T > end() const { + return ConstArrayIterator< T >( elems + n ); + } + size_t n; protected: @@ -146,4 +176,16 @@ private: T elems_memory[ N ]; }; +template< typename T, typename F > +inline void visit( array< T > & a, F f ) { + f( a.n ); + for( T & x : a ) f( x ); +} + +template< typename T, typename F > +inline void visit( const array< T > & a, F f ) { + f( a.n ); + for( const T & x : a ) f( x ); +} + #endif // _ARRAY_H_