b2GrowableStack.h
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2010 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #ifndef B2_GROWABLE_STACK_H
00020 #define B2_GROWABLE_STACK_H
00021 #include <Box2D/Common/b2Settings.h>
00022 #include <string.h>
00023 
00027 template <typename T, int32 N>
00028 class b2GrowableStack
00029 {
00030 public:
00031         b2GrowableStack()
00032         {
00033                 m_stack = m_array;
00034                 m_count = 0;
00035                 m_capacity = N;
00036         }
00037 
00038         ~b2GrowableStack()
00039         {
00040                 if (m_stack != m_array)
00041                 {
00042                         b2Free(m_stack);
00043                         m_stack = NULL;
00044                 }
00045         }
00046 
00047         void Push(const T& element)
00048         {
00049                 if (m_count == m_capacity)
00050                 {
00051                         T* old = m_stack;
00052                         m_capacity *= 2;
00053                         m_stack = (T*)b2Alloc(m_capacity * sizeof(T));
00054                         memcpy(m_stack, old, m_count * sizeof(T));
00055                         if (old != m_array)
00056                         {
00057                                 b2Free(old);
00058                         }
00059                 }
00060 
00061                 m_stack[m_count] = element;
00062                 ++m_count;
00063         }
00064 
00065         T Pop()
00066         {
00067                 b2Assert(m_count > 0);
00068                 --m_count;
00069                 return m_stack[m_count];
00070         }
00071 
00072         int32 GetCount()
00073         {
00074                 return m_count;
00075         }
00076 
00077 private:
00078         T* m_stack;
00079         T m_array[N];
00080         int32 m_count;
00081         int32 m_capacity;
00082 };
00083 
00084 
00085 #endif


mvsim
Author(s):
autogenerated on Thu Sep 7 2017 09:27:47