refactor persistence manager from cpp to c
Some checks failed
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 4m12s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 4m17s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 3m49s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 4m0s

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2026-01-10 11:12:31 +01:00
parent ef0cda1d67
commit b77fdee21d
13 changed files with 476 additions and 489 deletions

View File

@@ -1,108 +0,0 @@
#pragma once
#include <string>
#include <type_traits>
/**
* @interface IPersistenceManager
* @brief Abstract interface for platform-independent persistence management
* @details This interface defines the contract for key-value storage and retrieval
* systems across different platforms (Desktop/SDL3 and ESP32).
*/
class IPersistenceManager
{
public:
virtual ~IPersistenceManager() = default;
/**
* @brief Template methods for type-safe setting and retrieving of values
* @tparam T The type of value to set (must be one of: bool, int, float, double, std::string)
* @param key The key to associate with the value
* @param value The value to store
*/
template<typename T>
void SetValue(const std::string& key, const T& value) {
static_assert(std::is_same_v<T, bool> ||
std::is_same_v<T, int> ||
std::is_same_v<T, float> ||
std::is_same_v<T, double> ||
std::is_same_v<T, std::string>,
"Unsupported type for IPersistenceManager");
SetValueImpl(key, value);
}
/**
* @brief Template method for type-safe retrieval of values
* @tparam T The type of value to retrieve
* @param key The key to look up
* @param defaultValue The default value to return if the key is not found
* @return The stored value or default value if the key doesn't exist
*/
template<typename T>
[[nodiscard]] T GetValue(const std::string& key, const T& defaultValue = T{}) const {
return GetValueImpl<T>(key, defaultValue);
}
/**
* @brief Utility methods for key management
*/
[[nodiscard]] virtual bool HasKey(const std::string& key) const = 0; ///< Check if a key exists
virtual void RemoveKey(const std::string& key) = 0; ///< Remove a key-value pair
virtual void Clear() = 0; ///< Clear all stored data
[[nodiscard]] virtual size_t GetKeyCount() const = 0; ///< Get the number of stored keys
/**
* @brief Persistence operations
*/
virtual bool Save() = 0; ///< Save data to persistent storage
virtual bool Load() = 0; ///< Load data from persistent storage
protected:
/**
* @brief Template-specific implementations that must be overridden by derived classes
* @details These methods handle the actual storage and retrieval of different data types
*/
virtual void SetValueImpl(const std::string& key, bool value) = 0;
virtual void SetValueImpl(const std::string& key, int value) = 0;
virtual void SetValueImpl(const std::string& key, float value) = 0;
virtual void SetValueImpl(const std::string& key, double value) = 0;
virtual void SetValueImpl(const std::string& key, const std::string& value) = 0;
[[nodiscard]] virtual bool GetValueImpl(const std::string& key, bool defaultValue) const = 0;
[[nodiscard]] virtual int GetValueImpl(const std::string& key, int defaultValue) const = 0;
[[nodiscard]] virtual float GetValueImpl(const std::string& key, float defaultValue) const = 0;
[[nodiscard]] virtual double GetValueImpl(const std::string& key, double defaultValue) const = 0;
[[nodiscard]] virtual std::string GetValueImpl(const std::string& key, const std::string& defaultValue) const = 0;
private:
/**
* @brief Template dispatch methods for type-safe value retrieval
* @tparam T The type to retrieve
* @param key The key to look up
* @param defaultValue The default value to return
* @return The retrieved value or default if not found
*/
template<typename T>
[[nodiscard]] T GetValueImpl(const std::string& key, const T& defaultValue) const
{
if constexpr (std::is_same_v<T, bool>) {
return GetValueImpl(key, static_cast<bool>(defaultValue));
} else if constexpr (std::is_same_v<T, int>) {
return GetValueImpl(key, static_cast<int>(defaultValue));
} else if constexpr (std::is_same_v<T, float>) {
return GetValueImpl(key, static_cast<float>(defaultValue));
} else if constexpr (std::is_same_v<T, double>) {
return GetValueImpl(key, static_cast<double>(defaultValue));
} else if constexpr (std::is_same_v<T, std::string>) {
return GetValueImpl(key, static_cast<const std::string&>(defaultValue));
} else {
static_assert(std::is_same_v<T, bool> ||
std::is_same_v<T, int> ||
std::is_same_v<T, float> ||
std::is_same_v<T, double> ||
std::is_same_v<T, std::string>,
"Unsupported type for IPersistenceManager");
return defaultValue; // This line will never be reached, but satisfies the compiler
}
}
};

View File

@@ -1,58 +0,0 @@
#pragma once
#include "IPersistenceManager.h"
#include <string>
#include <unordered_map>
#include <nvs.h>
#include <nvs_flash.h>
/**
* @class PersistenceManager
* @brief ESP32-specific implementation using NVS (Non-Volatile Storage)
* @details This implementation uses ESP32's NVS API for persistent storage
* in flash memory, providing a platform-optimized solution for
* embedded systems.
*/
class PersistenceManager final : public IPersistenceManager
{
private:
nvs_handle_t nvs_handle_;
std::string namespace_;
bool initialized_;
public:
explicit PersistenceManager(const std::string &nvs_namespace = "config");
~PersistenceManager() override;
bool HasKey(const std::string &key) const override;
void RemoveKey(const std::string &key) override;
void Clear() override;
size_t GetKeyCount() const override;
bool Save() override;
bool Load() override;
bool Initialize();
void Deinitialize();
bool IsInitialized() const
{
return initialized_;
}
protected:
void SetValueImpl(const std::string &key, bool value) override;
void SetValueImpl(const std::string &key, int value) override;
void SetValueImpl(const std::string &key, float value) override;
void SetValueImpl(const std::string &key, double value) override;
void SetValueImpl(const std::string &key, const std::string &value) override;
bool GetValueImpl(const std::string &key, bool defaultValue) const override;
int GetValueImpl(const std::string &key, int defaultValue) const override;
float GetValueImpl(const std::string &key, float defaultValue) const override;
double GetValueImpl(const std::string &key, double defaultValue) const override;
std::string GetValueImpl(const std::string &key, const std::string &defaultValue) const override;
private:
bool EnsureInitialized() const;
};

View File

@@ -0,0 +1,200 @@
#pragma once
#include <nvs.h>
#include <nvs_flash.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Structure to manage persistent storage using NVS.
*
* This struct holds the NVS handle, namespace, and initialization state
* for managing persistent key-value storage on the device.
*/
typedef struct
{
/** Handle to the NVS storage. */
nvs_handle_t nvs_handle;
/** Namespace used for NVS operations (max 15 chars + null terminator). */
char nvs_namespace[16];
/** Indicates if the persistence manager is initialized. */
bool initialized;
} persistence_manager_t;
/**
* @brief Initialize the persistence manager with a given NVS namespace.
*
* @param pm Pointer to the persistence manager structure.
* @param nvs_namespace Namespace to use for NVS operations.
*/
void persistence_manager_init(persistence_manager_t *pm, const char *nvs_namespace);
/**
* @brief Deinitialize the persistence manager and release resources.
*
* @param pm Pointer to the persistence manager structure.
*/
void persistence_manager_deinit(persistence_manager_t *pm);
/**
* @brief Check if the persistence manager is initialized.
*
* @param pm Pointer to the persistence manager structure.
* @return true if initialized, false otherwise.
*/
bool persistence_manager_is_initialized(const persistence_manager_t *pm);
/**
* @brief Check if a key exists in the NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to check for existence.
* @return true if the key exists, false otherwise.
*/
bool persistence_manager_has_key(const persistence_manager_t *pm, const char *key);
/**
* @brief Remove a key from the NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to remove.
*/
void persistence_manager_remove_key(persistence_manager_t *pm, const char *key);
/**
* @brief Clear all keys from the NVS storage in the current namespace.
*
* @param pm Pointer to the persistence manager structure.
*/
void persistence_manager_clear(persistence_manager_t *pm);
/**
* @brief Get the number of keys stored in the current namespace.
*
* @param pm Pointer to the persistence manager structure.
* @return Number of keys.
*/
size_t persistence_manager_get_key_count(const persistence_manager_t *pm);
/**
* @brief Save all pending changes to NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @return true if successful, false otherwise.
*/
bool persistence_manager_save(persistence_manager_t *pm);
/**
* @brief Load all data from NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @return true if successful, false otherwise.
*/
bool persistence_manager_load(persistence_manager_t *pm);
/**
* @brief Set a boolean value for a key in NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to set.
* @param value Boolean value to store.
*/
void persistence_manager_set_bool(persistence_manager_t *pm, const char *key, bool value);
/**
* @brief Set an integer value for a key in NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to set.
* @param value Integer value to store.
*/
void persistence_manager_set_int(persistence_manager_t *pm, const char *key, int32_t value);
/**
* @brief Set a float value for a key in NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to set.
* @param value Float value to store.
*/
void persistence_manager_set_float(persistence_manager_t *pm, const char *key, float value);
/**
* @brief Set a double value for a key in NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to set.
* @param value Double value to store.
*/
void persistence_manager_set_double(persistence_manager_t *pm, const char *key, double value);
/**
* @brief Set a string value for a key in NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to set.
* @param value String value to store.
*/
void persistence_manager_set_string(persistence_manager_t *pm, const char *key, const char *value);
/**
* @brief Get a boolean value for a key from NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to retrieve.
* @param default_value Value to return if key does not exist.
* @return Boolean value.
*/
bool persistence_manager_get_bool(const persistence_manager_t *pm, const char *key, bool default_value);
/**
* @brief Get an integer value for a key from NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to retrieve.
* @param default_value Value to return if key does not exist.
* @return Integer value.
*/
int32_t persistence_manager_get_int(const persistence_manager_t *pm, const char *key, int32_t default_value);
/**
* @brief Get a float value for a key from NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to retrieve.
* @param default_value Value to return if key does not exist.
* @return Float value.
*/
float persistence_manager_get_float(const persistence_manager_t *pm, const char *key, float default_value);
/**
* @brief Get a double value for a key from NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to retrieve.
* @param default_value Value to return if key does not exist.
* @return Double value.
*/
double persistence_manager_get_double(const persistence_manager_t *pm, const char *key, double default_value);
/**
* @brief Get a string value for a key from NVS storage.
*
* @param pm Pointer to the persistence manager structure.
* @param key Key to retrieve.
* @param out_value Buffer to store the retrieved string.
* @param max_len Maximum length of the output buffer.
* @param default_value Value to use if key does not exist.
*/
void persistence_manager_get_string(const persistence_manager_t *pm, const char *key, char *out_value,
size_t max_len, const char *default_value);
#ifdef __cplusplus
}
#endif