1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #ifndef CORE_ARRAY_H
- #define CORE_ARRAY_H
- #include "utils/ArrayString.h"
- namespace Core {
- template<typename T, int N>
- class Array final {
- static_assert(N > 0, "Array size must be positive");
- T data[static_cast<unsigned int>(N)];
- public:
- Array() = default;
- Array(const T& t) {
- fill(t);
- }
- void fill(const T& t) {
- for(int i = 0; i < N; i++) {
- data[i] = t;
- }
- }
- T& operator[](int index) {
- return data[index];
- }
- const T& operator[](int index) const {
- return data[index];
- }
- T* begin() {
- return data;
- }
- T* end() {
- return data + N;
- }
- const T* begin() const {
- return data;
- }
- const T* end() const {
- return data + N;
- }
- constexpr int getLength() const {
- return N;
- }
- template<int L>
- check_return Error toString(ArrayString<L>& s) const {
- CORE_RETURN_ERROR(s.append("["));
- for(int i = 0; i < N - 1; i++) {
- CORE_RETURN_ERROR(s.append(data[i]));
- CORE_RETURN_ERROR(s.append(", "));
- }
- if(N > 0) {
- CORE_RETURN_ERROR(s.append(data[N - 1]));
- }
- return s.append("]");
- }
- };
- }
- #endif
|