cesium-native  0.41.0
Promise.h
1 #pragma once
2 
3 #include "Impl/AsyncSystemSchedulers.h"
4 #include "Impl/cesium-async++.h"
5 
6 #include <exception>
7 #include <memory>
8 
9 namespace CesiumAsync {
10 
11 template <typename T> class Future;
12 
18 template <typename T> class Promise {
19 public:
25  void resolve(T&& value) const { this->_pEvent->set(std::move(value)); }
26 
32  void resolve(const T& value) const { this->_pEvent->set(value); }
33 
39  template <typename TException> void reject(TException error) const {
40  this->_pEvent->set_exception(std::make_exception_ptr(error));
41  }
42 
49  void reject(const std::exception_ptr& error) const {
50  this->_pEvent->set_exception(error);
51  }
52 
61  Future<T> getFuture() const {
62  return Future<T>(this->_pSchedulers, this->_pEvent->get_task());
63  }
64 
65 private:
66  Promise(
67  const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
68  const std::shared_ptr<async::event_task<T>>& pEvent) noexcept
69  : _pSchedulers(pSchedulers), _pEvent(pEvent) {}
70 
71  std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
72  std::shared_ptr<async::event_task<T>> _pEvent;
73 
74  friend class AsyncSystem;
75 };
76 
77 // Specialization for promises that resolve to no value.
78 template <> class Promise<void> {
79 public:
80  void resolve() const { this->_pEvent->set(); }
81  template <typename TException> void reject(TException error) const {
82  this->_pEvent->set_exception(std::make_exception_ptr(error));
83  }
84  void reject(const std::exception_ptr& error) const {
85  this->_pEvent->set_exception(error);
86  }
87  Future<void> getFuture() const {
88  return Future<void>(this->_pSchedulers, this->_pEvent->get_task());
89  }
90 
91 private:
92  Promise(
93  const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
94  const std::shared_ptr<async::event_task<void>>& pEvent) noexcept
95  : _pSchedulers(pSchedulers), _pEvent(pEvent) {}
96 
97  std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
98  std::shared_ptr<async::event_task<void>> _pEvent;
99 
100  friend class AsyncSystem;
101 };
102 
103 } // namespace CesiumAsync
A system for managing asynchronous requests and tasks.
Definition: AsyncSystem.h:36
A value that will be available in the future, as produced by AsyncSystem.
Definition: Future.h:29
A promise that can be resolved or rejected by an asynchronous task.
Definition: Promise.h:18
Future< T > getFuture() const
Gets the Future that resolves or rejects when this Promise is resolved or rejected.
Definition: Promise.h:61
void resolve(const T &value) const
Will be called when the task completed successfully.
Definition: Promise.h:32
void reject(const std::exception_ptr &error) const
Will be called when the task failed.
Definition: Promise.h:49
void resolve(T &&value) const
Will be called when the task completed successfully.
Definition: Promise.h:25
void reject(TException error) const
Will be called when the task failed.
Definition: Promise.h:39
Classes that support asynchronous operations.