Skip to content

Commit b84f01f

Browse files
committed
Split EEBit into a pointer and reference type
1 parent 08f50d6 commit b84f01f

File tree

1 file changed

+27
-16
lines changed
  • hardware/arduino/avr/libraries/EEPROM/src

1 file changed

+27
-16
lines changed

hardware/arduino/avr/libraries/EEPROM/src/EEPROM.h

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,33 +101,41 @@ struct EERef{
101101
struct EEBit{
102102

103103
//Constructor, use by passing in index of EEPROM byte, then index of bit to read.
104-
EEBit( int index, uint8_t bidx )
105-
: ref( index ), mask( 0x01 << bidx ) {}
104+
EEBit( EEBitPtr ptr)
105+
: ptr( ptr ) {}
106106

107107
//Modifier functions.
108-
EEBit &setIndex( uint8_t bidx ) { return mask = (0x01 << bidx), *this; }
109108
EEBit &set() { return *this = true; }
110109
EEBit &clear() { return *this = false; }
111110

112111
//Read/write functions.
113-
operator bool() const { return ref & mask; }
114-
EEBit &operator =( const EEBit &copy ) { return *this = ( const bool ) copy; }
112+
operator bool() const { return *ptr.ref & ptr.mask; }
113+
EEBit &operator =( const EEBit &copy ) { return *this = ( bool ) copy; }
115114

116115
EEBit &operator =( const bool &copy ){
117-
if( copy ) ref |= mask;
118-
else ref &= ~mask;
116+
EERef cell = *ptr.ref;
117+
if( copy ) cell |= ptr.mask;
118+
else cell &= ~mask;
119119
return *this;
120120
}
121121

122+
const EEBitPtr ptr;
123+
}
124+
125+
struct EEBitPtr {
126+
//Constructor, use by passing in index of EEPROM byte, then index of bit to read.
127+
EEBitPtr( int index, uint8_t bidx )
128+
: ptr( index ), mask( 0x01 << bidx ) {}
129+
122130
//Iterator functionality.
123-
EEBit& operator*() { return *this; }
124-
bool operator==( const EEBit &bit ) { return (mask == bit.mask) && (ref.index == bit.ref.index); }
125-
bool operator!=( const EEBit &bit ) { return !(*this == bit); }
131+
EEBit operator*() const { return EEBit(*this); }
132+
bool operator==( const EEBitPtr &bit ) const { return (mask == bit.mask) && (ptr == bit.ptr); }
133+
bool operator!=( const EEBitPtr &bit ) const { return !(*this == bit); }
126134

127135
//Prefix & Postfix increment/decrement
128136
EEBit& operator++(){
129137
if( mask & 0x80 ){
130-
++ref.index;
138+
++ptr;
131139
mask = 0x01;
132140
}else{
133141
mask <<= 1;
@@ -137,7 +145,7 @@ struct EEBit{
137145

138146
EEBit& operator--(){
139147
if( mask & 0x01 ){
140-
--ref.index;
148+
--ptr;
141149
mask = 0x80;
142150
}else{
143151
mask >>= 1;
@@ -155,14 +163,17 @@ struct EEBit{
155163
return --(*this), cpy;
156164
}
157165

158-
EERef ref; //Reference to EEPROM cell.
166+
EEPtr ptr; //Reference to EEPROM cell.
159167
uint8_t mask; //Mask of bit to read/write.
160168
};
161169

170+
//Deferred definition till EEBitPtr becomes available.
171+
inline EEBitPtr EEBit::operator&() const { return ptr; }
172+
162173
//Deferred definition till EEBit becomes available.
163-
inline EEBit EERef::operator[]( const int bidx ) { return EEBit( index, bidx ); }
164-
inline EEBit EERef::begin() { return EEBit( index, 0 ); }
165-
inline EEBit EERef::end() { return EEBit( index + 1, 0 ); }
174+
inline EEBit EERef::operator[]( const int bidx ) { return *EEBitPtr( index, bidx ); }
175+
inline EEBitPtr EERef::begin() { return EEBitPtr( index, 0 ); }
176+
inline EEBitPtr EERef::end() { return EEBitPtr( index + 1, 0 ); }
166177

167178

168179
/***

0 commit comments

Comments
 (0)