AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
SettingCallbacks.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_SETTINGCALLBACKS_H_
16 #define ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGCALLBACKS_H_
17 
18 #include <functional>
19 #include <memory>
20 #include <vector>
21 
23 #include "SettingCallbackAdapter.h"
24 
25 namespace alexaClientSDK {
26 namespace settings {
27 
53 template <typename ManagerT>
55 public:
57  template <size_t id>
58  using ValueType = typename ManagerT::template ValueType<id>;
59 
61  template <size_t id>
62  using FunctionType = std::function<void(const ValueType<id>&, SettingNotifications)>;
63 
67  virtual ~SettingCallbacks();
68 
79  static std::shared_ptr<SettingCallbacks<ManagerT>> create(std::shared_ptr<ManagerT> manager);
80 
87  template <size_t id>
88  bool add(FunctionType<id> callback);
89 
97  void removeAll();
98 
99 private:
103  SettingCallbacks(std::shared_ptr<ManagerT> manager);
104 
106  std::shared_ptr<ManagerT> m_manager;
107 
109  std::vector<std::shared_ptr<SettingCallbackAdapterInterface<ManagerT>>> m_callbacks;
110 };
111 
112 template <typename ManagerT>
114  removeAll();
115 }
116 
117 template <typename ManagerT>
118 std::shared_ptr<SettingCallbacks<ManagerT>> SettingCallbacks<ManagerT>::create(std::shared_ptr<ManagerT> manager) {
119  if (manager) {
120  return std::shared_ptr<SettingCallbacks<ManagerT>>(new SettingCallbacks<ManagerT>(manager));
121  }
122  return nullptr;
123 }
124 
125 template <typename ManagerT>
126 template <size_t id>
128  auto wrapper = SettingCallbackAdapter<ManagerT, id>::create(callback);
129  if (wrapper) {
130  if (wrapper->addToManager(*m_manager)) {
131  m_callbacks.push_back(wrapper);
132  return true;
133  }
134  }
135  return false;
136 }
137 
138 template <typename ManagerT>
140  for (auto& wrapper : m_callbacks) {
141  wrapper->removeFromManager(*m_manager);
142  }
143  m_callbacks.clear();
144 }
145 
146 template <typename ManagerT>
147 SettingCallbacks<ManagerT>::SettingCallbacks(std::shared_ptr<ManagerT> manager) : m_manager{manager} {
148 }
149 
150 } // namespace settings
151 } // namespace alexaClientSDK
152 
153 #endif // ALEXA_CLIENT_SDK_SETTINGS_INCLUDE_SETTINGS_SETTINGCALLBACKS_H_
virtual ~SettingCallbacks()
Definition: SettingCallbacks.h:113
static std::shared_ptr< SettingCallbackAdapter > create(FunctionType callback)
Definition: SettingCallbackAdapter.h:144
bool add(FunctionType< id > callback)
Definition: SettingCallbacks.h:127
Definition: SettingCallbacks.h:54
std::function< void(const ValueType< id > &, SettingNotifications)> FunctionType
Callback function value type.
Definition: SettingCallbacks.h:62
void removeAll()
Definition: SettingCallbacks.h:139
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
typename ManagerT::template ValueType< id > ValueType
Setting value type.
Definition: SettingCallbacks.h:58
static std::shared_ptr< SettingCallbacks< ManagerT > > create(std::shared_ptr< ManagerT > manager)
Definition: SettingCallbacks.h:118
SettingNotifications
Definition: SettingObserverInterface.h:28

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