AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Notifier.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 ACSDK_NOTIFIER_NOTIFIER_H_
17 #define ACSDK_NOTIFIER_NOTIFIER_H_
18 
19 #include <memory>
20 
24 
25 namespace alexaClientSDK {
26 namespace notifier {
27 
37 template <typename ObserverType>
38 class Notifier : public notifierInterfaces::NotifierInterface<ObserverType> {
39 public:
40  Notifier();
41 
44  void addObserver(const std::shared_ptr<ObserverType>& observer) override;
45  void removeObserver(const std::shared_ptr<ObserverType>& observer) override;
46  void addWeakPtrObserver(const std::weak_ptr<ObserverType>& observer) override;
47  void removeWeakPtrObserver(const std::weak_ptr<ObserverType>& observer) override;
48  void notifyObservers(std::function<void(const std::shared_ptr<ObserverType>&)> notify) override;
49  bool notifyObserversInReverse(std::function<void(const std::shared_ptr<ObserverType>&)> notify) override;
50  void setAddObserverFunction(std::function<void(const std::shared_ptr<ObserverType>&)> addObserverFunc) override;
52 private:
54  using Traits = NotifierTraits<ObserverType>;
55 
57  std::unique_ptr<DataInterface> m_data;
58 };
59 
60 template <typename ObserverType>
62 }
63 
64 template <typename ObserverType>
65 inline void Notifier<ObserverType>::addObserver(const std::shared_ptr<ObserverType>& observer) {
66  m_data->doAddStrongRefObserver(Traits::toVoidPtr(observer));
67 }
68 
69 template <typename ObserverType>
70 inline void Notifier<ObserverType>::removeObserver(const std::shared_ptr<ObserverType>& observer) {
71  m_data->doRemoveObserver(Traits::toVoidPtr(observer));
72 }
73 
74 template <typename ObserverType>
75 inline void Notifier<ObserverType>::addWeakPtrObserver(const std::weak_ptr<ObserverType>& observer) {
76  m_data->doAddWeakRefObserver(Traits::toVoidPtr(observer.lock()));
77 }
78 
79 template <typename ObserverType>
80 inline void Notifier<ObserverType>::removeWeakPtrObserver(const std::weak_ptr<ObserverType>& observer) {
81  m_data->doRemoveObserver(Traits::toVoidPtr(observer.lock()));
82 }
83 
84 template <typename ObserverType>
85 inline void Notifier<ObserverType>::notifyObservers(std::function<void(const std::shared_ptr<ObserverType>&)> notify) {
86  m_data->doNotifyObservers(Traits::adaptFunction(std::move(notify)));
87 }
88 
89 template <typename ObserverType>
91  std::function<void(const std::shared_ptr<ObserverType>&)> notify) {
92  return m_data->doNotifyObserversInReverse(Traits::adaptFunction(std::move(notify)));
93 }
94 
95 template <typename ObserverType>
97  std::function<void(const std::shared_ptr<ObserverType>&)> addObserverFunc) {
98  m_data->doSetAddObserverFunction(Traits::adaptFunction(std::move(addObserverFunc)));
99 }
100 
101 } // namespace notifier
102 } // namespace alexaClientSDK
103 
104 #endif // ACSDK_NOTIFIER_NOTIFIER_H_
void addObserver(const std::shared_ptr< ObserverType > &observer) override
Add an observer.
Definition: Notifier.h:65
void removeWeakPtrObserver(const std::weak_ptr< ObserverType > &observer) override
Remove an observer.
Definition: Notifier.h:80
Generic implementation of NotifierInterface.
Definition: Notifier.h:38
void notifyObservers(std::function< void(const std::shared_ptr< ObserverType > &)> notify) override
Notify the observers in the order that they were added.
Definition: Notifier.h:85
void setAddObserverFunction(std::function< void(const std::shared_ptr< ObserverType > &)> addObserverFunc) override
Set a function for notifications on an observer additions.
Definition: Notifier.h:96
void removeObserver(const std::shared_ptr< ObserverType > &observer) override
Remove an observer.
Definition: Notifier.h:70
void addWeakPtrObserver(const std::weak_ptr< ObserverType > &observer) override
Add a weak reference to an observer.
Definition: Notifier.h:75
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
bool notifyObserversInReverse(std::function< void(const std::shared_ptr< ObserverType > &)> notify) override
Notify the observers in the reverse order that they were added.
Definition: Notifier.h:90
Notifier()
Definition: Notifier.h:61
std::unique_ptr< DataInterface > createDataInterface()
Constructs a new instance of DataInterface.
const T & move(const T &t)
Definition: gtest-port.h:1317
Generic observer operations interface.
Definition: NotifierInterface.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