#ifndef FIXED_ARRAY_H__ #define FIXED_ARRAY_H__ #include "Common.h" namespace Arrays { template struct FixedArrayBase { FixedArrayBase(const size_type sz) : arr_(new T[sz]) {} ~FixedArrayBase() { delete[] arr_; } void fill_from(const FixedArrayBase& other, const size_type sz) { for (size_type i=0; i < sz; ++i) { arr_[i] = other.arr_[i]; } } T* arr_; private: // Avoid copies FixedArrayBase(const FixedArrayBase&); FixedArrayBase& operator=(const FixedArrayBase&); }; template class FixedArray : private FixedArrayBase { public: typedef T value_type; typedef T* iterator; typedef const T* const_iterator; typedef T& reference; typedef const T& const_reference; FixedArray() : FixedArrayBase(N) {} FixedArray(const FixedArray& other) : FixedArrayBase(N) { fill_from(other, size()); } FixedArray& operator=(const FixedArray& other) { FixedArray temp(other); std::swap(arr_, temp.arr_); return *this; } iterator begin() { return arr_; } iterator end() { return &arr_[size()]; } const_iterator begin() const { return const_cast(*this).begin(); } const_iterator end() const { return const_cast(*this).end(); } size_type size() const { return N; } // Unchecked references reference operator[] (const size_type n) { return arr_[n]; } const_reference operator[] (const size_type n) const { return const_cast(*this)[n]; } // Checked references reference at(const size_type pos) { if (pos >= size()) throw OutOfRange(); return (*this)[pos]; } const_reference at(const size_type pos) const { return const_cast(*this).at(pos); } }; } #endif