cesium-native 0.44.2
Loading...
Searching...
No Matches
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#include <utility>
9
10namespace CesiumAsync {
11
12template <typename T> class Future;
13
19template <typename T> class Promise {
20public:
26 void resolve(T&& value) const { this->_pEvent->set(std::move(value)); }
27
33 void resolve(const T& value) const { this->_pEvent->set(value); }
34
40 template <typename TException> void reject(TException error) const {
41 this->_pEvent->set_exception(std::make_exception_ptr(std::move(error)));
42 }
43
50 void reject(const std::exception_ptr& error) const {
51 this->_pEvent->set_exception(error);
52 }
53
63 return Future<T>(this->_pSchedulers, this->_pEvent->get_task());
64 }
65
66private:
67 Promise(
68 const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
69 const std::shared_ptr<async::event_task<T>>& pEvent) noexcept
70 : _pSchedulers(pSchedulers), _pEvent(pEvent) {}
71
72 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
73 std::shared_ptr<async::event_task<T>> _pEvent;
74
75 friend class AsyncSystem;
76};
77
81template <> class Promise<void> {
82public:
86 void resolve() const { this->_pEvent->set(); }
87
93 template <typename TException> void reject(TException error) const {
94 this->_pEvent->set_exception(std::make_exception_ptr(std::move(error)));
95 }
96
103 void reject(const std::exception_ptr& error) const {
104 this->_pEvent->set_exception(error);
105 }
110 return Future<void>(this->_pSchedulers, this->_pEvent->get_task());
111 }
112
113private:
114 Promise(
115 const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
116 const std::shared_ptr<async::event_task<void>>& pEvent) noexcept
117 : _pSchedulers(pSchedulers), _pEvent(pEvent) {}
118
119 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
120 std::shared_ptr<async::event_task<void>> _pEvent;
121
122 friend class AsyncSystem;
123};
124
125} // namespace CesiumAsync
A value that will be available in the future, as produced by AsyncSystem.
Definition Promise.h:12
void resolve() const
Will be called when the task completed successfully.
Definition Promise.h:86
void reject(const std::exception_ptr &error) const
Will be called when the task failed.
Definition Promise.h:103
Future< void > getFuture() const
Gets the Future that resolves or rejects when this Promise is resolved or rejected.
Definition Promise.h:109
void reject(TException error) const
Will be called when the task failed.
Definition Promise.h:93
A promise that can be resolved or rejected by an asynchronous task.
Definition Promise.h:19
void resolve(const T &value) const
Will be called when the task completed successfully.
Definition Promise.h:33
void reject(const std::exception_ptr &error) const
Will be called when the task failed.
Definition Promise.h:50
void resolve(T &&value) const
Will be called when the task completed successfully.
Definition Promise.h:26
void reject(TException error) const
Will be called when the task failed.
Definition Promise.h:40
Future< T > getFuture() const
Gets the Future that resolves or rejects when this Promise is resolved or rejected.
Definition Promise.h:62
Classes that support asynchronous operations.