Array.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef ANKI_PtrSizeTIL_ARRAY_H
  2. #define ANKI_PtrSizeTIL_ARRAY_H
  3. #include "anki/util/Assert.h"
  4. #include "anki/util/StdTypes.h"
  5. namespace anki {
  6. /// @addtogroup util
  7. /// @{
  8. /// @addtogroup containers
  9. /// @{
  10. /// Like std::array but with some additions
  11. template<typename T, PtrSize N>
  12. struct Array
  13. {
  14. typedef T Value;
  15. typedef Value* Iterator;
  16. typedef const Value* ConstIterator;
  17. typedef Value& Reference;
  18. typedef const Value& ConstReference;
  19. // std compatible
  20. typedef Iterator iterator;
  21. typedef ConstIterator const_iterator;
  22. typedef Reference reference;
  23. typedef ConstReference const_reference;
  24. Value data[N];
  25. Reference operator[](const PtrSize n)
  26. {
  27. ANKI_ASSERT(n < N);
  28. return data[n];
  29. }
  30. ConstReference operator[](const PtrSize n) const
  31. {
  32. ANKI_ASSERT(n < N);
  33. return data[n];
  34. }
  35. /// Make it compatible with the C++11 range based for loop
  36. Iterator begin()
  37. {
  38. return &data[0];
  39. }
  40. /// Make it compatible with the C++11 range based for loop
  41. ConstIterator begin() const
  42. {
  43. return &data[0];
  44. }
  45. /// Make it compatible with the C++11 range based for loop
  46. Iterator end()
  47. {
  48. return &data[0] + N;
  49. }
  50. /// Make it compatible with the C++11 range based for loop
  51. ConstIterator end() const
  52. {
  53. return &data[0] + N;
  54. }
  55. /// Make it compatible with STL
  56. Reference front()
  57. {
  58. return data[0];
  59. }
  60. /// Make it compatible with STL
  61. ConstReference front() const
  62. {
  63. return data[0];
  64. }
  65. /// Make it compatible with STL
  66. Reference back()
  67. {
  68. return data[N - 1];
  69. }
  70. /// Make it compatible with STL
  71. ConstReference back() const
  72. {
  73. return data[N - 1];
  74. }
  75. static constexpr PtrSize getSize()
  76. {
  77. return N;
  78. }
  79. /// Make it compatible with STL
  80. static constexpr PtrSize size()
  81. {
  82. return N;
  83. }
  84. };
  85. /// 2D Array. @code Array2d<X, 10, 2> a; @endcode is equivelent to
  86. /// @code X a[10][2];
  87. template<typename T, PtrSize I, PtrSize J>
  88. using Array2d = Array<Array<T, J>, I>;
  89. /// @}
  90. /// @}
  91. } // end namespace anki
  92. #endif