Warning, file /education/gcompris/external/qml-box2d/Box2D/Common/b2GrowableStack.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * Copyright (c) 2010 Erin Catto http://www.box2d.org 0003 * 0004 * This software is provided 'as-is', without any express or implied 0005 * warranty. In no event will the authors be held liable for any damages 0006 * arising from the use of this software. 0007 * Permission is granted to anyone to use this software for any purpose, 0008 * including commercial applications, and to alter it and redistribute it 0009 * freely, subject to the following restrictions: 0010 * 1. The origin of this software must not be misrepresented; you must not 0011 * claim that you wrote the original software. If you use this software 0012 * in a product, an acknowledgment in the product documentation would be 0013 * appreciated but is not required. 0014 * 2. Altered source versions must be plainly marked as such, and must not be 0015 * misrepresented as being the original software. 0016 * 3. This notice may not be removed or altered from any source distribution. 0017 */ 0018 0019 #ifndef B2_GROWABLE_STACK_H 0020 #define B2_GROWABLE_STACK_H 0021 #include <Box2D/Common/b2Settings.h> 0022 #include <string.h> 0023 0024 /// This is a growable LIFO stack with an initial capacity of N. 0025 /// If the stack size exceeds the initial capacity, the heap is used 0026 /// to increase the size of the stack. 0027 template <typename T, int32 N> 0028 class b2GrowableStack 0029 { 0030 public: 0031 b2GrowableStack() 0032 { 0033 m_stack = m_array; 0034 m_count = 0; 0035 m_capacity = N; 0036 } 0037 0038 ~b2GrowableStack() 0039 { 0040 if (m_stack != m_array) 0041 { 0042 b2Free(m_stack); 0043 m_stack = NULL; 0044 } 0045 } 0046 0047 void Push(const T& element) 0048 { 0049 if (m_count == m_capacity) 0050 { 0051 T* old = m_stack; 0052 m_capacity *= 2; 0053 m_stack = (T*)b2Alloc(m_capacity * sizeof(T)); 0054 memcpy(m_stack, old, m_count * sizeof(T)); 0055 if (old != m_array) 0056 { 0057 b2Free(old); 0058 } 0059 } 0060 0061 m_stack[m_count] = element; 0062 ++m_count; 0063 } 0064 0065 T Pop() 0066 { 0067 b2Assert(m_count > 0); 0068 --m_count; 0069 return m_stack[m_count]; 0070 } 0071 0072 int32 GetCount() 0073 { 0074 return m_count; 0075 } 0076 0077 private: 0078 T* m_stack; 0079 T m_array[N]; 0080 int32 m_count; 0081 int32 m_capacity; 0082 }; 0083 0084 0085 #endif