-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRowCol.h
133 lines (110 loc) · 2.74 KB
/
RowCol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
*
* RowCol is a generic name for the two classes. Row and Col.
* These classes are for convenient control the rows and columns
* of the LED matrix using thr LedMatrix class.
*
* @author Valeriy V Dmitriev aka valmat <[email protected]>, http://valmat.ru/
* @licenses MIT https://opensource.org/licenses/MIT
* @repo https://github.com/valmat/LedMatrix
*
*/
#pragma once
// Forward declaration
template<uint8_t maxRows, bool isRow>
struct RowColIterator;
template<uint8_t maxRows, bool isRow>
class RowCol {
public:
// Constructor
constexpr RowCol(uint8_t nom) :
// If out of matrix range
_valid(nom < maxRows),
_nom(_valid ? nom : 0)
{}
// Copy & Move constructors
constexpr RowCol( const RowCol& that) :
_valid(that._valid),
_nom(that._nom)
{}
constexpr RowCol ( RowCol&& that) :
_valid(that._valid),
_nom(that._nom)
{}
// if the Row/Col object is valid
constexpr bool isValid() const
{
return _valid;
}
// Cast to a number
constexpr operator uint8_t () const
{
return _nom;
}
// Cast to a ... RowCol
// Row -> Col, Col -> Row
template<bool B>
constexpr operator RowCol<maxRows, B> () const
{
return RowCol<maxRows, B>(_nom);
}
// Reverse operator
constexpr RowCol operator!() const
{
return maxRows - 1 - _nom;
}
//
// Iterator operators
// Folowing operators needs to RowColIterator
//
// Increment position (pre-increment)
RowCol &operator++()
{
++_nom;
return *this;
}
// Increment position (post-increment)
RowCol operator++(int)
{
RowCol copy(*this);
++(*this);
return copy;
}
// Decrement position (pre-decrement)
RowCol &operator--()
{
--_nom;
return *this;
}
// Increment position (post-decrement)
RowCol operator--(int)
{
RowCol copy(*this);
--(*this);
return copy;
}
// Compare with other iterator
constexpr bool operator==(const RowCol &rhs) const
{
return (_nom == rhs._nom);
}
// Compare with other iterator
constexpr bool operator!=(const RowCol &rhs) const
{
return (_nom != rhs._nom);
}
// Dereference as a current object
constexpr const RowCol &operator*() const
{
return *this;
}
private:
// Novalidateble constructor
constexpr RowCol(uint8_t nom, bool valid) : _valid(valid), _nom(nom) {}
const bool _valid = true;
uint8_t _nom;
template<uint8_t __maxRows, bool __isRow>
friend struct RowColIterator;
};
using Row = RowCol<8, true>;
using Col = RowCol<8, false>;