The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.0KB

  1. /*
  2. * Copyright (c) 2010 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2_GROWABLE_STACK_H
  19. #define B2_GROWABLE_STACK_H
  20. #include "b2Settings.h"
  21. #include <cstring>
  22. /// This is a growable LIFO stack with an initial capacity of N.
  23. /// If the stack size exceeds the initial capacity, the heap is used
  24. /// to increase the size of the stack.
  25. template <typename T, juce::int32 N>
  26. class b2GrowableStack
  27. {
  28. public:
  29. b2GrowableStack()
  30. {
  31. m_stack = m_array;
  32. m_count = 0;
  33. m_capacity = N;
  34. }
  35. ~b2GrowableStack()
  36. {
  37. if (m_stack != m_array)
  38. {
  39. b2Free(m_stack);
  40. m_stack = NULL;
  41. }
  42. }
  43. void Push(const T& element)
  44. {
  45. if (m_count == m_capacity)
  46. {
  47. T* old = m_stack;
  48. m_capacity *= 2;
  49. m_stack = (T*)b2Alloc(m_capacity * sizeof(T));
  50. std::memcpy(m_stack, old, m_count * sizeof(T));
  51. if (old != m_array)
  52. {
  53. b2Free(old);
  54. }
  55. }
  56. m_stack[m_count] = element;
  57. ++m_count;
  58. }
  59. T Pop()
  60. {
  61. b2Assert(m_count > 0);
  62. --m_count;
  63. return m_stack[m_count];
  64. }
  65. juce::int32 GetCount()
  66. {
  67. return m_count;
  68. }
  69. private:
  70. T* m_stack;
  71. T m_array[N];
  72. juce::int32 m_count;
  73. juce::int32 m_capacity;
  74. };
  75. #endif