CTR-Pia  5.4.3
Game Communication Engine
 全て クラス ネームスペース 関数 変数 型定義 列挙型 列挙型の値 ページ
common_FixedRingBuffer.h
1 /*--------------------------------------------------------------------------------*
2  Copyright (C)Nintendo All rights reserved.
3 
4  These coded instructions, statements, and computer programs contain proprietary
5  information of Nintendo and/or its licensed developers and are protected by
6  national and international copyright laws. They may not be disclosed to third
7  parties or copied or duplicated in any form, in whole or in part, without the
8  prior written consent of Nintendo.
9 
10  The content herein is highly confidential and should be handled accordingly.
11  *--------------------------------------------------------------------------------*/
12 
13 
14 #pragma once
15 
16 #include <nn/pia/common/common_Definitions.h>
17 
18 
19 namespace nn
20 {
21 namespace pia
22 {
23 namespace common
24 {
25 
26 
27 /*!
28  @cond PRIVATE
29  @brief 固定長のリングバッファのクラスです。
30 
31  */
32 template <typename T, int N>
33 class FixedRingBuffer : public common::RootObject
34 {
35 public:
36  /*!
37  @brief デフォルトコンストラクタです。
38  */
39  FixedRingBuffer(void)
40  {
41  Clear();
42  }
43 
44 
45  /*!
46  @brief デストラクタです。
47  */
48  ~FixedRingBuffer(void)
49  {
50  }
51 
52 
53  /*!
54  @brief リングバッファを空にします。
55  */
56  void Clear(void)
57  {
58  m_Size = 0;
59  m_BeginIndex = 0;
60  }
61 
62 
63  /*!
64  @brief 最後尾に要素を追加します。満杯の場合に実行すると、アサート停止します。
65  */
66  void PushBack(T value)
67  {
68  PIA_ASSERT(!IsFull());
69 
70  pushBack(value);
71  }
72 
73 
74  /*!
75  @brief 最後尾に要素を追加します。満杯の場合に実行すると、先頭要素が破棄され、上書きされます。アサート停止はしません。
76  */
77  void PushBackForce(T value)
78  {
79  pushBack(value);
80  }
81 
82 
83  /*!
84  @brief 先頭から要素を一つ取り除きます。要素が一つも無いときに呼び出した場合は、アサート停止します。
85  */
86  void PopFront(void)
87  {
88  PIA_ASSERT(!IsEmpty());
89 
90  incBeginIndex();
91  --m_Size;
92  }
93 
94 
95  /*!
96  @brief リングバッファは満杯であるかどうかを判定します。
97  */
98  bool IsFull(void) const
99  {
100  return m_Size == N;
101  }
102 
103 
104  /*!
105  @brief リングバッファに一つも要素が無いかどうかを判定します。
106  */
107  bool IsEmpty(void) const
108  {
109  return m_Size == 0;
110  }
111 
112 
113  /*!
114  @brief リングバッファの要素数を取得します。
115  */
116  uint32_t GetSize(void) const
117  {
118  return m_Size;
119  }
120 
121 
122  /*!
123  @brief [] 演算子によるアクセスを提供します(非 const 版)。
124  */
125  T& operator[](size_t n)
126  {
127  return m_Values[getIndex(n)];
128  }
129 
130 
131  /*!
132  @brief [] 演算子によるアクセスを提供します( const 版)。
133  */
134  const T& operator[](size_t n) const
135  {
136  return m_Values[getIndex(n)];
137  }
138 
139 
140  /*!
141  @brief デバッグに有用な情報をプリントします。
142 
143  @param[in] flag トレースフラグの論理和。詳細は@ref TraceFlag 型を参照してください。
144  */
145  void Trace(uint64_t flag) const
146  {
147  (void)flag; // Not implemented...
148  }
149 
150 
151 private:
152  // 最後尾に要素を追加。要素が満杯の時は、先頭要素が上書きされる。
153  void pushBack(T value)
154  {
155  m_Values[getEndIndex()] = value;
156  if (IsFull())
157  {
158  incBeginIndex();
159  }
160  else
161  {
162  ++m_Size;
163  }
164  }
165 
166  // 先頭のインデックスをインクリメント
167  void incBeginIndex(void)
168  {
169  m_BeginIndex = (m_BeginIndex + 1) % N;
170  }
171 
172  // 現時点での先頭要素が m_Values[] におけるどの位置にあるかを添え字で返す。
173  uint32_t getBeginIndex(void) const
174  {
175  return m_BeginIndex;
176  }
177 
178  // 現時点での最後尾要素の次の要素が m_Values[] におけるどの位置にあるかを添え字で返す。
179  uint32_t getEndIndex(void) const
180  {
181  return (getBeginIndex() + GetSize()) % N;
182  }
183 
184  // 具体的なインデックスの値(m_Values[]における添え字の値)を返す
185  uint32_t getIndex(size_t n) const
186  {
187  PIA_ASSERT(n < N);
188  return (m_BeginIndex + n) % N;
189  }
190 
191  uint32_t m_Size; // 保持されている要素の個数。
192  uint32_t m_BeginIndex; // [0] で示される値の添え字に対応
193  T m_Values[N]; // 要素の実体
194 };
195 //! @endcond
196 }
197 }
198 } // end of namespace nn::pia::common