15 #ifndef ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTING_H_ 16 #define ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTING_H_ 47 template <
typename ValueT>
62 static std::shared_ptr<Setting<ValueT>>
create(
64 std::unique_ptr<SettingProtocolInterface> settingProtocol,
65 std::function<
bool(
const ValueType&)> applyValueFn = std::function<
bool(
const ValueType&)>());
84 std::function<
bool(
const ValueType&)> applyValueFn,
85 std::unique_ptr<SettingProtocolInterface> settingProtocol);
94 std::pair<bool, std::string> handleSetValue(
const ValueType& value);
105 std::function<bool(const ValueType&)> m_applyFunction;
108 std::unique_ptr<SettingProtocolInterface> m_protocol;
120 template <
typename ValueT>
123 std::unique_ptr<SettingProtocolInterface> settingProtocol,
124 std::function<
bool(
const ValueType&)> applyValueFn) {
125 if (!settingProtocol) {
131 std::shared_ptr<Setting<ValueT>>(
new Setting(defaultValue, applyValueFn,
std::move(settingProtocol)));
133 if (!setting->restore()) {
141 template <
typename ValueT>
145 std::tie(ok, valueStr) = toSettingString<ValueT>(value);
146 if (ok && (!this->m_applyFunction || this->m_applyFunction(value))) {
150 return {
true, valueStr};
153 return {
false, toSettingString<ValueType>(this->
m_value).second};
156 template <
typename ValueT>
158 auto executeSet = [
this, value] {
159 std::lock_guard<std::mutex> lock{m_mutex};
160 return this->handleSetValue(value);
162 auto revertChange = [
this] {
163 std::lock_guard<std::mutex> lock{m_mutex};
164 auto oldValue = m_oldValue;
165 return this->handleSetValue(oldValue).second;
168 return m_protocol->avsChange(executeSet, revertChange,
notifyObservers);
171 template <
typename ValueT>
173 auto executeSet = [
this, value] {
174 std::lock_guard<std::mutex> lock{m_mutex};
175 return this->handleSetValue(value);
177 auto revertChange = [
this] {
178 std::lock_guard<std::mutex> lock{m_mutex};
179 auto oldValue = m_oldValue;
180 return this->handleSetValue(oldValue).second;
183 return m_protocol->localChange(executeSet, revertChange,
notifyObservers);
186 template <
typename ValueT>
188 std::unique_lock<std::mutex> lock{m_mutex};
191 auto result = m_protocol->clearData();
201 template <
typename ValueT>
203 auto applyChange = [
this](
const std::string& databaseValue) -> std::pair<bool, std::string> {
204 bool convertOk, setOk;
207 if (databaseValue.empty()) {
211 std::tie(convertOk, value) = fromSettingString<ValueT>(databaseValue, this->
m_value);
213 std::lock_guard<std::mutex> lock{m_mutex};
214 std::tie(setOk, valueStr) = handleSetValue(value);
215 return {convertOk && setOk, valueStr};
221 template <
typename ValueT>
224 std::function<
bool(
const ValueType&)> applyValueFn,
225 std::unique_ptr<SettingProtocolInterface> settingProtocol) :
227 m_applyFunction{applyValueFn},
235 #endif // ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTING_H_ ValueT ValueType
Define an alias for setting value type.
Definition: Setting.h:51
static std::shared_ptr< Setting< ValueT > > create(const ValueType &defaultValue, std::unique_ptr< SettingProtocolInterface > settingProtocol, std::function< bool(const ValueType &)> applyValueFn=std::function< bool(const ValueType &)>())
Definition: Setting.h:121
::std::string string
Definition: gtest-port.h:1097
Definition: SettingInterface.h:45
bool setAvsChange(const ValueType &value) override
Definition: Setting.h:157
std::conditional< std::is_scalar< ValueType >::value, std::atomic< ValueType >, GuardedValue >::type m_value
The setting value. (is_trivially_copyable is not supported on gcc4.8)
Definition: SettingInterface.h:139
void notifyObservers(SettingNotifications notification)
Definition: SettingInterface.h:167
bool clearData(const ValueType &value) override
Definition: Setting.h:187
void acsdkInfo(const LogEntry &entry)
SetSettingResult
Definition: SetSettingResult.h:23
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
SettingNotifications
Definition: SettingObserverInterface.h:28
void acsdkError(const LogEntry &entry)
SetSettingResult setLocalChange(const ValueType &value) override
Definition: Setting.h:172
const T & move(const T &t)
Definition: gtest-port.h:1317
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:33