cesium-native 0.43.0
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
9namespace CesiumAsync {
10
11template <typename T> class Future;
12
18template <typename T> class Promise {
19public:
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
62 return Future<T>(this->_pSchedulers, this->_pEvent->get_task());
63 }
64
65private:
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
80template <> class Promise<void> {
81public:
85 void resolve() const { this->_pEvent->set(); }
86
92 template <typename TException> void reject(TException error) const {
93 this->_pEvent->set_exception(std::make_exception_ptr(error));
94 }
95
102 void reject(const std::exception_ptr& error) const {
103 this->_pEvent->set_exception(error);
104 }
109 return Future<void>(this->_pSchedulers, this->_pEvent->get_task());
110 }
111
112private:
113 Promise(
114 const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
115 const std::shared_ptr<async::event_task<void>>& pEvent) noexcept
116 : _pSchedulers(pSchedulers), _pEvent(pEvent) {}
117
118 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
119 std::shared_ptr<async::event_task<void>> _pEvent;
120
121 friend class AsyncSystem;
122};
123
124} // namespace CesiumAsync
A value that will be available in the future, as produced by AsyncSystem.
Definition Promise.h:11
void resolve() const
Will be called when the task completed successfully.
Definition Promise.h:85
void reject(const std::exception_ptr &error) const
Will be called when the task failed.
Definition Promise.h:102
Future< void > getFuture() const
Gets the Future that resolves or rejects when this Promise is resolved or rejected.
Definition Promise.h:108
void reject(TException error) const
Will be called when the task failed.
Definition Promise.h:92
A promise that can be resolved or rejected by an asynchronous task.
Definition Promise.h:18
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
Future< T > getFuture() const
Gets the Future that resolves or rejects when this Promise is resolved or rejected.
Definition Promise.h:61
Classes that support asynchronous operations.