1. template <typename T, int32 N>
  2. class b2GrowableStack
  3. {
  4. public:
  5.     b2GrowableStack()
  6.     {
  7.         m_stack = m_array;
  8.         m_count = 0;
  9.         m_capacity = N;
  10.     }
  11.  
  12.     ~b2GrowableStack()
  13.     {
  14.         if (m_stack != m_array)
  15.         {
  16.             b2Free(m_stack);
  17.             m_stack = NULL;
  18.         }
  19.     }
  20.  
  21.     void Push(const T& element)
  22.     {
  23.         if (m_count == m_capacity)
  24.         {
  25.             T* old = m_stack;
  26.             m_capacity *= 2;
  27.             m_stack = (T*)b2Alloc(m_capacity * sizeof(T));
  28.             std::memcpy(m_stack, old, m_count * sizeof(T));
  29.             if (old != m_array)
  30.             {
  31.                 b2Free(old);
  32.             }
  33.         }
  34.  
  35.         m_stack[m_count] = element;
  36.         ++m_count;
  37.     }
  38.  
  39.     T Pop()
  40.     {
  41.         b2Assert(m_count > 0);
  42.         --m_count;
  43.         return m_stack[m_count];
  44.     }
  45.  
  46.     int32 GetCount()
  47.     {
  48.         return m_count;
  49.     }
  50.  
  51. private:
  52.     T* m_stack;
  53.     T m_array[N];
  54.     int32 m_count;
  55.     int32 m_capacity;
  56. };