Shaka Player Embedded
async_results.h
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_ASYNC_RESULTS_H_
16 #define SHAKA_EMBEDDED_ASYNC_RESULTS_H_
17 
18 #include <chrono>
19 #include <future>
20 #include <type_traits>
21 
22 #include "error.h"
23 #include "variant.h"
24 
25 namespace shaka {
26 
45 template <typename T>
46 class AsyncResults final {
47  public:
48  using result_type = typename std::conditional<std::is_same<T, void>::value,
51 
54 
56  AsyncResults(std::shared_future<variant_type> future) : future_(future) {}
57 
59  if (future_.valid())
60  future_.wait();
61  }
62 
63  // \cond Doxygen_Skip
64  AsyncResults(AsyncResults&&) = default;
65  AsyncResults& operator=(AsyncResults&&) = default;
66  AsyncResults(const AsyncResults&) = delete;
67  AsyncResults& operator=(const AsyncResults&) = delete;
68  // \endcond
69 
70 
72  bool valid() const {
73  return future_.valid();
74  }
75 
77  void wait() {
78  future_.wait();
79  }
80 
85  template <typename Rep, typename Period>
86  std::future_status wait_for(
87  const std::chrono::duration<Rep, Period>& timeout_duration) const {
88  return future_.wait_for(timeout_duration);
89  }
90 
95  template <typename Clock, typename Duration>
96  std::future_status wait_until(
97  std::chrono::time_point<Clock, Duration>& timeout_time) const {
98  return future_.wait_until(timeout_time);
99  }
100 
101 
106  bool has_error() const {
107  return holds_alternative<Error>(future_.get());
108  }
109 
114  template <typename U = T, typename = typename std::enable_if<
115  !std::is_same<U, void>::value>::type>
116  const U& results() const {
117  return get<T>(future_.get());
118  }
119 
124  const Error& error() const {
125  return get<Error>(future_.get());
126  }
127 
128  private:
129  std::shared_future<variant_type> future_;
130 };
131 
132 } // namespace shaka
133 
134 #endif // SHAKA_EMBEDDED_ASYNC_RESULTS_H_
AsyncResults(std::shared_future< variant_type > future)
Definition: async_results.h:56
const Error & error() const
typename std::conditional< std::is_same< T, void >::value, monostate, T >::type result_type
Definition: async_results.h:49
ExceptionCode type
const U & results() const
std::future_status wait_for(const std::chrono::duration< Rep, Period > &timeout_duration) const
Definition: async_results.h:86
bool valid() const
Definition: async_results.h:72
std::future_status wait_until(std::chrono::time_point< Clock, Duration > &timeout_time) const
Definition: async_results.h:96
bool has_error() const