EEPROM-Storage Library
The EEPROM Storage library provides the ability to access variables stored in EEPROM just as if they were stored in RAM.
 
Loading...
Searching...
No Matches
EEPROM-Cache.h
Go to the documentation of this file.
1// Copyright © 2017-2025 Daniel Porrey. All Rights Reserved.
2//
3// This file is part of the EEPROM-Storage library.
4//
5// EEPROM-Storage library is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// EEPROM-Storage library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with EEPROM-Storage library. If not,
17// see http://www.gnu.org/licenses/.
18//
19#pragma once
20#ifndef EEPROM_CACHE_H
21#define EEPROM_CACHE_H
22
28//
29// Cross-compatable with Arduino, GNU C++ for tests, and Particle.
30//
31#if defined(ARDUINO) && ARDUINO >= 100
32 #include <Arduino.h>
33 #include <EEPROM.h>
34#elif defined(PARTICLE)
35 #include <Particle.h>
36#endif
37
38#include "EEPROM-Base.h"
39
52template <typename T>
53class EEPROMCache : public EEPROMBase<T>
54{
55 public:
60 EEPROMCache(const uint address) : EEPROMBase<T>(address)
61 {
62 //
63 // Read the current value from EEPROM.
64 //
65 this->restore();
66 }
67
73 EEPROMCache(const uint address, T value) : EEPROMBase<T>(address, value)
74 {
75 this->_value = value;
76 }
77
85 EEPROMCache<T>& operator = (T const& value)
86 {
87 this->set(value);
88 return *this;
89 }
90
98 {
99 this->set(item.get());
100 return *this;
101 }
102
107 T get() const
108 {
109 return this->_value;
110 }
111
117 T set(T const& value)
118 {
119 this->_value = value;
120 return this->_value;
121 }
122
128 {
129 //
130 // Read the current value from EEPROM.
131 //
132 this->_value = this->read();
133 return this->_value;
134 }
135
141 {
142 this->write(this->_value);
143 return this->_value;
144 }
145
146 protected:
148};
149#endif
This file contains the EEPROMBase<T> definition.
Base class to wrap an EEPROM variable.
Definition EEPROM-Base.h:54
void write(T const &value) const
Write the value to the EEPROM using the address in this instance.
T read() const
Read the variable value from the EEPROM using the address in this variable.
Provides indirect access to an EEPROM variable.
T set(T const &value)
Set the variable value.
T commit()
Commit the cached value to the EEPROM.
T get() const
Get the variable value.
EEPROMCache(const uint address, T value)
Initialize an instance of EEPROMBase with the specified address and initial value.
EEPROMCache< T > & operator=(T const &value)
Allows assignment of a variable of type T value to be this instance's value.
EEPROMCache(const uint address)
Initialize an instance of EEPROMCache<T> with the specified address.
T _value
The cached value of the EEPROM variable.
T restore()
Restores the cached value by reading from EEPROM.