AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
CommunicationProperty.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 ACSDKCOMMUNICATIONINTERFACES_COMMUNICATIONPROPERTY_H_
16 #define ACSDKCOMMUNICATIONINTERFACES_COMMUNICATIONPROPERTY_H_
17 
18 #include <functional>
19 
24 
25 namespace alexaClientSDK {
26 namespace acsdkCommunicationInterfaces {
27 
33 template <typename T>
35 public:
41  bool setValue(T newValue) {
42  std::unique_lock<std::mutex> lock(m_propertyMutex);
43  m_value = newValue;
44  auto weakSubscriptionProxy = m_weakSubscriptionProxy;
45  auto propertyName = m_name;
48  m_staticExecutor.execute([weakSubscriptionProxy, newValue, propertyName]() {
49  weakSubscriptionProxy->notifyObservers(
50  [=](const std::shared_ptr<CommunicationPropertyChangeSubscriber<T>>& obs) {
51  obs->onCommunicationPropertyChange(propertyName, newValue);
52  });
53  });
54  return true;
55  }
56 
62  T getValue() {
63  std::unique_lock<std::mutex> lock(m_propertyMutex);
64  return m_value;
65  }
66 
74  static std::shared_ptr<CommunicationProperty<T>> create(const std::string& name, T initValue, bool writeable) {
75  return std::shared_ptr<CommunicationProperty<T>>(new CommunicationProperty(name, initValue, writeable));
76  }
77 
82  bool isWriteable() {
83  return m_writeable;
84  }
85 
91  bool addSubscriber(const std::weak_ptr<CommunicationPropertyChangeSubscriber<T>>& subscriber) {
92  m_weakSubscriptionProxy->addWeakPtrObserver(subscriber);
93  return !subscriber.expired();
94  }
95 
100  void removeSubscriber(const std::shared_ptr<CommunicationPropertyChangeSubscriber<T>>& subscriber) {
101  m_weakSubscriptionProxy->removeWeakPtrObserver(subscriber);
102  }
103 
104 private:
108  bool notifyOnCommunicationPropertyChange(const std::string& propertyName, T newValue) {
109  m_weakSubscriptionProxy->notifyObservers(
110  [=](const std::shared_ptr<CommunicationPropertyChangeSubscriber<T>>& obs) {
111  obs->onCommunicationPropertyChange(propertyName, newValue);
112  });
113  return true;
114  }
115 
122  CommunicationProperty<T>(std::string name, T initValue, bool writeable) :
123  m_weakSubscriptionProxy{std::make_shared<notifier::Notifier<CommunicationPropertyChangeSubscriber<T>>>()},
124  m_name{std::move(name)},
125  m_value{std::move(initValue)},
126  m_writeable{writeable} {
127  }
128 
129 private:
131  const std::shared_ptr<notifier::Notifier<CommunicationPropertyChangeSubscriber<T>>> m_weakSubscriptionProxy;
132 
134  const std::string m_name;
135 
137  T m_value;
138 
140  const bool m_writeable;
141 
143  std::mutex m_propertyMutex;
144 
146  static avsCommon::utils::threading::Executor m_staticExecutor;
147 };
148 
149 template <typename T>
151 
152 } // namespace acsdkCommunicationInterfaces
153 } // namespace alexaClientSDK
154 
155 #endif // ACSDKCOMMUNICATIONINTERFACES_COMMUNICATIONPROPERTY_H_
bool execute(std::function< void()> &&function) noexcept
Schedules a function for execution.
void removeSubscriber(const std::shared_ptr< CommunicationPropertyChangeSubscriber< T >> &subscriber)
Definition: CommunicationProperty.h:100
::std::string string
Definition: gtest-port.h:1097
Single-thread executor implementation.
Definition: Executor.h:45
bool isWriteable()
Definition: CommunicationProperty.h:82
static std::shared_ptr< CommunicationProperty< T > > create(const std::string &name, T initValue, bool writeable)
Definition: CommunicationProperty.h:74
bool addSubscriber(const std::weak_ptr< CommunicationPropertyChangeSubscriber< T >> &subscriber)
Definition: CommunicationProperty.h:91
bool setValue(T newValue)
Definition: CommunicationProperty.h:41
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
const T & move(const T &t)
Definition: gtest-port.h:1317

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