CTR Pia  4.11.3
Game Communication Engine
common_SimpleContainer.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_SimpleContainer.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 Represents a simple container having a fixed size.
31 */
32 template <typename T, int N>
33 class SimpleContainer : public nn::pia::common::RootObject
34 {
35 public:
36  typedef T* Iterator; //!< Defines a non-<tt>const</tt> iterator type.
37  typedef const T* ConstIterator; //!< Defines a <tt>const</tt> iterator type.
38 
39 /*!
40 @brief Gets the iterator for the instance at the start of the container. (<tt>const</tt> version)
41 If there are no valid elements in the container, this function returns the same iterator as the one returned by the <tt>End</tt> function.
42 @return Returns an iterator for the instance at the start of the container.
43 @see End
44 */
45  ConstIterator Begin(void) const
46  {
47  return &(m_Buffer[0]); // This is fine, because when there are no elements in the container, the <tt>End</tt> function returns <tt>&(m_Buffer[0])</tt>.
48  }
49 
50 
51 /*!
52 @brief Gets the iterator for the instance at the start of the container (non-<tt>const</tt> version).
53 If there are no valid elements, this function returns the same iterator as the one returned by the <tt>End</tt> function.
54 @return Returns an iterator for the instance at the start of the container.
55 @see End
56 */
57  Iterator Begin(void)
58  {
59  return &(m_Buffer[0]); // This is fine, because when there are no elements in the container, the <tt>End</tt> function returns <tt>&(m_Buffer[0])</tt>.
60  }
61 
62 
63 /*!
64 @brief Gets the iterator for the instance at the end of the container (<tt>const</tt> version).
65 @return Returns the iterator for the instance at the end of the container.
66 @see Begin
67 */
68  ConstIterator End(void) const
69  {
70  return &(m_Buffer[m_Elements]);
71  }
72 
73 
74 /*!
75 @brief Gets the iterator for the instance at the end of the container (non-<tt>const</tt> version).
76 @return Returns the iterator for the instance at the end of the container.
77 @see Begin
78 */
79  Iterator End(void)
80  {
81  return &(m_Buffer[m_Elements]);
82  }
83 
84 
85 /*!
86 @brief Instantiates the object.
87 */
88  SimpleContainer(void)
89  : m_Elements(0),
90  m_Buffer()
91  {
92  }
93 
94 
95 /*!
96 @brief This is the copy constructor.
97 */
98  SimpleContainer(const SimpleContainer& rhs)
99  {
100  *this = rhs;
101  }
102 
103 
104 /*!
105 @brief Destroys the object.
106 */
107  virtual ~SimpleContainer(void)
108  {
109  }
110 
111 
112 /*!
113 @brief Adds an element to the container.
114 
115 @return Returns a <tt>Result</tt> value for which <tt>IsSuccess</tt> returns <tt>true</tt> if added successfully.
116 @retval ResultBufferIsFull Specifies that no more elements can be added because the container is full.
117 @see IsFull, Clear, Erase
118 */
119  nn::Result Add(const T& t)
120  {
121  if (IsFull())
122  {
123  PIA_RETURN_RESULT(ResultBufferIsFull);
124  }
125  else
126  {
127  m_Buffer[m_Elements++] = t;
128  return ResultSuccess();
129  }
130  }
131 
132 
133 /*!
134 @brief Removes the element pointed to by the iterator from the container.
135 Elements after the element removed are shifted toward the front.
136 A bit more CPU cost is therefore incurred when dealing with large elements or removing an element near the front.
137 
138 @param[in] it Specifies a valid iterator retrieved from this container.
139 @return Returns an iterator pointing to the next element after the one removed. Returns the same iterator as the <tt>End</tt> function if the last element was removed.
140 @see Add
141 
142 */
143  Iterator Erase(Iterator it)
144  {
145  if ((Begin() <= it) && (it < End()))
146  {
147  // Shift subsequent elements.
148  for (Iterator i = it; (i + 1) != End(); ++i)
149  {
150  *i = *(i + 1);
151  }
152  PIA_ASSERT(m_Elements > 0);
153  --m_Elements;
154  // Because the elements were shifted to the front, the removed location becomes the location of the next element in the container.
155  // Fortunately, deleting the last element makes this location the same as what the <tt>End</tt> function returns.
156  return it;
157  }
158  else
159  {
160  // Specifies an invalid iterator. (It might be out of range.) Does nothing.
161  return End();
162  }
163  }
164 
165 /*!
166 @brief Removes all elements in the container.
167 
168 @see IsFull, Clear
169 */
170  void Clear(void)
171  {
172  m_Elements = 0;
173  }
174 
175 
176 /*!
177 @brief Determines whether the container is empty.
178 
179 @return Returns <tt>true</tt> if the container is empty, and <tt>false</tt> if there are any elements in it.
180 @see IsFull, GetSize
181 */
182  bool IsEmpty(void) const
183  {
184  return m_Elements == 0;
185  }
186 
187 
188 /*!
189 @brief Determines whether the container is full.
190 
191 @return Returns <tt>true</tt> if no more elements can be added; returns <tt>false</tt> otherwise.
192 @see IsEmpty, GetSize
193 */
194  bool IsFull(void) const
195  {
196  return m_Elements == N;
197  }
198 
199 
200 /*!
201 @brief Gets the number of elements in the container.
202 
203 @return Returns the number of valid elements in the container.
204 @see IsEmpty, IsFull
205 */
206  size_t GetSize(void) const
207  {
208  return m_Elements;
209  }
210 
211 
212 /*!
213 @brief This is the assignment operator.
214 
215 @return Returns a reference to this instance.
216 */
217  SimpleContainer& operator=(const SimpleContainer& rhs)
218  {
219  m_Elements = rhs.m_Elements;
220 
221  for (int i = 0; i < N; ++i)
222  {
223  m_Buffer[i] = rhs.m_Buffer[i];
224  }
225 
226  return *this;
227  }
228 
229 
230 /*!
231 @brief Prints information useful for debugging.
232 
233 @param[in] flag Specifies the bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
234 */
235  virtual void Trace(u64 flag) const
236  {
237  PIA_TRACE(flag, "----- SimpleContainer::Trace() called. -----");
238  PIA_TRACE(flag, "Number of elements: %d", m_Elements);
239  PIA_TRACE(flag, "----- SimpleContainer::Trace() end. -----");
240  }
241 
242 
243 private:
244  // Specifies the number of valid elements included in the container.
245  size_t m_Elements;
246 
247  // Represents the element instances.
248  T m_Buffer[N];
249 };
250 //! @endcond
251 }
252 }
253 } // end of namespace nn::pia::common
Definition: assert.h:115
This is the common base class used inside the Pia library.
Definition: common_RootObject.h:40