15template <
typename ElementData>
22 Array2D() : _width(0), _height(0), _pData(nullptr) {}
31 size_t width()
const {
return _width; }
36 size_t height()
const {
return _height; }
48 const ElementData*
data()
const {
return _pData.get(); }
68 if (n >= _height * _width || !_pData)
69 throw std::out_of_range(
"invalid subscript");
70 ElementData*
data = _pData.get();
93 const ElementData&
at(uint32_t row, uint32_t col)
const
95 if (row >= _height || col >= _width || !_pData)
96 throw std::out_of_range(
"invalid subscript");
97 ElementData*
data = _pData.get();
98 return data[row * _width + col];
109 ElementData&
at(uint32_t row, uint32_t col)
111 return const_cast<ElementData&
>(
121 copy.
resize(_width, _height);
122 memcpy(copy.
data(),
data(), _height * _width *
sizeof(ElementData));
141 _pData.reset(
new ElementData[_width * _height], [](ElementData* p) {
delete[] p; });
157 std::shared_ptr<ElementData> _pData;
Represents a 2D container of data.
Definition Array2D.h:17
void release()
Deallocates the data in the Array2D object.
Definition Array2D.h:147
const ElementData & at(uint32_t row, uint32_t col) const
Returns a reference to the constant element at the specified row and column in the Array2D object.
Definition Array2D.h:93
ElementData & operator[](std::size_t n)
Returns a reference to the constant element with the specified index in the Array2D object using the ...
Definition Array2D.h:80
const ElementData * data() const
Returns the pointer to an element in the Array2D object. The returned pointer will be invalidated aft...
Definition Array2D.h:48
Array2D< ElementData > clone() const
Creates a deep copy of the Array2D object.
Definition Array2D.h:118
ElementData * data()
Returns the pointer to an element in the Array2D object. The returned pointer will be invalidated aft...
Definition Array2D.h:55
ElementData & at(uint32_t row, uint32_t col)
Returns a reference to the element at the specified row and column in the Array2D object.
Definition Array2D.h:109
size_t width() const
Returns the width of the Array2D object.
Definition Array2D.h:31
const ElementData & operator[](std::size_t n) const
Returns a reference to the constant element with the specified index in the Array2D object using the ...
Definition Array2D.h:66
bool isEmpty() const
Returns true if the Array2D object has no elements.
Definition Array2D.h:41
size_t height() const
Returns the height of the Array2D object.
Definition Array2D.h:36
void resize(size_t width, size_t height)
Changes the size of the Array2D object. It destroys the existing data and reallocates memory accordin...
Definition Array2D.h:131
Array2D()
Constructor.
Definition Array2D.h:22
~Array2D()
Describes a destructor.
Definition Array2D.h:26