GrowableStack.hpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2010 Erin Catto http://www.box2d.org
3  * Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 
22 #ifndef PLAYRHO_COMMON_GROWABLESTACK_HPP
23 #define PLAYRHO_COMMON_GROWABLESTACK_HPP
24 
26 #include <cstring>
27 
28 namespace playrho {
29 
33 template <typename T, std::size_t N>
35 {
36 public:
37 
39  using ElementType = T;
40 
42  using CountType = std::size_t;
43 
46  {
47  return CountType(N);
48  }
49 
52  {
53  return CountType{2};
54  }
55 
56  GrowableStack() = default;
57 
58  GrowableStack(const GrowableStack& other) = delete;
59 
60  GrowableStack(GrowableStack&& other) = delete;
61 
62  ~GrowableStack() noexcept
63  {
64  if (m_stack != m_array)
65  {
66  Free(m_stack);
67  m_stack = nullptr;
68  }
69  }
70 
71  GrowableStack& operator= (const GrowableStack& copy) = delete;
72 
73  GrowableStack& operator= (GrowableStack&& copy) = delete;
74 
76  void push(const ElementType& element)
77  {
78  if (m_count == m_capacity)
79  {
80  const auto old = m_stack;
81  m_capacity *= GetBufferGrowthRate();
82  m_stack = Alloc<T>(m_capacity);
83  std::memcpy(m_stack, old, m_count * sizeof(T));
84  if (old != m_array)
85  {
86  Free(old);
87  }
88  }
89 
90  *(m_stack + m_count) = element;
91  ++m_count;
92  }
93 
97  ElementType top() const
98  {
99  assert(m_count > 0);
100  return m_stack[m_count - 1];
101  }
102 
104  void pop() noexcept
105  {
106  assert(m_count > 0);
107  --m_count;
108  }
109 
111  PLAYRHO_CONSTEXPR inline CountType size() const noexcept
112  {
113  return m_count;
114  }
115 
117  PLAYRHO_CONSTEXPR inline CountType capacity() const noexcept
118  {
119  return m_capacity;
120  }
121 
123  PLAYRHO_CONSTEXPR inline bool empty() const noexcept
124  {
125  return m_count == 0;
126  }
127 
128 private:
129  ElementType m_array[N];
130  ElementType* m_stack = m_array;
131  CountType m_count = 0;
132  CountType m_capacity = N;
133 };
134 
135 } // namespace playrho
136 
137 #endif // PLAYRHO_COMMON_GROWABLESTACK_HPP