From \Arduino\hardware\arduino\avr\libraries\EEPROM\EEPROM.h:
//Functionality to 'get' and 'put' objects to and from EEPROM.
template< typename T > T &get( int idx, T &t ){
EEPtr e = idx;
uint8_t *ptr = (uint8_t*) &t;
for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e;
return t;
}
In the examples , EEPROM.get(address, object ) , they show you can read a structure from EEPROM.
You set an address to read, but where do you say whats the length ??? is it the object sizeOf ?
What if I want to read a word at address 100 , in the length of 15 ? do I pass an array as object ?
EEPROM.get(address, data[15] ) ?
EEPROM.get() is implemented as a template function.
//Functionality to 'get' and 'put' objects to and from EEPROM.
template< typename T > T &get( int idx, T &t ){
EEPtr e = idx;
uint8_t *ptr = (uint8_t*) &t;
for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e;
return t;
}
template< typename T > const T &put( int idx, const T &t ){
EEPtr e = idx;
const uint8_t *ptr = (const uint8_t*) &t;
for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ );
return t;
}
For operations that will have the same code no matter what type you are passing to them, templates are a good way to make them work. This is known as generic programming. In this case, when you use this function with your custom struct, the compiler will deduce what the template parameter T is supposed to be, and make a version of the function with T replaced as your struct. It will also have the correct return type. put() uses the exact same pattern in reverse.
This first stores two words in eeprom from an array of words and next reads each of them back into a character array. It reads back the sizeof(data) number of bytes (as shown by the HEX output).
Next it modifies the first word in the array and reads both words back again; this to show that only the first 6 bytes (of the 15 in the first element of the array of words) are modified (the data that is read back still partially contains the original number of the first word).