Skip to content

Commit db80e32

Browse files
committed
Improved EEPtr for better iteration. And standardised its behavior.
Taking the address of an EERef will now return an EEPtr as well.
1 parent a209440 commit db80e32

File tree

1 file changed

+13
-3
lines changed
  • hardware/arduino/avr/libraries/EEPROM/src

1 file changed

+13
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <avr/eeprom.h>
2626
#include <avr/io.h>
2727

28+
struct EEPtr; //Forward declaration for EERef::opreator&
29+
2830
/***
2931
EERef class.
3032
@@ -41,6 +43,8 @@ struct EERef{
4143
//Access/read members.
4244
uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); }
4345
operator uint8_t() const { return **this; }
46+
47+
EEPtr operator&() const; //Defined below EEPtr
4448

4549
//Assignment/write members.
4650
EERef &operator=( const EERef &ref ) { return *this = *ref; }
@@ -88,18 +92,22 @@ struct EEPtr{
8892

8993
template< typename T > EEPtr( T *ptr ) : index( (int) ptr ) {}
9094
EEPtr( const int index ) : index( index ) {}
91-
95+
96+
//Pointer read/write.
9297
operator int() const { return index; }
9398
EEPtr &operator=( int in ) { return index = in, *this; }
9499
EERef operator[]( int idx ) { return index + idx; }
95100

96101
//Iterator functionality.
97102
bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
103+
EEPtr& operator+=( int idx ) { return index += idx, *this; }
104+
EEPtr& operator-=( int idx ) { return index -= idx, *this; }
98105

99106
//Dreference & member access.
100107
EERef operator*() { return index; }
101-
102-
/** Prefix & Postfix increment/decrement **/
108+
EERef *operator->() { return (EERef*) this; }
109+
110+
// Prefix & Postfix increment/decrement
103111
EEPtr& operator++() { return ++index, *this; }
104112
EEPtr& operator--() { return --index, *this; }
105113
EEPtr operator++ (int) { return index++; }
@@ -108,6 +116,8 @@ struct EEPtr{
108116
int index; //Index of current EEPROM cell.
109117
};
110118

119+
inline EEPtr EERef::operator&() const { return index; } //Deferred definition till EEPtr becomes available.
120+
111121
/***
112122
EEPROMClass class.
113123

0 commit comments

Comments
 (0)