LiquidFun
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
b2StackQueue.h
1 /*
2 * Copyright (c) 2013 Google, Inc.
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_STACK_QUEUE
19 #define B2_STACK_QUEUE
20 
21 #include <Box2D/Common/b2StackAllocator.h>
22 
23 template <typename T>
25 {
26 
27 public:
28 
29  b2StackQueue(b2StackAllocator *allocator, int32 capacity)
30  {
31  m_allocator = allocator;
32  m_buffer = (T*) m_allocator->Allocate(sizeof(T) * capacity);
33  m_front = m_buffer;
34  m_back = m_buffer;
35  m_end = m_buffer + capacity;
36  }
37 
38  ~b2StackQueue()
39  {
40  m_allocator->Free(m_buffer);
41  m_buffer = NULL;
42  m_front = NULL;
43  m_back = NULL;
44  m_end = NULL;
45  }
46 
47  void Push(const T &item)
48  {
49  if (m_back >= m_end)
50  {
51  ptrdiff_t diff = m_front - m_buffer;
52  for (T *it = m_front; it < m_back; ++it)
53  {
54  *(it - diff) = *it;
55  }
56  m_front -= diff;
57  m_back -= diff;
58  if (m_back >= m_end)
59  {
60  return;
61  }
62  }
63  *m_back++ = item;
64  }
65 
66  void Pop()
67  {
68  b2Assert(m_front < m_back);
69  m_front++;
70  }
71 
72  bool Empty() const
73  {
74  return m_front >= m_back;
75  }
76 
77  const T &Front() const
78  {
79  return *m_front;
80  }
81 
82 private:
83 
84  b2StackAllocator *m_allocator;
85  T* m_buffer;
86  T* m_front;
87  T* m_back;
88  T* m_end;
89 
90 };
91 
92 #endif
Definition: b2StackAllocator.h:37
Definition: b2StackQueue.h:24