Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
3.4 KiB

  1. /*
  2. Part of the SETTINGS module
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. Copyright (C) 2020-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  5. */
  6. #pragma once
  7. #include <Arduino.h>
  8. #include <utility>
  9. // --------------------------------------------------------------------------
  10. class SettingsKey {
  11. public:
  12. SettingsKey(const char* key) :
  13. _key(key)
  14. {}
  15. SettingsKey(const String& key) :
  16. _key(key)
  17. {}
  18. SettingsKey(String&& key) :
  19. _key(std::move(key))
  20. {}
  21. SettingsKey(const String& prefix, size_t index) {
  22. _key.reserve(prefix.length() + 4);
  23. _key += prefix;
  24. _key += index;
  25. }
  26. SettingsKey(String&& prefix, size_t index) :
  27. _key(std::move(prefix))
  28. {
  29. _key += index;
  30. }
  31. SettingsKey(const char* prefix, size_t index) :
  32. _key(prefix)
  33. {
  34. _key += index;
  35. }
  36. const char* c_str() const {
  37. return _key.c_str();
  38. }
  39. size_t length() const {
  40. return _key.length();
  41. }
  42. bool operator==(const char* other) const {
  43. return _key == other;
  44. }
  45. bool operator==(const String& other) const {
  46. return _key == other;
  47. }
  48. const String& value() const {
  49. return _key;
  50. }
  51. explicit operator String() const & {
  52. return _key;
  53. }
  54. explicit operator String() && {
  55. return std::move(_key);
  56. }
  57. private:
  58. String _key;
  59. };
  60. // --------------------------------------------------------------------------
  61. namespace settings {
  62. namespace internal {
  63. struct BasicSetting {
  64. using Get = String(*)();
  65. BasicSetting() = delete;
  66. constexpr BasicSetting(const char* const key, Get get) :
  67. _key(key),
  68. _get(get)
  69. {}
  70. constexpr const char* const key() const {
  71. return _key;
  72. }
  73. String get() const {
  74. return _get();
  75. }
  76. private:
  77. const char* const _key;
  78. Get _get;
  79. };
  80. struct IndexedSetting {
  81. using Get = String(*)(size_t);
  82. IndexedSetting() = delete;
  83. constexpr IndexedSetting(const char* const prefix, Get get) :
  84. _prefix(prefix),
  85. _get(get)
  86. {}
  87. constexpr const char* const prefix() const {
  88. return _prefix;
  89. }
  90. String get(size_t index) const {
  91. return _get(index);
  92. }
  93. private:
  94. const char* const _prefix;
  95. Get _get;
  96. };
  97. } // namespace internal
  98. // 'optional' type for byte range
  99. struct ValueResult {
  100. ValueResult() = default;
  101. ValueResult(const ValueResult&) = default;
  102. ValueResult(ValueResult&&) = default;
  103. explicit ValueResult(const String& value) :
  104. _result(true),
  105. _value(value)
  106. {}
  107. explicit ValueResult(String&& value) :
  108. _result(true),
  109. _value(std::move(value))
  110. {}
  111. template <typename T>
  112. ValueResult& operator=(T&& value) {
  113. if (!_result) {
  114. _result = true;
  115. _value = std::forward<T>(value);
  116. }
  117. return *this;
  118. }
  119. explicit operator bool() const {
  120. return _result;
  121. }
  122. String get() && {
  123. auto moved = std::move(_value);
  124. _result = false;
  125. return moved;
  126. }
  127. const String& ref() const {
  128. return _value;
  129. }
  130. const char* c_str() const {
  131. return _value.c_str();
  132. }
  133. size_t length() const {
  134. return _value.length();
  135. }
  136. private:
  137. bool _result { false };
  138. String _value;
  139. };
  140. } // namespace settings