Flutter Impeller
promise.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_BASE_PROMISE_H_
6 #define FLUTTER_IMPELLER_BASE_PROMISE_H_
7 
8 #include <future>
9 
10 namespace impeller {
11 
12 template <class T>
13 std::future<T> RealizedFuture(T t) {
14  std::promise<T> promise;
15  auto future = promise.get_future();
16  promise.set_value(std::move(t));
17  return future;
18 }
19 
20 // Wraps a std::promise and completes the promise with a value during
21 // destruction if the promise does not already have a value.
22 //
23 // By default the std::promise destructor will complete an empty promise with an
24 // exception. This will fail because Flutter is built without exception support.
25 template <typename T>
27  public:
28  NoExceptionPromise() = default;
29 
31  if (!value_set_) {
32  promise_.set_value({});
33  }
34  }
35 
36  std::future<T> get_future() { return promise_.get_future(); }
37 
38  void set_value(const T& value) {
39  promise_.set_value(value);
40  value_set_ = true;
41  }
42 
43  private:
44  std::promise<T> promise_;
45  bool value_set_ = false;
46 };
47 
48 } // namespace impeller
49 
50 #endif // FLUTTER_IMPELLER_BASE_PROMISE_H_
void set_value(const T &value)
Definition: promise.h:38
std::future< T > get_future()
Definition: promise.h:36
int32_t value
std::future< T > RealizedFuture(T t)
Definition: promise.h:13