CTR Pia  4.11.3
Game Communication Engine
common_FixedRingBuffer.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_FixedRingBuffer.h
4 
5  Copyright Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 
15 #pragma once
16 
17 #include <pia/common/common_definitions.h>
18 
19 
20 namespace nn
21 {
22 namespace pia
23 {
24 namespace common
25 {
26 
27 
28 /*!
29 @cond PRIVATE
30 @brief This class represents a fixed length ring buffer.
31 
32 @date 2012-05-14 Initial version.
33 */
34 template <typename T, int N>
35 class FixedRingBuffer : public common::RootObject
36 {
37 public:
38 /*!
39 @brief Instantiates the object.
40 */
41  FixedRingBuffer(void)
42  {
43  Clear();
44  }
45 
46 
47 /*!
48 @brief Destroys the object.
49 */
50  ~FixedRingBuffer(void)
51  {
52  }
53 
54 
55 /*!
56 @brief Empties the ring buffer.
57 */
58  void Clear(void)
59  {
60  m_Size = 0;
61  m_BeginIndex = 0;
62  }
63 
64 
65 /*!
66 @brief Adds an element to the end. This function asserts if executed when the buffer is full.
67 */
68  void PushBack(T value)
69  {
70  PIA_ASSERT(!IsFull());
71 
72  pushBack(value);
73  }
74 
75 
76 /*!
77 @brief Adds an element to the end. Destroys the element at the start of the buffer and overwrites if executed when the buffer is full. This version does not assert.
78 */
79  void PushBackForce(T value)
80  {
81  pushBack(value);
82  }
83 
84 
85 /*!
86 @brief Removes one element from the start of the buffer. This function asserts if called when the buffer is empty.
87 */
88  void PopFront(void)
89  {
90  PIA_ASSERT(!IsEmpty());
91 
92  incBeginIndex();
93  --m_Size;
94  }
95 
96 
97 /*!
98 @brief Determines whether the ring buffer is full.
99 */
100  bool IsFull(void) const
101  {
102  return m_Size == N;
103  }
104 
105 
106 /*!
107 @brief Determines whether there are any elements in the ring buffer.
108 */
109  bool IsEmpty(void) const
110  {
111  return m_Size == 0;
112  }
113 
114 
115 /*!
116 @brief Gets the number of elements in the ring buffer.
117 */
118  size_t GetSize(void) const
119  {
120  return m_Size;
121  }
122 
123 
124 /*!
125 @brief Provides access using the [] operators (non-<tt>const</tt> version).
126 */
127  T& operator[](size_t n)
128  {
129  return m_Values[getIndex(n)];
130  }
131 
132 
133 /*!
134 @brief Provides access using the [] operators (<tt>const</tt> version).
135 */
136  const T& operator[](size_t n) const
137  {
138  return m_Values[getIndex(n)];
139  }
140 
141 
142 /*!
143 @brief Prints information useful for debugging.
144 
145 @param[in] flag Specifies the bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
146 */
147  void Trace(u64 flag) const
148  {
149  (void)flag; // Not implemented.
150  }
151 
152 
153 private:
154  // Add an element to the end. When the elements are full, the element at the start is overwritten.
155  void pushBack(T value)
156  {
157  m_Values[getEndIndex()] = value;
158  if (IsFull())
159  {
160  incBeginIndex();
161  }
162  else
163  {
164  ++m_Size;
165  }
166  }
167 
168  // Increments the start index.
169  void incBeginIndex(void)
170  {
171  m_BeginIndex = (m_BeginIndex + 1) % N;
172  }
173 
174  // Currently, the position that the lead element is in <tt>m_Values[]</tt> is returned as a subscript.
175  size_t getBeginIndex(void) const
176  {
177  return m_BeginIndex;
178  }
179 
180  // Currently, the position that the element after the last element is in <tt>m_Values[]</tt> is returned as a subscript.
181  size_t getEndIndex(void) const
182  {
183  return (getBeginIndex() + GetSize()) % N;
184  }
185 
186  // Returns a specific index value (a subscript value in <tt>m_Values[]</tt>).
187  size_t getIndex(size_t n) const
188  {
189  PIA_ASSERT(n < N);
190  return (m_BeginIndex + n) % N;
191  }
192 
193  size_t m_Size; // Number of elements maintained.
194  size_t m_BeginIndex; // Supports the subscript of the value displayed with [0].
195  T m_Values[N]; // The instance of the elements.
196 };
197 //! @endcond
198 }
199 }
200 } // end of namespace nn::pia::common
Definition: assert.h:115