Ocean
StaticBuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_STATIC_BUFFER_H
9 #define META_OCEAN_BASE_STATIC_BUFFER_H
10 
11 #include "ocean/base/Base.h"
12 
13 namespace Ocean
14 {
15 
16 /**
17  * This class implements a static buffer that has a fixed capacity.
18  * @tparam T Data type of the elements that will be stored
19  * @tparam tCapacity Number of elements that can be stored, with range [1, infinity)
20  * @ingroup base
21  */
22 template <typename T, size_t tCapacity>
24 {
25  public:
26 
27  /**
28  * Definition of the data type of each individual buffer element.
29  */
30  typedef T Type;
31 
32  public:
33 
34  /**
35  * Creates a new buffer object.
36  */
37  StaticBuffer() = default;
38 
39  /**
40  * Creates a new buffer object.
41  * @param value The value that will be set for the first element of this buffer
42  */
43  explicit inline StaticBuffer(const T& value);
44 
45  /**
46  * Creates a new buffer object.
47  * @param value The value that will be set for the first element of this buffer
48  */
49  explicit inline StaticBuffer(T&& value);
50 
51  /**
52  * Creates a new buffer object.
53  * @param buffer A buffer with at least as much elements as this static buffer has, all 'tCapacity' elements are copied, must be valid
54  */
55  explicit inline StaticBuffer(const T* buffer);
56 
57  /**
58  * Creates a new buffer object.
59  * @param number The number of elements to be created, with range [0, tCapacity]
60  * @param value The value that will be created in the first 'number' elements of this buffer
61  */
62  inline StaticBuffer(const size_t number, const T& value);
63 
64  /**
65  * Creates a new buffer object.
66  * This constructor converts a stl vector object to a static buffer object.<br>
67  * Only the first tCapacity elements of the given vector are copied.
68  * @param values The values that will be used as first elements
69  */
70  explicit inline StaticBuffer(const std::vector<T>& values);
71 
72  /**
73  * Creates a new buffer object.
74  * This constructor converts a stl vector object to a static buffer object.<br>
75  * Only the first tCapacity elements of the given vector are copied.
76  * @param values The values that will be used as first elements
77  */
78  explicit inline StaticBuffer(std::vector<T>&& values);
79 
80  /**
81  * Returns the capacity of this buffer.
82  * @return Buffer capacity
83  */
84  static constexpr size_t capacity();
85 
86  /**
87  * Clears all elements of this buffer.
88  */
89  inline void clear();
90 
91  /**
92  * Returns the first elements of this buffer.
93  * @return First element
94  */
95  inline const T& front() const;
96 
97  /**
98  * Returns the first elements of this buffer.
99  * @return First element
100  */
101  inline T& front();
102 
103  /**
104  * Returns the last elements of this buffer.
105  * @return Last element
106  */
107  inline const T& back() const;
108 
109  /**
110  * Returns the last elements of this buffer.
111  * @return Last element
112  */
113  inline T& back();
114 
115  /**
116  * Returns the buffer data pointer.
117  * @return Data pointer
118  */
119  inline const T* data() const;
120 
121  /**
122  * Returns the buffer data pointer.
123  * @return Data pointer
124  */
125  inline T* data();
126 
127  /**
128  * Returns one element of this buffer.
129  * Beware: No range check is done.
130  * @param index The index of the element that will be returned, with range [0, tCapacity)
131  * @return Buffer element
132  */
133  inline const T& operator[](const size_t index) const;
134 
135  /**
136  * Returns one element of this buffer.
137  * Beware: No range check is done.
138  * @param index The index of the element that will be returned, with range [0, tCapacity)
139  * @return Buffer element
140  */
141  inline T& operator[](const size_t index);
142 
143  /**
144  * Returns whether two buffers are identical.
145  * @param second The second buffer object
146  * @return True, if so
147  */
148  inline bool operator==(const StaticBuffer<T, tCapacity>& second) const;
149 
150  /**
151  * Returns whether two buffers are not identical.
152  * @param second The second buffer object
153  * @return True, if so
154  */
155  inline bool operator!=(const StaticBuffer<T, tCapacity>& second) const;
156 
157  protected:
158 
159  /// Elements of this buffer (with at least one entry).
160  T elements_[tCapacity > size_t(0) ? tCapacity : size_t(1)];
161 };
162 
163 template <typename T, size_t tCapacity>
165 {
166  static_assert(tCapacity > 0, "Invalid buffer capacity!");
167 
168  elements_[0] = value;
169 }
170 
171 template <typename T, size_t tCapacity>
173 {
174  static_assert(tCapacity > 0, "Invalid buffer capacity!");
175 
176  elements_[0] = std::move(value);
177 }
178 
179 template <typename T, size_t tCapacity>
181 {
182  static_assert(tCapacity > 0, "Invalid buffer capacity!");
183 
184  ocean_assert(buffer != nullptr);
185 
186  for (size_t n = 0u; n < tCapacity; ++n)
187  {
188  elements_[n] = buffer[n];
189  }
190 }
191 
192 template <typename T, size_t tCapacity>
193 inline StaticBuffer<T, tCapacity>::StaticBuffer(const size_t number, const T& value)
194 {
195  static_assert(tCapacity > 0, "Invalid buffer capacity!");
196 
197  ocean_assert(number <= tCapacity);
198 
199  for (size_t n = 0; n < number; ++n)
200  {
201  elements_[n] = value;
202  }
203 }
204 
205 template <typename T, size_t tCapacity>
206 inline StaticBuffer<T, tCapacity>::StaticBuffer(const std::vector<T>& values)
207 {
208  static_assert(tCapacity > 0, "Invalid buffer capacity!");
209 
210  const size_t size = min(tCapacity, values.size());
211 
212  for (size_t n = 0; n < size; ++n)
213  {
214  elements_[n] = values[n];
215  }
216 }
217 
218 template <typename T, size_t tCapacity>
219 inline StaticBuffer<T, tCapacity>::StaticBuffer(std::vector<T>&& values)
220 {
221  static_assert(tCapacity > 0, "Invalid buffer capacity!");
222 
223  const size_t size = min(tCapacity, values.size());
224 
225  for (size_t n = 0; n < size; ++n)
226  {
227  elements_[n] = std::move(values[n]);
228  }
229 }
230 
231 template <typename T, size_t tCapacity>
233 {
234  static_assert(tCapacity > 0, "Invalid buffer capacity!");
235 
236  return tCapacity;
237 }
238 
239 template <typename T, size_t tCapacity>
240 inline const T* StaticBuffer<T, tCapacity>::data() const
241 {
242  return elements_;
243 }
244 
245 template <typename T, size_t tCapacity>
247 {
248  return elements_;
249 }
250 
251 template <typename T, size_t tCapacity>
253 {
254  for (size_t n = 0; n < tCapacity; ++n)
255  {
256  elements_[n] = T();
257  }
258 }
259 
260 template <typename T, size_t tCapacity>
261 inline const T& StaticBuffer<T, tCapacity>::front() const
262 {
263  static_assert(tCapacity > 0, "Invalid buffer capacity!");
264 
265  return elements_[0];
266 }
267 
268 template <typename T, size_t tCapacity>
270 {
271  static_assert(tCapacity > 0, "Invalid buffer capacity!");
272 
273  return elements_[0];
274 }
275 
276 template <typename T, size_t tCapacity>
277 inline const T& StaticBuffer<T, tCapacity>::back() const
278 {
279  static_assert(tCapacity > 0, "Invalid buffer capacity!");
280 
281  return elements_[tCapacity - 1];
282 }
283 
284 template <typename T, size_t tCapacity>
286 {
287  static_assert(tCapacity > 0, "Invalid buffer capacity!");
288 
289  return elements_[tCapacity - 1];
290 }
291 
292 template <typename T, size_t tCapacity>
293 inline const T& StaticBuffer<T, tCapacity>::operator[](const size_t index) const
294 {
295  static_assert(tCapacity > 0, "Invalid buffer capacity!");
296 
297  ocean_assert(index < tCapacity);
298 
299  return elements_[index];
300 }
301 
302 template <typename T, size_t tCapacity>
303 inline T& StaticBuffer<T, tCapacity>::operator[](const size_t index)
304 {
305  static_assert(tCapacity > 0, "Invalid buffer capacity!");
306 
307  ocean_assert(index < tCapacity);
308 
309  return elements_[index];
310 }
311 
312 template <typename T, size_t tCapacity>
314 {
315  for (size_t n = 0; n < tCapacity; ++n)
316  {
317  if (elements_[n] != second.elements_[n])
318  {
319  return false;
320  }
321  }
322 
323  return true;
324 }
325 
326 template <typename T, size_t tCapacity>
328 {
329  return !(*this == second);
330 }
331 
332 }
333 
334 #endif // META_OCEAN_BASE_STATIC_BUFFER_H
This class implements a static buffer that has a fixed capacity.
Definition: StaticBuffer.h:24
const T * data() const
Returns the buffer data pointer.
Definition: StaticBuffer.h:240
T elements_[tCapacity > size_t(0) ? tCapacity :size_t(1)]
Elements of this buffer (with at least one entry).
Definition: StaticBuffer.h:160
StaticBuffer(std::vector< T > &&values)
Creates a new buffer object.
Definition: StaticBuffer.h:219
static constexpr size_t capacity()
Returns the capacity of this buffer.
Definition: StaticBuffer.h:232
T Type
Definition of the data type of each individual buffer element.
Definition: StaticBuffer.h:30
T * data()
Returns the buffer data pointer.
Definition: StaticBuffer.h:246
StaticBuffer(T &&value)
Creates a new buffer object.
Definition: StaticBuffer.h:172
const T & back() const
Returns the last elements of this buffer.
Definition: StaticBuffer.h:277
bool operator!=(const StaticBuffer< T, tCapacity > &second) const
Returns whether two buffers are not identical.
Definition: StaticBuffer.h:327
T & back()
Returns the last elements of this buffer.
Definition: StaticBuffer.h:285
StaticBuffer(const size_t number, const T &value)
Creates a new buffer object.
Definition: StaticBuffer.h:193
T & front()
Returns the first elements of this buffer.
Definition: StaticBuffer.h:269
const T & front() const
Returns the first elements of this buffer.
Definition: StaticBuffer.h:261
StaticBuffer(const T &value)
Creates a new buffer object.
Definition: StaticBuffer.h:164
StaticBuffer()=default
Creates a new buffer object.
const T & operator[](const size_t index) const
Returns one element of this buffer.
Definition: StaticBuffer.h:293
bool operator==(const StaticBuffer< T, tCapacity > &second) const
Returns whether two buffers are identical.
Definition: StaticBuffer.h:313
T & operator[](const size_t index)
Returns one element of this buffer.
Definition: StaticBuffer.h:303
StaticBuffer(const std::vector< T > &values)
Creates a new buffer object.
Definition: StaticBuffer.h:206
StaticBuffer(const T *buffer)
Creates a new buffer object.
Definition: StaticBuffer.h:180
void clear()
Clears all elements of this buffer.
Definition: StaticBuffer.h:252
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15