AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
SettingInterface.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 #ifndef ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGINTERFACE_H_
16 #define ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGINTERFACE_H_
17 
18 #include <atomic>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <type_traits>
23 #include <unordered_set>
24 
28 
31 
32 namespace alexaClientSDK {
33 namespace settings {
34 
44 template <typename ValueT>
46 public:
48  using ValueType = ValueT;
49 
52 
56  virtual ~SettingInterface() = default;
57 
64  virtual SetSettingResult setLocalChange(const ValueType& value) = 0;
65 
72  virtual bool setAvsChange(const ValueType& value) = 0;
73 
80  virtual bool clearData(const ValueType& value) = 0;
81 
87  inline ValueType get() const;
88 
94  inline ValueType getDefault() const;
95 
104  inline bool addObserver(std::shared_ptr<ObserverType> observer);
105 
111  inline void removeObserver(std::shared_ptr<ObserverType>& observer);
112 
113 protected:
117  SettingInterface(const ValueType& value);
118 
124  void notifyObservers(SettingNotifications notification);
125 
127  std::mutex m_observerMutex;
128 
130  std::unordered_set<std::shared_ptr<ObserverType>> m_observers;
131 
134 
137 
139  typename std::conditional<std::is_scalar<ValueType>::value, std::atomic<ValueType>, GuardedValue>::type m_value;
140 
143 };
144 
145 template <typename ValueT>
147  return m_value;
148 }
149 
150 template <typename ValueT>
152  return m_defaultValue;
153 }
154 
155 template <typename ValueT>
156 bool SettingInterface<ValueT>::addObserver(std::shared_ptr<ObserverType> observer) {
157  if (observer) {
158  std::lock_guard<std::mutex>{m_observerMutex};
159  m_observers.insert(observer);
160  return true;
161  }
162  avsCommon::utils::logger::acsdkError(LogEntry("SettingInterface", "addObserverFailed").d("reason", "nullObserver"));
163  return false;
164 }
165 
166 template <typename ValueT>
168  std::lock_guard<std::mutex>{m_observerMutex};
169  for (auto& observer : m_observers) {
170  ValueT value = m_value;
171  observer->onSettingNotification(value, notification);
172  }
173 }
174 
175 template <typename ValueT>
176 void SettingInterface<ValueT>::removeObserver(std::shared_ptr<ObserverType>& observer) {
177  std::lock_guard<std::mutex>{m_observerMutex};
178  m_observers.erase(observer);
179 }
180 
181 template <typename ValueT>
183 }
184 
185 } // namespace settings
186 } // namespace alexaClientSDK
187 
188 #endif // ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGINTERFACE_H_
const ValueType m_defaultValue
The default setting value.
Definition: SettingInterface.h:142
virtual bool setAvsChange(const ValueType &value)=0
std::mutex m_observerMutex
Mutex used to guard observers.
Definition: SettingInterface.h:127
Definition: SettingInterface.h:45
SettingInterface(const ValueType &value)
Definition: SettingInterface.h:182
void removeObserver(std::shared_ptr< ObserverType > &observer)
Definition: SettingInterface.h:176
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
bool addObserver(std::shared_ptr< ObserverType > observer)
Definition: SettingInterface.h:156
ValueT ValueType
Define the setting value type.
Definition: SettingInterface.h:48
virtual bool clearData(const ValueType &value)=0
ValueType getDefault() const
Definition: SettingInterface.h:151
void notifyObservers(SettingNotifications notification)
Definition: SettingInterface.h:167
avsCommon::utils::logger::LogEntry LogEntry
Alias to make log entries less verbose.
Definition: SettingInterface.h:136
ValueType get() const
Definition: SettingInterface.h:146
SetSettingResult
Definition: SetSettingResult.h:23
virtual SetSettingResult setLocalChange(const ValueType &value)=0
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
SettingNotifications
Definition: SettingObserverInterface.h:28
type
Definition: upload.py:443
void acsdkError(const LogEntry &entry)
Definition: SettingObserverInterface.h:92
std::unordered_set< std::shared_ptr< ObserverType > > m_observers
Set of all observers of this setting.
Definition: SettingInterface.h:130
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