ESPHome  2022.6.3
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 
49  template<typename T, enable_if_t<is_trivially_copyable<T>::value, bool> = true>
50  ESPPreferenceObject make_preference(uint32_t type, bool in_flash) {
51  return this->make_preference(sizeof(T), type, in_flash);
52  }
53 
54  template<typename T, enable_if_t<is_trivially_copyable<T>::value, bool> = true>
56  return this->make_preference(sizeof(T), type);
57  }
58 };
59 
60 extern ESPPreferences *global_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
61 
62 } // namespace esphome
virtual bool load(uint8_t *data, size_t len)=0
ESPPreferenceObject make_preference(uint32_t type)
Definition: preferences.h:55
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:50
ESPPreferenceObject(ESPPreferenceBackend *backend)
Definition: preferences.h:19
uint8_t type
std::string size_t len
Definition: helpers.h:278
Definition: a4988.cpp:4