EEPROMEx  1.0.0
EEPROMEX is an extension of the standard Arduino EEPROM library for reading and writing basic types, structs, strings, arrays and more.
D:/My Documents/Github/Arduino/Own Arduino libraries/EEPROMEx/Arduino-EEPROMEx/EEPROMVar.h
00001 #include <EEPROMex.h>
00002 
00003 /*
00004   EEPROMvar.h - EEPROM variable library
00005   Copyright (c) 2012 Thijs Elenbaas.  All right reserved.
00006   
00007   based on class by AlphaBeta
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 */
00023 
00024 template<typename T> class EEPROMVar 
00025 {
00026     public:
00027       EEPROMVar(T init) {
00028         address = EEPROM.getAddress(sizeof(T)); 
00029         var = init;
00030       }
00031       operator T () { 
00032         return var; 
00033       }
00034       EEPROMVar &operator=(T val) {
00035         var = val;
00036         return *this;
00037       }
00038       
00039       void operator+=(T val) {
00040         var += T(val); 
00041       }
00042       void operator-=(T val) {
00043         var -= T(val); 
00044       }       
00045       void operator++(int) {
00046         var += T(1); 
00047       }
00048       void operator--(int) {
00049         var -= T(1); 
00050       }
00051       void operator++() {
00052         var += T(1); 
00053       }
00054       void operator--() {
00055         var -= T(1); 
00056       }
00057       template<typename V>
00058         void operator /= (V divisor) {
00059         var = var / divisor;
00060       }
00061       template<typename V>
00062         void operator *= (V multiplicator) {
00063         var = var * multiplicator;
00064       }
00065       void save(){         
00066         EEPROM.writeBlock<T>(address, var);
00067       }
00068       
00069       void update(){           
00070         EEPROM.updateBlock<T>(address, var);
00071       }
00072       
00073       int getAddress(){        
00074         return address;
00075       }
00076       
00077       void restore(){
00078         EEPROM.readBlock<T>(address, var);
00079       }
00080     protected:  
00081       T var;
00082       int address;
00083 };