AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
PromiseFuturePair.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 ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_PROMISEFUTUREPAIR_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_PROMISEFUTUREPAIR_H_
18 
19 #include <future>
20 
21 namespace alexaClientSDK {
22 namespace avsCommon {
23 namespace utils {
24 
28 template <typename Type>
30 public:
34  PromiseFuturePair() : m_future{m_promise.get_future()} {
35  }
36 
41  void setValue(Type val) {
42  m_promise.set_value(val);
43  }
44 
50  bool waitFor(std::chrono::milliseconds timeout) {
51  auto future = m_future;
52  return future.wait_for(timeout) == std::future_status::ready;
53  }
54 
59  auto future = m_future;
60  return future.get();
61  }
62 
63 private:
65  std::promise<Type> m_promise;
66 
68  std::shared_future<Type> m_future;
69 };
70 
74 template <>
75 class PromiseFuturePair<void> {
76 public:
80  PromiseFuturePair() : m_future{m_promise.get_future()} {
81  }
82 
86  void setValue() {
87  m_promise.set_value();
88  }
89 
95  bool waitFor(std::chrono::milliseconds timeout) {
96  auto future = m_future;
97  return future.wait_for(timeout) == std::future_status::ready;
98  }
99 
100 private:
102  std::promise<void> m_promise;
103 
105  std::shared_future<void> m_future;
106 };
107 
108 } // namespace utils
109 } // namespace avsCommon
110 } // namespace alexaClientSDK
111 
112 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_PROMISEFUTUREPAIR_H_
Type getValue()
Definition: PromiseFuturePair.h:58
Definition: PromiseFuturePair.h:29
void setValue(Type val)
Definition: PromiseFuturePair.h:41
PromiseFuturePair()
Definition: PromiseFuturePair.h:34
void setValue()
Definition: PromiseFuturePair.h:86
bool waitFor(std::chrono::milliseconds timeout)
Definition: PromiseFuturePair.h:95
PromiseFuturePair()
Definition: PromiseFuturePair.h:80
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
bool waitFor(std::chrono::milliseconds timeout)
Definition: PromiseFuturePair.h:50

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