ESPHome  2024.11.1
preferences.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstring>
4 #include <cstdint>
5 
6 #include "esphome/core/helpers.h"
7 
8 namespace 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:
18  ESPPreferenceObject() = default;
19  ESPPreferenceObject(ESPPreferenceBackend *backend) : backend_(backend) {}
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:
34  ESPPreferenceBackend *backend_{nullptr};
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 
68 extern ESPPreferences *global_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
69 
70 } // namespace esphome
virtual bool load(uint8_t *data, size_t len)=0
uint16_t sync
Definition: sun_gtil2.cpp:14
ESPPreferenceObject make_preference(uint32_t type)
Definition: preferences.h:63
virtual bool save(const uint8_t *data, size_t len)=0
bool save(const T *src)
Definition: preferences.h:21
ESPPreferences * global_preferences
ESPPreferenceObject make_preference(uint32_t type, bool in_flash)
Definition: preferences.h:58
ESPPreferenceObject(ESPPreferenceBackend *backend)
Definition: preferences.h:19
uint8_t type
uint16_t reset
Definition: ina226.h:39
std::string size_t len
Definition: helpers.h:293
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7