AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
InMemoryCommunicationInvokeHandler.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 ACSDKCOMMUNICATION_INMEMORYCOMMUNICATIONINVOKEHANDLER_H_
17 #define ACSDKCOMMUNICATION_INMEMORYCOMMUNICATIONINVOKEHANDLER_H_
18 
19 #include <functional>
20 #include <mutex>
21 #include <unordered_map>
22 
25 
26 namespace alexaClientSDK {
27 namespace acsdkCommunication {
28 
33 template <typename ReturnType, typename... Types>
35  : public virtual acsdkCommunicationInterfaces::CommunicationInvokeHandlerInterface<ReturnType, Types...> {
36 public:
39  bool registerFunction(
40  const std::string& name,
42  functionImplementation) override;
44  override;
45  bool deregister(
46  const std::string& name,
48  functionImplementation) override;
50 
51 private:
52  // map of names to functions.
53  std::unordered_map<
55  std::weak_ptr<acsdkCommunicationInterfaces::FunctionInvokerInterface<ReturnType, Types...>>>
56  m_functions;
57  // guard to protect the functions.
58  std::mutex m_functionsGuard;
59 };
60 template <typename ReturnType, typename... Types>
62  const std::string& name,
64  functionImplementation) {
65  std::lock_guard<std::mutex> lock(m_functionsGuard);
66  auto it = m_functions.find(name);
67  if (it != m_functions.end()) {
68  if (!it->second.expired()) {
70  alexaClientSDK::avsCommon::utils::logger::LogEntry("InMemoryCommunicationHandler", "registerFunction")
71  .m("Function is already Registered")
72  .d("function", name));
73  return false;
74  }
75  // Erase if function is a nullptr;
76  m_functions.erase(it);
77  }
78  if (!functionImplementation) {
80  alexaClientSDK::avsCommon::utils::logger::LogEntry("InMemoryCommunicationHandler", "registerFunction")
81  .m("FunctionImplementation is a nullptr")
82  .d("function", name));
83  return false;
84  }
85  auto weakFunction = std::weak_ptr<acsdkCommunicationInterfaces::FunctionInvokerInterface<ReturnType, Types...>>(
86  functionImplementation);
87 
88  m_functions.insert({name, weakFunction});
89  return true;
90 }
91 template <typename ReturnType, typename... Types>
93  ReturnType,
94  Types...>::invoke(const std::string& name, Types... args) {
95  auto it = m_functions.find(name);
96  if (it == m_functions.end()) {
97  ACSDK_ERROR(alexaClientSDK::avsCommon::utils::logger::LogEntry("InMemoryCommunicationHandler", "invoke")
98  .m("Function is not Registered")
99  .d("function", name));
101  }
102  auto function = it->second.lock();
103  if (!function) {
104  ACSDK_ERROR(alexaClientSDK::avsCommon::utils::logger::LogEntry("InMemoryCommunicationHandler", "invoke")
105  .m("Function is expired")
106  .d("function", name));
107  std::lock_guard<std::mutex> lock(m_functionsGuard);
108  m_functions.erase(it);
110  }
111  auto result = function->functionToBeInvoked(name, args...);
113 }
114 
115 template <typename ReturnType, typename... Types>
117  const std::string& name,
119  functionImplementation) {
120  if (!functionImplementation) {
121  ACSDK_ERROR(alexaClientSDK::avsCommon::utils::logger::LogEntry("InMemoryCommunicationHandler", "deregister")
122  .m("FunctionImplementation is a nullptr")
123  .d("function", name));
124  return false;
125  }
126  std::lock_guard<std::mutex> lock(m_functionsGuard);
127  auto it = m_functions.find(name);
128  if (it == m_functions.end()) {
129  ACSDK_ERROR(alexaClientSDK::avsCommon::utils::logger::LogEntry("InMemoryCommunicationHandler", "deregister")
130  .m("Function is not Registered")
131  .d("function", name));
132  return false;
133  }
134  if (it->second.lock() != functionImplementation) {
135  ACSDK_ERROR(alexaClientSDK::avsCommon::utils::logger::LogEntry("InMemoryCommunicationHandler", "deregister")
136  .m("Function is Registered but does not match")
137  .d("function", name));
138  return false;
139  }
140  m_functions.erase(it);
141  return true;
142 }
143 } // namespace acsdkCommunication
144 } // namespace alexaClientSDK
145 
146 #endif // ACSDKCOMMUNICATION_INMEMORYCOMMUNICATIONINVOKEHANDLER_H_
def args
Definition: android_test.py:111
bool registerFunction(const std::string &name, std::shared_ptr< acsdkCommunicationInterfaces::FunctionInvokerInterface< ReturnType, Types... >> functionImplementation) override
}@
Definition: InMemoryCommunicationInvokeHandler.h:61
Definition: InMemoryCommunicationInvokeHandler.h:34
::std::string string
Definition: gtest-port.h:1097
alexaClientSDK::avsCommon::utils::error::SuccessResult< ReturnType > invoke(const std::string &name, Types... args) override
}@
Definition: InMemoryCommunicationInvokeHandler.h:94
static SuccessResult< TValue > failure()
Definition: SuccessResult.h:87
std::mutex m
Definition: AlexaPresentationTest.cpp:91
static SuccessResult< TValue > success(TValue value)
Definition: SuccessResult.h:92
bool deregister(const std::string &name, const std::shared_ptr< acsdkCommunicationInterfaces::FunctionInvokerInterface< ReturnType, Types... >> &functionImplementation) override
}@
Definition: InMemoryCommunicationInvokeHandler.h:116
#define ACSDK_ERROR(entry)
Definition: Logger.h:481
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
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