FastUIDraw
c_array.hpp
Go to the documentation of this file.
1 /*!
2  * \file c_array.hpp
3  * \brief file c_array.hpp
4  *
5  * Adapted from: c_array.hpp of WRATH:
6  *
7  * Copyright 2013 by Nomovok Ltd.
8  * Contact: info@nomovok.com
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@nomovok.com>
16  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
17  *
18  */
19 
20 
21 #ifndef FASTUIDRAW_C_ARRAY_HPP
22 #define FASTUIDRAW_C_ARRAY_HPP
23 
24 #include <fastuidraw/util/util.hpp>
25 #include <fastuidraw/util/vecN.hpp>
26 
27 namespace fastuidraw
28 {
29 
30 /*!\addtogroup Utility
31  * @{
32  */
33 
34 /*!
35  * \brief
36  * A c_array is a wrapper over a
37  * C pointer with a size parameter
38  * to facilitate bounds checking
39  * and provide an STL-like iterator
40  * interface.
41  */
42 template<typename T>
43 class c_array
44 {
45 public:
46 
47  /*!
48  * \brief
49  * STL compliant typedef
50  */
51  typedef T* pointer;
52 
53  /*!
54  * \brief
55  * STL compliant typedef; notice that const_pointer
56  * is type T* and not const T*. This is because
57  * a c_array is just a HOLDER of a pointer and
58  * a length and thus the contents of the value
59  * behind the pointer are not part of the value
60  * of a c_array.
61  */
62  typedef T* const_pointer;
63 
64  /*!
65  * \brief
66  * STL compliant typedef
67  */
68  typedef T& reference;
69 
70  /*!
71  * \brief
72  * STL compliant typedef; notice that const_pointer
73  * is type T& and not const T&. This is because
74  * a c_array is just a HOLDER of a pointer and
75  * a length and thus the contents of the value
76  * behind the pointer are not part of the value
77  * of a c_array.
78  */
79  typedef T& const_reference;
80 
81  /*!
82  * \brief
83  * STL compliant typedef
84  */
85  typedef T value_type;
86 
87  /*!
88  * \brief
89  * STL compliant typedef
90  */
91  typedef size_t size_type;
92 
93  /*!
94  * \brief
95  * STL compliant typedef
96  */
97  typedef ptrdiff_t difference_type;
98 
99  /*!
100  * \brief
101  * iterator typedef to pointer
102  */
103  typedef pointer iterator;
104 
105  /*!
106  * \brief
107  * iterator typedef to const_pointer
108  */
109  typedef const_pointer const_iterator;
110 
111  /*!
112  * Default ctor, initializing the pointer as nullptr
113  * with size 0.
114  */
115  c_array(void):
116  m_size(0),
117  m_ptr(nullptr)
118  {}
119 
120  /*!
121  * Ctor initializing the pointer and size
122  * \param pptr pointer value
123  * \param sz size, must be no more than the number of elements that pptr points to.
124  */
125  template<typename U>
126  c_array(U *pptr, size_type sz):
127  m_size(sz),
128  m_ptr(pptr)
129  {
130  FASTUIDRAWstatic_assert(sizeof(U) == sizeof(T));
131  }
132 
133  /*!
134  * Ctor from a vecN, size is the size of the fixed size array
135  * \param pptr fixed size array that c_array references, must be
136  * in scope as until c_array is changed
137  */
138  template<typename U, size_type N>
140  m_size(N),
141  m_ptr(pptr.c_ptr())
142  {
143  FASTUIDRAWstatic_assert(sizeof(U) == sizeof(T));
144  }
145 
146  /*!
147  * Ctor from a vecN, size is the size of the fixed size array
148  * \param pptr fixed size array that c_array references, must be
149  * in scope as until c_array is changed
150  */
151  template<typename U, size_type N>
152  c_array(const vecN<U, N> &pptr):
153  m_size(N),
154  m_ptr(pptr.c_ptr())
155  {
156  FASTUIDRAWstatic_assert(sizeof(U) == sizeof(T));
157  }
158 
159  /*!
160  * Ctor from another c_array object.
161  * \tparam U type U* must be convertible to type T* AND
162  * the size of U must be the same as the size
163  * of T
164  * \param obj value from which to copy
165  */
166  template<typename U>
167  c_array(const c_array<U> &obj):
168  m_size(obj.m_size),
169  m_ptr(obj.m_ptr)
170  {
171  FASTUIDRAWstatic_assert(sizeof(U) == sizeof(T));
172  }
173 
174  /*!
175  * Ctor from a range of pointers.
176  * \param R R.m_begin will be the pointer and R.m_end - R.m_begin the size.
177  */
179  m_size(R.m_end - R.m_begin),
180  m_ptr((m_size > 0) ? &*R.m_begin : nullptr)
181  {}
182 
183  /*!
184  * Resets the \ref c_array object to be equivalent to
185  * a nullptr, i.e. c_ptr() will return nullptr and size()
186  * will return 0.
187  */
188  void
189  reset(void)
190  {
191  m_size = 0;
192  m_ptr = nullptr;
193  }
194 
195  /*!
196  * Reinterpret style cast for c_array. It is required
197  * that the sizeof(T)*size() evenly divides sizeof(S).
198  * \tparam S type to which to be reinterpreted casted
199  */
200  template<typename S>
201  c_array<S>
203  {
204  S *ptr;
205  size_type num_bytes(size() * sizeof(T));
206  FASTUIDRAWassert(num_bytes % sizeof(S) == 0);
207  ptr = reinterpret_cast<S*>(c_ptr());
208  return c_array<S>(ptr, num_bytes / sizeof(S));
209  }
210 
211  /*!
212  * Const style cast for c_array. It is required
213  * that the sizeof(T) is the same as sizeof(S).
214  * \tparam S type to which to be const casted
215  */
216  template<typename S>
217  c_array<S>
218  const_cast_pointer(void) const
219  {
220  S *ptr;
221  FASTUIDRAWstatic_assert(sizeof(S) == sizeof(T));
222  ptr = const_cast<S*>(c_ptr());
223  return c_array<S>(ptr, m_size);
224  }
225 
226  /*!
227  * For when T is vecN<S, N> for a type S, flattens the c_array<T>
228  * into a c_array<S> refering to the same data.
229  */
231  flatten_array(void) const
232  {
233  typename unvecN<T>::type *p;
234  p = reinterpret_cast<typename unvecN<T>::type*>(c_ptr());
236  }
237 
238  /*!
239  * Pointer of the c_array.
240  */
241  T*
242  c_ptr(void) const
243  {
244  return m_ptr;
245  }
246 
247  /*!
248  * Pointer to the element one past
249  * the last element of the c_array.
250  */
251  T*
252  end_c_ptr(void) const
253  {
254  return m_ptr + m_size;
255  }
256 
257  /*!
258  * Access named element of c_array, under
259  * debug build also performs bounds checking.
260  * \param j index
261  */
262  reference
263  operator[](size_type j) const
264  {
265  FASTUIDRAWassert(c_ptr() != nullptr);
266  FASTUIDRAWassert(j < m_size);
267  return c_ptr()[j];
268  }
269 
270  /*!
271  * STL compliant function.
272  */
273  bool
274  empty(void) const
275  {
276  return m_size == 0;
277  }
278 
279  /*!
280  * STL compliant function.
281  */
282  size_type
283  size(void) const
284  {
285  return m_size;
286  }
287 
288  /*!
289  * STL compliant function.
290  */
291  iterator
292  begin(void) const
293  {
294  return iterator(c_ptr());
295  }
296 
297  /*!
298  * STL compliant function.
299  */
300  iterator
301  end(void) const
302  {
303  return iterator(c_ptr() + static_cast<difference_type>(size()));
304  }
305 
306  /*!
307  * Returns the range of the c_array as an
308  * iterator range.
309  */
311  range(void) const
312  {
313  return range_type<iterator>(begin(), end());
314  }
315 
316  /*!
317  * Equivalent to
318  * \code
319  * operator[](size()-1-I)
320  * \endcode
321  * \param I index from the back to retrieve, I=0
322  * corrseponds to the back of the array.
323  */
324  reference
325  back(size_type I) const
326  {
327  FASTUIDRAWassert(I < size());
328  return (*this)[size() - 1 - I];
329  }
330 
331  /*!
332  * STL compliant function.
333  */
334  reference
335  back(void) const
336  {
337  return (*this)[size() - 1];
338  }
339 
340  /*!
341  * STL compliant function.
342  */
343  reference
344  front(void) const
345  {
346  return (*this)[0];
347  }
348 
349  /*!
350  * Returns a sub-array
351  * \param pos position of returned sub-array to start,
352  * i.e. returned c_array's c_ptr() will return
353  * this->c_ptr()+pos. It is an error if pos
354  * is negative.
355  * \param length length of sub array to return, note
356  * that it is an error if length+pos>size()
357  * or if length is negative.
358  */
359  c_array
360  sub_array(size_type pos, size_type length) const
361  {
362  FASTUIDRAWassert(pos + length <= m_size);
363  return c_array(m_ptr + pos, length);
364  }
365 
366  /*!
367  * Returns a sub-array, equivalent to
368  * \code
369  * sub_array(pos, size() - pos)
370  * \endcode
371  * \param pos position of returned sub-array to start,
372  * i.e. returned c_array's c_ptr() will return
373  * this->c_ptr() + pos. It is an error is pos
374  * is negative.
375  */
376  c_array
377  sub_array(size_type pos) const
378  {
379  FASTUIDRAWassert(pos <= m_size);
380  return c_array(m_ptr + pos, m_size - pos);
381  }
382 
383  /*!
384  * Returns a sub-array, equivalent to
385  * \code
386  * sub_array(R.m_begin, R.m_end - R.m_begin)
387  * \endcode
388  * \tparam I type convertible to size_type
389  * \param R range of returned sub-array
390  */
391  template<typename I>
392  c_array
394  {
395  return sub_array(R.m_begin, R.m_end - R.m_begin);
396  }
397 
398  /*!
399  * Provided as a conveniance, equivalent to
400  * \code
401  * *this = this->sub_array(0, size() - 1);
402  * \endcode
403  * It is an error to call this when size() is 0.
404  */
405  void
406  pop_back(void)
407  {
408  FASTUIDRAWassert(m_size > 0);
409  --m_size;
410  }
411 
412  /*!
413  * Provided as a conveniance, equivalent to
414  * \code
415  * *this = this->sub_array(1, size() - 1);
416  * \endcode
417  * It is an error to call this when size() is 0.
418  */
419  void
420  pop_front(void)
421  {
422  FASTUIDRAWassert(m_size > 0);
423  --m_size;
424  ++m_ptr;
425  }
426 
427  /*!
428  * Returns true if and only if the passed
429  * the c_array references exactly the same
430  * data as this c_array.
431  * \param rhs c_array to which to compare
432  */
433  template<typename U>
434  bool
435  same_data(const c_array<U> &rhs) const
436  {
437  return m_ptr == rhs.m_ptr
438  && m_size * sizeof(T) == rhs.m_size * sizeof(U);
439  }
440 
441 private:
442  template<typename>
443  friend class c_array;
444 
445  size_type m_size;
446  T *m_ptr;
447 };
448 
449 /*!
450  * Convert an array of 32-bit floating point values
451  * to an array of 16-bit half values
452  * \param src fp32 value to convert
453  * \param dst location to which to write fp16 values
454  */
455 void
457 
458 /*!
459  * Convert an array of 16-bit floating point values
460  * to an array of 32-bit half values
461  * \param src fp16 value to convert
462  * \param dst location to which to write fp32 values
463  */
464 void
466 
467 /*!
468  * Conveniance function to pack a single \ref vec2
469  * into an fp16-pair returned as a uint32_t.
470  * \param src float-pair to pack as an fp16-pair.
471  */
472 inline
473 uint32_t
475 {
476  uint32_t return_value;
477  uint16_t *ptr(reinterpret_cast<uint16_t*>(&return_value));
478  convert_to_fp16(src, c_array<uint16_t>(ptr, 2));
479  return return_value;
480 }
481 
482 /*!
483  * Provided as a conveniance, equivalent to
484  * \code
485  * pack_as_fp16(vec2(x, y));
486  * \endcode
487  */
488 inline
489 uint32_t
490 pack_as_fp16(float x, float y)
491 {
492  return pack_as_fp16(vec2(x, y));
493 }
494 
495 /*! @} */
496 
497 } //namespace
498 
499 #endif
T & const_reference
STL compliant typedef; notice that const_pointer is type T& and not const T&. This is because a c_arr...
Definition: c_array.hpp:79
iterator end(void) const
Definition: c_array.hpp:301
reference front(void) const
Definition: c_array.hpp:344
T * const_pointer
STL compliant typedef; notice that const_pointer is type T* and not const T*. This is because a c_arr...
Definition: c_array.hpp:62
T * pointer
STL compliant typedef.
Definition: c_array.hpp:51
void reset(void)
Definition: c_array.hpp:189
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
uint32_t pack_as_fp16(vec2 src)
Definition: c_array.hpp:474
vecN is a simple static array class with no virtual functions and no memory overhead. Supports runtim array index checking and STL style iterators via pointer iterators.
Definition: vecN.hpp:42
T value_type
STL compliant typedef.
Definition: c_array.hpp:85
bool empty(void) const
Definition: c_array.hpp:274
bool same_data(const c_array< U > &rhs) const
Definition: c_array.hpp:435
c_array< S > const_cast_pointer(void) const
Definition: c_array.hpp:218
size_type size(void) const
Definition: c_array.hpp:283
c_array sub_array(range_type< I > R) const
Definition: c_array.hpp:393
reference back(void) const
Definition: c_array.hpp:335
c_array(const vecN< U, N > &pptr)
Definition: c_array.hpp:152
T * end_c_ptr(void) const
Definition: c_array.hpp:252
iterator begin(void) const
Definition: c_array.hpp:292
vecN< float, 2 > vec2
Definition: vecN.hpp:1231
c_array(range_type< iterator > R)
Definition: c_array.hpp:178
#define FASTUIDRAWstatic_assert(X)
Definition: util.hpp:118
c_array sub_array(size_type pos, size_type length) const
Definition: c_array.hpp:360
c_array< typename unvecN< T >::type > flatten_array(void) const
Definition: c_array.hpp:231
T & reference
STL compliant typedef.
Definition: c_array.hpp:68
size_t size_type
STL compliant typedef.
Definition: c_array.hpp:91
void convert_to_fp16(c_array< const float > src, c_array< uint16_t > dst)
file util.hpp
c_array< S > reinterpret_pointer(void) const
Definition: c_array.hpp:202
c_array(vecN< U, N > &pptr)
Definition: c_array.hpp:139
const_pointer const_iterator
iterator typedef to const_pointer
Definition: c_array.hpp:109
range_type< iterator > range(void) const
Definition: c_array.hpp:311
reference back(size_type I) const
Definition: c_array.hpp:325
A c_array is a wrapper over a C pointer with a size parameter to facilitate bounds checking and provi...
Definition: c_array.hpp:43
void pop_front(void)
Definition: c_array.hpp:420
file vecN.hpp
T * c_ptr(void) const
Definition: c_array.hpp:242
ptrdiff_t difference_type
STL compliant typedef.
Definition: c_array.hpp:97
pointer iterator
iterator typedef to pointer
Definition: c_array.hpp:103
A class reprenting the STL range [m_begin, m_end).
Definition: util.hpp:392
void convert_to_fp32(c_array< const uint16_t > src, c_array< float > dst)
c_array(U *pptr, size_type sz)
Definition: c_array.hpp:126
c_array(const c_array< U > &obj)
Definition: c_array.hpp:167
reference operator[](size_type j) const
Definition: c_array.hpp:263
#define FASTUIDRAWassert(X)
Definition: util.hpp:99
c_array sub_array(size_type pos) const
Definition: c_array.hpp:377
void pop_back(void)
Definition: c_array.hpp:406