CTR-Pia  5.4.3
Game Communication Engine
 全て クラス ネームスペース 関数 変数 型定義 列挙型 列挙型の値 ページ
common_SimpleContainer.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 template <typename T, int N>
32 class SimpleContainer : public nn::pia::common::RootObject
33 {
34 public:
35  typedef T* Iterator; //!< 非constのイテレータ型です。
36  typedef const T* ConstIterator; //!< constのイテレータ型です。
37 
38  /*!
39  @brief コンテナ先頭にあるインスタンスに対応するイテレータを得ます(const版)。
40  コンテナ内に有効な要素が一つも無ければ、Endが返すものと同じイテレータが返されます。
41  @return 先頭にあるインスタンスのイテレータを返します。
42  @see End
43  */
44  ConstIterator Begin(void) const
45  {
46  return &(m_Buffer[0]); // 要素が一つも無いとき、End()は&(m_Buffer[0])を返すので、これで十分。
47  }
48 
49 
50  /*!
51  @brief コンテナ先頭にあるインスタンスに対応するイテレータを得ます(非const版)。
52  有効な要素が一つも無ければ、Endが返すものと同じイテレータが返されます。
53  @return 先頭にあるインスタンスのイテレータを返します。
54  @see End
55  */
56  Iterator Begin(void)
57  {
58  return &(m_Buffer[0]); // 要素が一つも無いとき、End()は&(m_Buffer[0])を返すので、これで十分。
59  }
60 
61 
62  /*!
63  @brief 最後尾のインスタンスの次に対応するイテレータを得ます(const版)。
64  @return 最後尾のインスタンスの次に対応するイテレータを得ます。
65  @see Begin
66  */
67  ConstIterator End(void) const
68  {
69  return &(m_Buffer[m_Elements]);
70  }
71 
72 
73  /*!
74  @brief 最後尾の次のインスタンスに対応するイテレータを得ます(非const版)。
75  @return 最後尾の次のインスタンスに対応するイテレータを得ます。
76  @see Begin
77  */
78  Iterator End(void)
79  {
80  return &(m_Buffer[m_Elements]);
81  }
82 
83 
84  /*!
85  @brief コンストラクタです。
86  */
87  SimpleContainer(void)
88  : m_Elements(0),
89  m_Buffer()
90  {
91  }
92 
93 
94  /*!
95  @brief コピーコンストラクタです。
96  */
97  SimpleContainer(const SimpleContainer& rhs)
98  {
99  *this = rhs;
100  }
101 
102 
103  /*!
104  @brief デストラクタです。
105  */
106  virtual ~SimpleContainer(void)
107  {
108  }
109 
110 
111  /*!
112  @brief コンテナに要素を追加します。
113 
114  @return 追加に成功すれば、IsSuccess()がtrueを返すResultが返されます。
115  @retval ResultBufferIsFull コンテナが満杯のため、これ以上要素を追加することができません。
116  @see IsFull, Clear, Erase
117  */
118  Result Add(const T& t)
119  {
120  if (IsFull())
121  {
122  PIA_RETURN_RESULT(ResultBufferIsFull);
123  }
124  else
125  {
126  m_Buffer[m_Elements++] = t;
127  return ResultSuccess();
128  }
129  }
130 
131 
132  /*!
133  @brief イテレータで指定された要素をコンテナから取り除きます。
134  取り除く要素の後ろにあった要素は、すべて前へ詰められます。
135  そのため、要素のサイズが大きい場合や、先頭付近の要素を
136  取り除く場合は、大きめのCPU処理コストが発生します。
137 
138  @param[in] it 本コンテナから得られた、有効なイテレータ。
139  @return 消去した要素の次の要素を指すイテレータ。削除したのが最後尾の要素であれば、End()と同じイテレータが返されます。
140  @see Add
141  */
142  Iterator Erase(Iterator it)
143  {
144  if ((Begin() <= it) && (it < End()))
145  {
146  // 後続の要素を詰める。
147  for (Iterator i = it; (i + 1) != End(); ++i)
148  {
149  *i = *(i + 1);
150  }
151  PIA_ASSERT(m_Elements > 0);
152  --m_Elements;
153  // 前に詰めたので、「消した場所=消した場所の次にあった要素」となる。
154  // 最後尾の要素を消した場合は、めでたくEnd()と同じになる。
155  return it;
156  }
157  else
158  {
159  // 無効なイテレータ(範囲外の可能性)。何もしない…。
160  return End();
161  }
162  }
163 
164  /*!
165  @brief コンテナ内の要素を全て削除します。
166 
167  @see IsFull, Clear
168  */
169  void Clear(void)
170  {
171  m_Elements = 0;
172  }
173 
174 
175  /*!
176  @brief コンテナが空であるかどうかを判定します。
177 
178  @return 空であればtrue、何か一つでも要素があればfalseが返されます。
179  @see IsFull, GetSize
180  */
181  bool IsEmpty(void) const
182  {
183  return m_Elements == 0;
184  }
185 
186 
187  /*!
188  @brief コンテナが満杯であるかどうかを判定します。
189 
190  @return これ以上要素を追加することができない状態であればtrue、そうでなければfalseが返されます。
191  @see IsEmpty, GetSize
192  */
193  bool IsFull(void) const
194  {
195  return m_Elements == N;
196  }
197 
198 
199  /*!
200  @brief コンテナ内の要素数を返します。
201 
202  @return コンテナに含まれている有効な要素の数を返します。
203  @see IsEmpty, IsFull
204  */
205  int32_t GetSize(void) const
206  {
207  return m_Elements;
208  }
209 
210 
211  /*!
212  @brief 代入演算子です。
213 
214  @return 本インスタンスへの参照が返ります。
215  */
216  SimpleContainer& operator=(const SimpleContainer& rhs)
217  {
218  m_Elements = rhs.m_Elements;
219 
220  for (int i = 0; i < N; ++i)
221  {
222  m_Buffer[i] = rhs.m_Buffer[i];
223  }
224 
225  return *this;
226  }
227 
228 
229  /*!
230  @brief デバッグに有用な情報をプリントします。
231 
232  @param[in] flag トレースフラグの論理和。詳細は@ref TraceFlag 型を参照してください。
233  */
234  virtual void Trace(uint64_t flag) const
235  {
236  PIA_TRACE(flag, "----- SimpleContainer::Trace() called. -----");
237  PIA_TRACE(flag, "Number of elements: %d", m_Elements);
238  PIA_TRACE(flag, "----- SimpleContainer::Trace() end. -----");
239  }
240 
241 
242 private:
243  // コンテナに含まれる、有効な要素の数。
244  int32_t m_Elements;
245 
246  // 要素の実体。
247  T m_Buffer[N];
248 };
249 //! @endcond
250 }
251 }
252 } // end of namespace nn::pia::common