Mech-Eye API 2.4.0
API reference documentation for Mech-Eye Industrial 3D Camera
Loading...
Searching...
No Matches
Array2D.h
1#pragma once
2#include <cstdint>
3#include <cstring>
4#include <memory>
5#include <string>
6#include <stdexcept>
7
8namespace mmind {
9
10namespace eye {
11
15template <typename ElementData>
17{
18public:
22 Array2D() : _width(0), _height(0), _pData(nullptr) {}
27
31 size_t width() const { return _width; }
32
36 size_t height() const { return _height; }
37
41 bool isEmpty() const { return !_pData; }
42
48 const ElementData* data() const { return _pData.get(); }
49
56 {
57 return const_cast<ElementData*>(static_cast<const Array2D<ElementData>&>(*this).data());
58 }
59
66 const ElementData& operator[](std::size_t n) const
67 {
68 if (n >= _height * _width || !_pData)
69 throw std::out_of_range("invalid subscript");
70 ElementData* data = _pData.get();
71 return data[n];
72 }
73
80 ElementData& operator[](std::size_t n)
81 {
82 return const_cast<ElementData&>(static_cast<const Array2D<ElementData>&>(*this)[n]);
83 }
84
94 {
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];
99 }
100
110 {
111 return const_cast<ElementData&>(
112 static_cast<const Array2D<ElementData>&>(*this).at(row, col));
113 }
114
119 {
121 copy.resize(_width, _height);
122 memcpy(copy.data(), data(), _height * _width * sizeof(ElementData));
123 return copy;
124 }
131 void resize(size_t width, size_t height)
132 {
133 if (width == 0 || height == 0)
134 return release();
135
136 if (_width == width && _height == height)
137 return;
138
139 _width = width;
140 _height = height;
141 _pData.reset(new ElementData[_width * _height], [](ElementData* p) { delete[] p; });
142 }
143
147 void release()
148 {
149 _pData.reset();
150 _width = 0;
151 _height = 0;
152 }
153
154private:
155 size_t _width;
156 size_t _height;
157 std::shared_ptr<ElementData> _pData;
158};
159
160} // namespace eye
161} // namespace mmind
Represents a 2D container of data.
Definition Array2D.h:17
size_t height() const
Returns the height of the Array2D object.
Definition Array2D.h:36
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 & 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
ElementData * data()
Returns the pointer to an element in the Array2D object. The returned pointer will be invalidated aft...
Definition Array2D.h:55
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
Array2D< ElementData > clone() const
Creates a deep copy of the Array2D object.
Definition Array2D.h:118
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
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()
Constructor.
Definition Array2D.h:22
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
bool isEmpty() const
Returns true if the Array2D object has no elements.
Definition Array2D.h:41
void release()
Deallocates the data in the Array2D object.
Definition Array2D.h:147
~Array2D()
Describes a destructor.
Definition Array2D.h:26