ESPHome 2025.5.0
Loading...
Searching...
No Matches
preferences.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include <cstdint>
5
7
8namespace esphome {
9
11 public:
12 virtual bool save(const uint8_t *data, size_t len) = 0;
13 virtual bool load(uint8_t *data, size_t len) = 0;
14};
15
17 public:
20
21 template<typename T> bool save(const T *src) {
22 if (backend_ == nullptr)
23 return false;
24 return backend_->save(reinterpret_cast<const uint8_t *>(src), sizeof(T));
25 }
26
27 template<typename T> bool load(T *dest) {
28 if (backend_ == nullptr)
29 return false;
30 return backend_->load(reinterpret_cast<uint8_t *>(dest), sizeof(T));
31 }
32
33 protected:
35};
36
38 public:
39 virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash) = 0;
40 virtual ESPPreferenceObject make_preference(size_t length, uint32_t type) = 0;
41
47 virtual bool sync() = 0;
48
55 virtual bool reset() = 0;
56
57 template<typename T, enable_if_t<is_trivially_copyable<T>::value, bool> = true>
58 ESPPreferenceObject make_preference(uint32_t type, bool in_flash) {
59 return this->make_preference(sizeof(T), type, in_flash);
60 }
61
62 template<typename T, enable_if_t<is_trivially_copyable<T>::value, bool> = true>
64 return this->make_preference(sizeof(T), type);
65 }
66};
67
68extern ESPPreferences *global_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
69
70} // namespace esphome
virtual bool load(uint8_t *data, size_t len)=0
virtual bool save(const uint8_t *data, size_t len)=0
bool save(const T *src)
Definition preferences.h:21
ESPPreferenceObject(ESPPreferenceBackend *backend)
Definition preferences.h:19
ESPPreferenceBackend * backend_
Definition preferences.h:34
virtual bool sync()=0
Commit pending writes to flash.
ESPPreferenceObject make_preference(uint32_t type)
Definition preferences.h:63
virtual bool reset()=0
Forget all unsaved changes and re-initialize the permanent preferences storage.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type)=0
ESPPreferenceObject make_preference(uint32_t type, bool in_flash)
Definition preferences.h:58
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint8_t type
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:301
ESPPreferences * global_preferences
uint16_t length
Definition tt21100.cpp:0