Template class to implement a list with a fixed maximum number of elements (i.e. number of elements in the list is dynamic but cannot exceed the initially defined value)

Dependents:   FixedLengthListTest XBeeApi

Revision:
1:d98987d1d67c
Parent:
0:99d701354221
Child:
2:16c77b601175
diff -r 99d701354221 -r d98987d1d67c FixedLengthList.hpp
--- a/FixedLengthList.hpp	Sat Jan 18 16:04:07 2014 +0000
+++ b/FixedLengthList.hpp	Sun Jan 19 13:23:21 2014 +0000
@@ -35,6 +35,71 @@
 #define STATIC_ASSERT( condition, name ) typedef char assert_failed_ ## name [ (condition) ? 1 : -1 ]
 #endif
 
+/*
+    Each item in the FixedLengthList is wrapped in a
+    FixedLengthListItem which provides the actual item and
+    infrastructure support for the list */
+template < class L > class FixedLengthListItem
+{
+    public:
+        /** Pointer to the next item in the list */
+        FixedLengthListItem<L>* m_forward;
+        /** The content/value of the item itself */
+        L                       m_item;
+};
+
+
+/**
+    Iterator support class for FixedLengthList
+
+   Example:
+   \code
+          #define LIST_LEN (20U)
+          FixedLengthList<int,  LIST_LEN > list;
+
+          int main( void ) {
+             // List is empty
+
+             list.queue( 111 );
+             list.queue( 222 );
+
+             FixedLengthList<int, LIST_LEN>::iterator it = list.begin();
+             *it == 111;
+             it++;
+             *it == 222;
+             it++;
+             *it == list.end();
+          }
+   \endcode
+*/
+template< class T, size_t queueMax > class FixedLengthListIter
+{
+    protected:
+        /** The iterator hooks into the used list within the FixedLengthList */
+        FixedLengthListItem<T>* m_item;
+    public:
+        /** Void constructor - iterator will be equal to T::end() */
+        FixedLengthListIter( void );
+        /** Construct an iterator which points to a list item within a
+            FixedLengthList */
+        FixedLengthListIter( FixedLengthListItem<T>* p_item );
+        /** De-reference operator, yields the value of the list item */
+        T& operator*();
+        /** Inequality operator */
+        bool operator!=( const FixedLengthListIter& p_comp ) const;
+        /** Equality operator */
+        bool operator==( const FixedLengthListIter& p_comp ) const;
+        /** Move the iterator forward a specified number of list elements
+ 
+            \param p_inc Number of items to traverse */
+        FixedLengthListIter& operator+=( const unsigned p_inc );
+        /** Post-increment operator */
+        FixedLengthListIter operator++( int p_int );
+        /** Pre-increment operator */
+        FixedLengthListIter& operator++( void );
+};
+
+
 /**
    Template class to implement a list with a fixed maximum
    number of elements (i.e. the number of elements in the list
@@ -92,22 +157,10 @@
 
     private:
 
-        /**
-           Each item in the FixedLengthList is wrapped in a
-           FixedLengthListItem which provides the actual item and
-           infrastructure support for the list */
-        template < class L > class FixedLengthListItem
-        {
-            public:
-                /** Pointer to the next item in the list */
-                FixedLengthListItem<L>* m_forward;
-                /** The content/value of the item itself */
-                L                       m_item;
-        };
-
         /** Pool of list items */
         FixedLengthListItem<T>  m_items[ queueMax ];
 
+
         /** Pointer to the start of the queue of free list slots.  Will be NULL
             in the case that there none are available */
         FixedLengthListItem<T>* m_freeHead;
@@ -195,8 +248,17 @@
         /** Remove the entire contents of the list and return it back to
             an empty state */
         void clear( void );
+
+        typedef FixedLengthListIter<T, queueMax> iterator;
+        typedef T value_type;
+        typedef T * pointer;
+        typedef T & reference;
+
+        iterator begin( void );
+        iterator end( void );
 };
 
+
 template < class T, size_t queueMax > 
 FixedLengthList< T, queueMax >::FixedLengthList( void )
 {
@@ -204,7 +266,8 @@
 }
  
 template < class T, size_t queueMax > 
-FixedLengthList< T, queueMax >::FixedLengthList( const T* const p_items, size_t p_count ) {
+FixedLengthList< T, queueMax >::FixedLengthList( const T* const p_items, size_t p_count ) 
+{
 
     const T* src = p_items;
 
@@ -471,4 +534,75 @@
     return ret_val;
 }
 
-#endif
+template < class T, size_t queueMax >
+FixedLengthListIter<T, queueMax> FixedLengthList< T,queueMax >::begin( void )
+{
+    return iterator( m_usedHead );
+}
+
+template < class T, size_t queueMax >
+FixedLengthListIter<T, queueMax> FixedLengthList< T,queueMax >::end( void )
+{
+    return iterator( NULL );
+}
+
+template < class T, size_t queueMax >
+FixedLengthListIter< T, queueMax >::FixedLengthListIter( void ) : m_item( NULL)
+{
+}
+
+template < class T, size_t queueMax >
+FixedLengthListIter< T, queueMax >::FixedLengthListIter( FixedLengthListItem<T>* p_item ) : m_item( p_item )
+{
+} 
+
+template < class T, size_t queueMax > 
+T& FixedLengthListIter< T, queueMax >::operator*()
+{
+    return m_item->m_item;
+} 
+
+template < class T, size_t queueMax > 
+FixedLengthListIter< T,queueMax > FixedLengthListIter< T,queueMax >::operator++( int p_int )
+{
+    FixedLengthListIter< T,queueMax > clone( *this );
+    m_item = m_item->m_forward;
+    return clone;
+} 
+
+template < class T, size_t queueMax > 
+FixedLengthListIter< T,queueMax >& FixedLengthListIter< T,queueMax >::operator++( void )
+{
+    m_item = m_item->m_forward;
+    return *this;
+} 
+        
+template < class T, size_t queueMax > 
+FixedLengthListIter< T,queueMax >& FixedLengthListIter< T,queueMax >::operator+=( const unsigned p_inc ) {
+    for(unsigned i = 0;
+        i < p_inc;
+        i++ )
+    {
+        if( m_item == NULL ) {
+            break;
+        } else {
+            m_item = m_item->m_forward;
+        }
+    }
+    return *this;
+}
+
+template < class T, size_t queueMax > 
+bool FixedLengthListIter< T,queueMax >::operator==( const FixedLengthListIter& p_comp ) const
+{
+    return m_item == p_comp.m_item;
+}
+
+template < class T, size_t queueMax > 
+bool FixedLengthListIter< T,queueMax >::operator!=( const FixedLengthListIter& p_comp ) const
+{
+    return m_item != p_comp.m_item;
+}
+
+
+#endif
\ No newline at end of file