35
35
36
36
struct EERef {
37
37
38
- EERef ( const int index )
39
- : index( index ) {}
38
+ template < typename T > EERef ( T *ptr ) : index( ( int ) ptr ) {}
39
+ EERef ( const int index ) : index( index ) {}
40
40
41
41
// Access/read members.
42
42
uint8_t operator *() const { return eeprom_read_byte ( (uint8_t *) index ); }
43
- operator const uint8_t () const { return **this ; }
43
+ operator uint8_t () const { return **this ; }
44
44
45
45
// Assignment/write members.
46
46
EERef &operator =( const EERef &ref ) { return *this = *ref; }
@@ -86,15 +86,18 @@ struct EERef{
86
86
87
87
struct EEPtr {
88
88
89
- EEPtr ( const int index )
90
- : index( index ) {}
89
+ template < typename T > EEPtr ( T *ptr ) : index( ( int ) ptr ) {}
90
+ EEPtr ( const int index ) : index( index ) {}
91
91
92
- operator const int () const { return index ; }
92
+ operator int () const { return index ; }
93
93
EEPtr &operator =( int in ) { return index = in, *this ; }
94
+ EERef operator []( int idx ) { return index + idx; }
94
95
95
96
// Iterator functionality.
96
97
bool operator !=( const EEPtr &ptr ) { return index != ptr.index ; }
97
- EERef operator *() { return index ; }
98
+
99
+ // Dreference & member access.
100
+ EERef operator *() { return index ; }
98
101
99
102
/* * Prefix & Postfix increment/decrement **/
100
103
EEPtr& operator ++() { return ++index , *this ; }
@@ -115,31 +118,29 @@ struct EEPtr{
115
118
116
119
struct EEPROMClass {
117
120
118
- // Basic user access methods.
119
- EERef operator []( const int idx ) { return idx ; }
120
- uint8_t read ( int idx ) { return EERef ( idx ) ; }
121
- void write ( int idx , uint8_t val ) { ( EERef ( idx )) = val; }
122
- void update ( int idx , uint8_t val ) { EERef ( idx ) .update ( val ); }
121
+ // Basic user access methods.
122
+ EERef operator []( EERef ref ) { return ref ; }
123
+ EERef read ( EERef ref ) { return ref ; }
124
+ void write ( EERef ref , uint8_t val ) { ref = val; }
125
+ void update ( EERef ref , uint8_t val ) { ref .update ( val ); }
123
126
124
127
// STL and C++11 iteration capability.
125
128
EEPtr begin () { return 0x00 ; }
126
129
EEPtr end () { return length (); } // Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
127
130
uint16_t length () { return E2END + 1 ; }
128
131
129
132
// Functionality to 'get' and 'put' objects to and from EEPROM.
130
- template < typename T > T &get ( int idx, T &t ){
131
- EEPtr e = idx;
132
- uint8_t *ptr = (uint8_t *) &t;
133
- for ( int count = sizeof (T) ; count ; --count, ++e ) *ptr++ = *e;
134
- return t;
135
- }
133
+ template < typename T > T &get ( EEPtr ptr, T &t ){
134
+ uint8_t *dest = (uint8_t *) &t;
135
+ for ( int count = sizeof (T) ; count ; --count, ++ptr ) *dest++ = *ptr;
136
+ return t;
137
+ }
136
138
137
- template < typename T > const T &put ( int idx, const T &t ){
138
- EEPtr e = idx;
139
- const uint8_t *ptr = (const uint8_t *) &t;
140
- for ( int count = sizeof (T) ; count ; --count, ++e ) (*e).update ( *ptr++ );
141
- return t;
142
- }
139
+ template < typename T > const T &put ( EEPtr ptr, const T &t ){
140
+ const uint8_t *src = (const uint8_t *) &t;
141
+ for ( int count = sizeof (T) ; count ; --count, ++ptr ) (*ptr).update ( *src++ );
142
+ return t;
143
+ }
143
144
};
144
145
145
146
static EEPROMClass EEPROM;
0 commit comments