-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBitIntMember.h
More file actions
94 lines (78 loc) · 1.81 KB
/
BitIntMember.h
File metadata and controls
94 lines (78 loc) · 1.81 KB
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
/*
*
* BitIntMember is an auxiliary type that allows to make BigInt array-accessible and iterable
*
* @author Valeriy V Dmitriev aka valmat <ufabiz@gmail.com>, http://valmat.ru/
* @licenses MIT https://opensource.org/licenses/MIT
* @repo https://github.com/valmat/LedMatrix
*
*/
#pragma once
// Forward declaration
template<typename T, uint8_t _size>
class BitInt;
template<typename T, uint8_t _size>
class BitIntMember
{
public:
BitIntMember(uint8_t index, BitInt<T, _size> &val) :
_val(val),
_index(index)
{}
BitIntMember& operator=(bool state)
{
_val.set(_index, state);
return *this;
}
operator bool () const
{
return _val.get(_index);
}
//
// Iterator operators
//
// Increment position (pre-increment)
BitIntMember &operator++()
{
++_index;
return *this;
}
// Increment position (post-increment)
BitIntMember operator++(int)
{
BitIntMember copy(*this);
++(*this);
return copy;
}
// Decrement position (pre-decrement)
BitIntMember &operator--()
{
--_index;
return *this;
}
// Increment position (post-decrement)
BitIntMember operator--(int)
{
BitIntMember copy(*this);
--(*this);
return copy;
}
// Compare with other iterator
bool operator==(const BitIntMember<T, _size> &rhs) const
{
return (_val == rhs._val && _index == rhs._index);
}
// Compare with other iterator
bool operator!=(const BitIntMember<T, _size> &rhs) const
{
return !(*this == rhs);
}
// Dereference as a current object
bool operator*() const
{
return _val.get(_index);
}
private:
BitInt<T, _size> &_val;
uint8_t _index;
};