|
@ -53,51 +53,63 @@ extern kvs_type kv_store; |
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------- |
|
|
// -------------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
class settings_key_t { |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
|
settings_key_t(const char* value, unsigned char index) : |
|
|
|
|
|
_value(value), _index(index) |
|
|
|
|
|
{} |
|
|
|
|
|
settings_key_t(const String& value, unsigned char index) : |
|
|
|
|
|
_value(value), _index(index) |
|
|
|
|
|
{} |
|
|
|
|
|
settings_key_t(String&& value, unsigned char index) : |
|
|
|
|
|
_value(std::move(value)), _index(index) |
|
|
|
|
|
{} |
|
|
|
|
|
settings_key_t(const char* value) : |
|
|
|
|
|
_value(value), _index(-1) |
|
|
|
|
|
{} |
|
|
|
|
|
settings_key_t(const String& value) : |
|
|
|
|
|
_value(value), _index(-1) |
|
|
|
|
|
{} |
|
|
|
|
|
settings_key_t(const __FlashStringHelper* value) : |
|
|
|
|
|
_value(value), _index(-1) |
|
|
|
|
|
{} |
|
|
|
|
|
settings_key_t() : |
|
|
|
|
|
_value(), _index(-1) |
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
bool match(const char* value) const { |
|
|
|
|
|
return (_value == value) || (toString() == value); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool match(const String& value) const { |
|
|
|
|
|
return (_value == value) || (toString() == value); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String toString() const; |
|
|
|
|
|
|
|
|
|
|
|
explicit operator String () const { |
|
|
|
|
|
return toString(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
|
const String _value; |
|
|
|
|
|
int _index; |
|
|
|
|
|
|
|
|
class SettingsKey { |
|
|
|
|
|
public: |
|
|
|
|
|
SettingsKey(const char* key) : |
|
|
|
|
|
_key(key) |
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
SettingsKey(const String& key) : |
|
|
|
|
|
_key(key) |
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
SettingsKey(String&& key) : |
|
|
|
|
|
_key(std::move(key)) |
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
SettingsKey(const String& prefix, unsigned char index) { |
|
|
|
|
|
_key.reserve(prefix.length()); |
|
|
|
|
|
_key += prefix; |
|
|
|
|
|
_key += index; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SettingsKey(String&& prefix, unsigned char index) : |
|
|
|
|
|
_key(std::move(prefix)) |
|
|
|
|
|
{ |
|
|
|
|
|
_key += index; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SettingsKey(const char* prefix, unsigned char index) : |
|
|
|
|
|
_key(prefix) |
|
|
|
|
|
{ |
|
|
|
|
|
_key += index; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool operator==(const char* other) const { |
|
|
|
|
|
return _key == other; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool operator==(const String& other) const { |
|
|
|
|
|
return _key == other; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const String& toString() const { |
|
|
|
|
|
return _key; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
explicit operator String() const & { |
|
|
|
|
|
return _key; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
explicit operator String() && { |
|
|
|
|
|
return std::move(_key); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
|
String _key; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
using settings_move_key_t = std::pair<settings_key_t, settings_key_t>; |
|
|
|
|
|
|
|
|
using settings_move_key_t = std::pair<SettingsKey, SettingsKey>; |
|
|
using settings_filter_t = std::function<String(String& value)>; |
|
|
using settings_filter_t = std::function<String(String& value)>; |
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------- |
|
|
// -------------------------------------------------------------------------- |
|
@ -121,13 +133,13 @@ using is_arduino_string = std::is_same<String, typename std::decay<T>::type>; |
|
|
template <typename T> |
|
|
template <typename T> |
|
|
using enable_if_arduino_string = std::enable_if<is_arduino_string<T>::value>; |
|
|
using enable_if_arduino_string = std::enable_if<is_arduino_string<T>::value>; |
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
using enable_if_not_arduino_string = std::enable_if<!is_arduino_string<T>::value>; |
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------- |
|
|
// -------------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
uint32_t u32fromString(const String& string, int base); |
|
|
uint32_t u32fromString(const String& string, int base); |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
using convert_t = T(*)(const String& value); |
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
template <typename T> |
|
|
T convert(const String& value); |
|
|
T convert(const String& value); |
|
|
|
|
|
|
|
@ -188,35 +200,41 @@ void moveSetting(const String& from, const String& to); |
|
|
void moveSetting(const String& from, const String& to, unsigned int index); |
|
|
void moveSetting(const String& from, const String& to, unsigned int index); |
|
|
void moveSettings(const String& from, const String& to); |
|
|
void moveSettings(const String& from, const String& to); |
|
|
|
|
|
|
|
|
template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert> |
|
|
|
|
|
R getSetting(const settings_key_t& key, R defaultValue) __attribute__((noinline)); |
|
|
|
|
|
|
|
|
template <typename T, typename = typename settings::internal::enable_if_not_arduino_string<T>::type> |
|
|
|
|
|
T getSetting(const SettingsKey& key, T defaultValue) __attribute__((noinline)); |
|
|
|
|
|
|
|
|
template<typename R, settings::internal::convert_t<R> Rfunc = settings::internal::convert> |
|
|
|
|
|
R getSetting(const settings_key_t& key, R defaultValue) { |
|
|
|
|
|
|
|
|
template <typename T, typename = typename settings::internal::enable_if_not_arduino_string<T>::type> |
|
|
|
|
|
T getSetting(const SettingsKey& key, T defaultValue) { |
|
|
auto result = settings::kv_store.get(key.toString()); |
|
|
auto result = settings::kv_store.get(key.toString()); |
|
|
if (!result) { |
|
|
if (!result) { |
|
|
return defaultValue; |
|
|
return defaultValue; |
|
|
} |
|
|
} |
|
|
return Rfunc(result.value); |
|
|
|
|
|
|
|
|
return settings::internal::convert<T>(result.value); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
template<> |
|
|
|
|
|
String getSetting(const settings_key_t& key, String defaultValue); |
|
|
|
|
|
|
|
|
String getSetting(const char* key); |
|
|
|
|
|
String getSetting(const String& key); |
|
|
|
|
|
String getSetting(const __FlashStringHelper* key); |
|
|
|
|
|
|
|
|
String getSetting(const settings_key_t& key); |
|
|
|
|
|
String getSetting(const settings_key_t& key, const char* defaultValue); |
|
|
|
|
|
String getSetting(const settings_key_t& key, const __FlashStringHelper* defaultValue); |
|
|
|
|
|
|
|
|
String getSetting(const SettingsKey& key); |
|
|
|
|
|
String getSetting(const SettingsKey& key, const char* defaultValue); |
|
|
|
|
|
String getSetting(const SettingsKey& key, const __FlashStringHelper* defaultValue); |
|
|
|
|
|
String getSetting(const SettingsKey& key, const String& defaultValue); |
|
|
|
|
|
String getSetting(const SettingsKey& key, const String& defaultValue); |
|
|
|
|
|
String getSetting(const SettingsKey& key, String&& defaultValue); |
|
|
|
|
|
|
|
|
template<typename T> |
|
|
|
|
|
bool setSetting(const settings_key_t& key, const T& value) { |
|
|
|
|
|
return settings::kv_store.set(key.toString(), String(value)); |
|
|
|
|
|
|
|
|
template<typename T, typename = typename settings::internal::enable_if_arduino_string<T>::type> |
|
|
|
|
|
bool setSetting(const SettingsKey& key, T&& value) { |
|
|
|
|
|
return settings::kv_store.set(key.toString(), value); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
template<> |
|
|
|
|
|
bool setSetting(const settings_key_t& key, const String& value); |
|
|
|
|
|
|
|
|
template<typename T, typename = typename settings::internal::enable_if_not_arduino_string<T>::type> |
|
|
|
|
|
bool setSetting(const SettingsKey& key, T value) { |
|
|
|
|
|
return setSetting(key, std::move(String(value))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
bool delSetting(const settings_key_t& key); |
|
|
|
|
|
bool hasSetting(const settings_key_t& key); |
|
|
|
|
|
|
|
|
bool delSetting(const SettingsKey& key); |
|
|
|
|
|
bool hasSetting(const SettingsKey& key); |
|
|
|
|
|
|
|
|
void saveSettings(); |
|
|
void saveSettings(); |
|
|
void resetSettings(); |
|
|
void resetSettings(); |
|
|