123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #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;
- }
- // returns true on error
- template<int L>
- check_return bool toString(ArrayString<L>& s) const {
- if(s.append("[")) {
- return true;
- }
- for(int i = 0; i < N - 1; i++) {
- if(s.append(data[i]) || s.append(", ")) {
- return true;
- }
- }
- if(N > 0 && s.append(data[N - 1])) {
- return true;
- }
- return s.append("]");
- }
- };
- }
- #endif
|