AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
SettingsManager.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGSMANAGER_H_
17 #define ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGSMANAGER_H_
18 
19 #include <cstddef>
20 #include <memory>
21 #include <mutex>
22 #include <type_traits>
23 #include <utility>
24 
29 
33 
34 namespace alexaClientSDK {
35 namespace settings {
36 
42 template <typename SettingsT>
45  std::shared_ptr<SettingsT> setting;
48 };
49 
53 template <typename... SettingsT>
55 public:
57  template <size_t index>
58  using SettingType = typename std::tuple_element<index, std::tuple<SettingsT...>>::type;
59 
61  template <size_t index>
63 
65  template <size_t index>
67 
69  template <typename SettingT>
70  using SettingPointerType = std::shared_ptr<SettingT>;
71 
73  using SettingConfigurations = std::tuple<SettingConfiguration<SettingsT>...>;
74 
76  static constexpr size_t NUMBER_OF_SETTINGS{sizeof...(SettingsT)};
77 
85  std::shared_ptr<registrationManager::CustomerDataManagerInterface> dataManager,
86  SettingConfigurations settingConfigurations);
87 
94  SettingsManager(std::shared_ptr<registrationManager::CustomerDataManagerInterface> dataManager);
95 
99  virtual ~SettingsManager() = default;
100 
108  template <size_t index>
109  SetSettingResult setValue(const ValueType<index>& value);
110 
119  template <size_t index>
120  std::pair<bool, ValueType<index>> getValue(const ValueType<index>& defaultValue = ValueType<index>()) const;
121 
129  template <size_t index>
130  std::string getJsonValue() const;
131 
139  template <size_t index>
140  bool addObserver(std::shared_ptr<ObserverType<index>> observer);
141 
148  template <size_t index>
149  void removeObserver(std::shared_ptr<ObserverType<index>> observer);
150 
158  template <size_t index>
159  bool addSetting(std::shared_ptr<SettingType<index>> setting);
160 
167  template <size_t index>
168  void removeSetting(std::shared_ptr<SettingType<index>> setting);
169 
176  template <size_t index>
177  bool hasSetting();
178 
184  SettingConfigurations getConfigurations() const;
185 
193  template <size_t index>
194  std::shared_ptr<SettingType<index>> getSetting() const;
195 
198  void clearData() override;
200 
201 private:
204 
211  template <size_t index>
212  typename std::enable_if<index >= NUMBER_OF_SETTINGS, void>::type doClearData() {
213  }
214 
222  template <size_t index>
223  typename std::enable_if<index <= NUMBER_OF_SETTINGS - 1, void>::type doClearData() {
224  auto& setting = std::get<index>(m_settings);
225  if (setting) {
226  if (!setting->clearData(setting->getDefault())) {
227  avsCommon::utils::logger::acsdkError(LogEntry("SettingManager", "clearDataFailed")
228  .d("reason", "invalidSetting")
229  .d("settingIndex", index));
230  }
231  } else {
233  LogEntry("SettingManager", __func__).d("reason", "invalidSetting").d("settingIndex", index));
234  }
235  doClearData<index + 1>();
236  }
237 
241  SettingsManager(const SettingsManager&) = delete;
242 
246  SettingsManager& operator=(const SettingsManager&) = delete;
247 
249  mutable std::mutex m_mutex;
250 
251  // A tuple with all the settings supported by this manager.
252  std::tuple<SettingPointerType<SettingsT>...> m_settings;
253 
255  SettingConfigurations m_settingConfigs;
256 };
257 
258 template <typename... SettingsT>
260  std::shared_ptr<registrationManager::CustomerDataManagerInterface> dataManager,
261  SettingConfigurations settingConfiguration) :
262  CustomerDataHandler{dataManager},
263  m_settingConfigs{settingConfiguration} {
264 }
265 
266 template <typename... SettingsT>
268  std::shared_ptr<registrationManager::CustomerDataManagerInterface> dataManager) :
269  CustomerDataHandler{dataManager} {
270 }
271 
272 template <typename... SettingsT>
273 template <size_t index>
274 std::pair<bool, typename SettingsManager<SettingsT...>::template ValueType<index>> SettingsManager<
275  SettingsT...>::getValue(const ValueType<index>& defaultValue) const {
276  std::lock_guard<std::mutex> lock{m_mutex};
277  auto& setting = std::get<index>(m_settings);
278  if (setting) {
279  return {true, setting->get()};
280  }
281 
283  LogEntry("SettingManager", "getValueFailed").d("reason", "invalidSetting").d("settingIndex", index));
284  return {false, defaultValue};
285 }
286 
287 template <typename... SettingsT>
288 template <size_t index>
290  std::lock_guard<std::mutex> lock{m_mutex};
291  auto& setting = std::get<index>(m_settings);
292  if (setting) {
293  auto result = toSettingString<ValueType<index>>(setting->get());
294  if (!result.first) {
295  avsCommon::utils::logger::acsdkError(LogEntry("SettingManager", "getStringValueFailed")
296  .d("reason", "toSettingStringFailed")
297  .d("settingIndex", index));
298  return std::string();
299  }
300  return result.second;
301  }
302 
304  LogEntry("SettingManager", __func__).d("result", "noSettingAvailable").d("settingIndex", index));
305  return std::string();
306 }
307 
308 template <typename... SettingsT>
309 template <size_t index>
311  std::lock_guard<std::mutex> lock{m_mutex};
312  auto& setting = std::get<index>(m_settings);
313  if (setting) {
314  return setting->setLocalChange(value);
315  }
316 
318  LogEntry("SettingManager", "setValueFailed").d("reason", "invalidSetting").d("settingIndex", index));
320 }
321 
322 template <typename... SettingsT>
323 template <size_t index>
325  std::lock_guard<std::mutex> lock{m_mutex};
326  auto& setting = std::get<index>(m_settings);
327  if (setting && observer) {
328  return setting->addObserver(observer);
329  }
330 
332  LogEntry("SettingManager", "addObserverFailed").d("reason", "invalidSetting").d("settingIndex", index));
333  return false;
334 }
335 
336 template <typename... SettingsT>
337 template <size_t index>
339  std::lock_guard<std::mutex> lock{m_mutex};
340  auto& setting = std::get<index>(m_settings);
341  if (setting && observer) {
342  setting->removeObserver(observer);
343  return;
344  }
345 
347  LogEntry("SettingManager", "removeObserverFailed").d("reason", "invalidSetting").d("settingIndex", index));
348 }
349 
350 template <typename... SettingsT>
351 template <size_t index>
353  std::lock_guard<std::mutex> lock{m_mutex};
354  auto& setting = std::get<index>(m_settings);
355  if (!setting && newSetting) {
356  setting.swap(newSetting);
357  return true;
358  }
359 
361  LogEntry("SettingManager", "addSettingFailed").d("reason", "invalidSetting").d("settingIndex", index));
362  return false;
363 }
364 
365 template <typename... SettingsT>
366 template <size_t index>
368  std::lock_guard<std::mutex> lock{m_mutex};
369  auto& setting = std::get<index>(m_settings);
370  if (setting == oldSetting) {
371  setting.reset();
372  } else {
374  LogEntry("SettingManager", "removeSettingFailed").d("reason", "invalidSetting").d("settingIndex", index));
375  }
376 }
377 
378 template <typename... SettingsT>
379 template <size_t index>
381  std::lock_guard<std::mutex> lock{m_mutex};
382  return std::get<index>(m_settings);
383 }
384 
385 template <typename... SettingsT>
387  std::lock_guard<std::mutex> lock{m_mutex};
388  doClearData<0>();
389 }
390 
391 template <typename... SettingsT>
392 std::tuple<SettingConfiguration<SettingsT>...> SettingsManager<SettingsT...>::getConfigurations() const {
393  return m_settingConfigs;
394 }
395 
396 template <typename... SettingsT>
397 template <size_t index>
398 std::shared_ptr<typename SettingsManager<SettingsT...>::template SettingType<index>> SettingsManager<
399  SettingsT...>::getSetting() const {
400  return std::get<index>(m_settingConfigs).setting;
401 }
402 
403 } // namespace settings
404 } // namespace alexaClientSDK
405 
406 #endif // ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGSMANAGER_H_
typename std::tuple_element< index, std::tuple< SettingsT... > >::type SettingType
The setting type kept at index position.
Definition: SettingsManager.h:58
Definition: SettingsManager.h:54
bool addSetting(std::shared_ptr< SettingType< index >> setting)
Definition: SettingsManager.h:352
typename SettingType< index >::ObserverType ObserverType
Define the observer type for setting kept on position index.
Definition: SettingsManager.h:66
std::shared_ptr< SettingT > SettingPointerType
Define the shared_ptr type for the given SettingT.
Definition: SettingsManager.h:70
void acsdkDebug0(const LogEntry &entry)
std::string getJsonValue() const
Definition: SettingsManager.h:289
::std::string string
Definition: gtest-port.h:1097
CustomerDataHandler(const std::shared_ptr< registrationManager::CustomerDataManagerInterface > &dataManager)
typename SettingType< index >::ValueType ValueType
Define the value type for setting kept on position index.
Definition: SettingsManager.h:62
static int index
Mock buffer index.
Definition: AndroidSLESMediaQueueTest.cpp:70
std::pair< bool, ValueType< index > > getValue(const ValueType< index > &defaultValue=ValueType< index >()) const
SettingsManager(std::shared_ptr< registrationManager::CustomerDataManagerInterface > dataManager, SettingConfigurations settingConfigurations)
Definition: SettingsManager.h:259
The request failed because the setting requested does not exist.
bool addObserver(std::shared_ptr< ObserverType< index >> observer)
Definition: SettingsManager.h:324
void removeSetting(std::shared_ptr< SettingType< index >> setting)
Definition: SettingsManager.h:367
SettingConfigurations getConfigurations() const
Definition: SettingsManager.h:392
SetSettingResult
Definition: SetSettingResult.h:23
Definition: SettingsManager.h:43
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
bool hasSetting()
Definition: SettingsManager.h:380
SetSettingResult setValue(const ValueType< index > &value)
Definition: SettingsManager.h:310
avsCommon::utils::Optional< settings::SettingEventMetadata > metadata
The setting metadata.
Definition: SettingsManager.h:47
type
Definition: upload.py:443
void removeObserver(std::shared_ptr< ObserverType< index >> observer)
Definition: SettingsManager.h:338
void acsdkError(const LogEntry &entry)
std::shared_ptr< SettingsT > setting
The setting configured.
Definition: SettingsManager.h:45
std::shared_ptr< SettingType< index > > getSetting() const
std::tuple< SettingConfiguration< SettingsT >... > SettingConfigurations
The tuple holding the settings configuration.
Definition: SettingsManager.h:73
void clearData() override
Definition: SettingsManager.h:386
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:33

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0