Mech-Eye API Reference 2.1.4-alpha
API reference documentation for Mech-Eye 3D Laser Profiler
All Classes Functions Variables Enumerations Enumerator Pages
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
47 const ElementData* data() const { return _pData.get(); }
48
53 ElementData* data()
54 {
55 return const_cast<ElementData*>(static_cast<const Array2D<ElementData>&>(*this).data());
56 }
57
63 const ElementData& operator[](std::size_t n) const
64 {
65 if (n >= _height * _width || !_pData)
66 throw std::out_of_range("invalid subscript");
67 ElementData* data = _pData.get();
68 return data[n];
69 }
70
76 ElementData& operator[](std::size_t n)
77 {
78 return const_cast<ElementData&>(static_cast<const Array2D<ElementData>&>(*this)[n]);
79 }
80
88 const ElementData& at(uint32_t row, uint32_t col) const
89 {
90 if (row >= _height || col >= _width || !_pData)
91 throw std::out_of_range("invalid subscript");
92 ElementData* data = _pData.get();
93 return data[row * _width + col];
94 }
95
103 ElementData& at(uint32_t row, uint32_t col)
104 {
105 return const_cast<ElementData&>(
106 static_cast<const Array2D<ElementData>&>(*this).at(row, col));
107 }
108
113 {
115 copy.resize(_width, _height);
116 memcpy(copy.data(), data(), _height * _width * sizeof(ElementData));
117 return copy;
118 }
125 void resize(size_t width, size_t height)
126 {
127 if(width == 0 || height == 0)
128 return release();
129
130 if(_width == width && _height == height)
131 return ;
132
133 _width = width;
134 _height = height;
135 _pData.reset(new ElementData[_width * _height], [](ElementData* p) { delete[] p; });
136 }
137
141 void release()
142 {
143 _pData.reset();
144 _width = 0;
145 _height = 0;
146 }
147
148private:
149 size_t _width;
150 size_t _height;
151 std::shared_ptr<ElementData> _pData;
152};
153
154} // namespace eye
155} // 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 an element reference to the specified index in the Array2D object using the operator [].
Definition Array2D.h:76
const ElementData & operator[](std::size_t n) const
Returns a const element reference to the specified index in the Array2D object using the operator [].
Definition Array2D.h:63
ElementData * data()
Returns the pointer to element in the Array2D object. The returned pointer will be invalidated after ...
Definition Array2D.h:53
const ElementData & at(uint32_t row, uint32_t col) const
Returns a const element reference to the specified row and column index in the Array2D object.
Definition Array2D.h:88
Array2D< ElementData > clone() const
Creates a deep copy of the Array2D object.
Definition Array2D.h:112
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:125
const ElementData * data() const
Returns the pointer to element in the Array2D object. The returned pointer will be invalidated after ...
Definition Array2D.h:47
Array2D()
Constructor.
Definition Array2D.h:22
ElementData & at(uint32_t row, uint32_t col)
Returns an element reference to the specified row and column index in the Array2D object.
Definition Array2D.h:103
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:141
~Array2D()
Destructor.
Definition Array2D.h:26