AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
NotifierTraits.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_INTERNAL_NOTIFIERTRAITS_H_
17 #define ACSDK_NOTIFIER_INTERNAL_NOTIFIERTRAITS_H_
18 
19 #include <functional>
20 #include <memory>
21 
22 namespace alexaClientSDK {
23 namespace notifier {
24 
34 template <class T>
35 class NotifierTraits {
36 public:
37  // This type is not constructable.
38  NotifierTraits() noexcept = delete;
39  ~NotifierTraits() noexcept = delete;
40  void operator=(const NotifierTraits<T>&) noexcept = delete;
41 
53  static std::shared_ptr<void> toVoidPtr(const std::shared_ptr<T>& src) noexcept;
55 
67  static std::shared_ptr<T> fromVoidPtr(const std::shared_ptr<void>& src) noexcept;
68 
81  static std::function<void(const std::shared_ptr<void>&)> adaptFunction(
82  std::function<void(const std::shared_ptr<T>&)>&& notify) noexcept;
83 
84 private:
95  static void invokeNotify(
96  const std::function<void(const std::shared_ptr<T>&)>& notify,
97  const std::shared_ptr<void>& data) noexcept;
98 };
99 
100 template <class T>
101 std::shared_ptr<void> NotifierTraits<T>::toVoidPtr(const std::shared_ptr<T>& src) noexcept {
102  return std::static_pointer_cast<void>(src);
103 }
104 
105 template <class T>
106 std::shared_ptr<T> NotifierTraits<T>::fromVoidPtr(const std::shared_ptr<void>& src) noexcept {
107  return std::static_pointer_cast<T>(src);
108 }
109 
110 template <class T>
111 std::function<void(const std::shared_ptr<void>&)> NotifierTraits<T>::adaptFunction(
112  std::function<void(const std::shared_ptr<T>&)>&& notify) noexcept {
113  if (notify) {
114  return std::bind(&NotifierTraits<T>::invokeNotify, std::move(notify), std::placeholders::_1);
115  } else {
116  return nullptr;
117  }
118 }
119 
120 template <class T>
121 void NotifierTraits<T>::invokeNotify(
122  const std::function<void(const std::shared_ptr<T>&)>& notify,
123  const std::shared_ptr<void>& data) noexcept {
124  notify(fromVoidPtr(data));
125 }
126 
127 } // namespace notifier
128 } // namespace alexaClientSDK
129 
130 #endif // ACSDK_NOTIFIER_INTERNAL_NOTIFIERTRAITS_H_
Definition: CapabilityConfiguration.h:190
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
const
Definition: upload.py:398
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