1

implement simple array wrapper

This commit is contained in:
2022-07-18 21:55:24 +02:00
parent 2e4c0339a3
commit f5a4a66207

42
c_os/user/lib/Array.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef __ARRAY_INCLUDE_H
#define __ARRAY_INCLUDE_H
#include "user/lib/Iterator.h"
#include <cstddef>
#include <utility>
template<typename T, const std::size_t N, typename I = Iterator<T>>
class Array {
public:
using Type = T;
using Iterator = I;
private:
Type buf[N];
public:
Iterator begin() { return Iterator(&buf[0]); }
Iterator end() { return Iterator(&buf[N]); }
constexpr Iterator begin() const { return this->begin(); }
constexpr Iterator end() const { return this->end(); }
constexpr unsigned int size() const {
return N;
}
T& operator[](std::size_t i) {
return this->buf[i];
}
constexpr const T& operator[](std::size_t i) const {
return this->buf[i];
}
void swap(Array<Type, N>& other) {
for (std::size_t i = 0; i < N; ++i) {
std::swap(this->buf[i], other[i]);
}
}
};
#endif