10 #include <type_traits>
19 template<
template<
class>
class DeriveT,
class T>
24 template<
class T,
class =
void>
25 struct PromiseParameterT
30 template<
class PromiseT>
31 struct PromiseParameterT<PromiseT,
32 std::void_t<std::enable_if_t<std::is_base_of_v<TypelessPromise, PromiseT>, int>>>
34 using type =
typename PromiseT::DataT;
40 template<
class T,
class =
void>
46 struct PromiseResult<T,
std::void_t<std::invoke_result_t<T>>>
48 using Res = std::invoke_result_t<T>;
50 using type = PromiseParameter<Res>;
57 template<
class T,
class P>
58 struct PromiseThenResultT
60 using Res = std::invoke_result_t<T, PromiseParameter<P>>;
66 template<
class T,
class P>
70 constexpr
auto isPromise = std::is_base_of_v<TypelessPromise, T>;
72 template<
template<
class>
class DeriveT,
class T>
78 static_assert(!isPromise<DataT>,
"Cannot create a promise of promise");
84 -> DeriveT<PromiseThenResult<FuncT, DataT>> {
85 return m_derived->then(std::forward<FuncT>(func));
89 DeriveT<T> *m_derived;
92 namespace PromiseCombination
98 template<
class T,
class =
void>
107 std::void_t<decltype(createDefaultForPromiseThen(std::declval<T>()))>>
126 template<
class DeriveT>
128 : m_d(
std::unique_ptr<Concept>(new Model<DeriveT>(
std::move(obj)))) {}
131 : m_d(that.m_d->clone())
135 : m_d(
std::move(that.m_d))
139 m_d = that.m_d->clone();
144 m_d = std::move(that.m_d);
150 if constexpr (std::is_same_v<std::invoke_result_t<F, DataT>,
void>) {
151 return m_d->thenVoid(f);
152 }
else if constexpr(
isPromise<std::invoke_result_t<F, DataT>>) {
153 return m_d->thenPromise(f);
155 return m_d->thenData(f);
159 bool ready()
const {
return m_d->ready(); }
165 virtual ~Concept() =
default;
169 virtual std::unique_ptr<Concept> clone()
const = 0;
170 virtual bool ready()
const = 0;
174 template<
class DeriveT>
175 struct Model :
public Concept
177 Model(DeriveT obj) : instance(
std::move(obj)) {}
178 ~Model()
override =
default;
180 return instance.then([=](
DataT v) {
186 return instance.then([=](
DataT v) {
return f(v); });
189 return instance.then([=](
DataT v) {
return f(v); });
191 std::unique_ptr<Concept> clone()
const override {
192 return std::unique_ptr<Concept>(
new Model<DeriveT>(instance));
194 bool ready()
const override {
195 return instance.ready();
198 return instance.get();
203 std::unique_ptr<Concept> m_d;
208 template<
class DeriveT,
template<
class>
class PromiseT>
214 template<
class T,
class FuncT>
215 auto create(FuncT &&func) -> PromiseT<T>;
224 template<
class DeriveT,
template<
class>
class PromiseT>
225 template<
class T,
class FuncT>
227 return m_derived->create(std::forward<FuncT>(func));
230 template<
class DeriveT,
template<
class>
class PromiseT>
233 return m_derived->createResolved(std::forward<T>(val));
245 template<
class DeriveT>
247 : m_d(
std::unique_ptr<Concept>(new Model<
std::decay_t<DeriveT>>(
std::move(obj)))) {
249 throw std::logic_error(
"promise handler is empty");
256 throw std::logic_error(
"promise handler is empty");
260 : m_d(
std::move(that.m_d)) {
262 throw std::logic_error(
"promise handler is empty");
272 m_d = std::move(that.m_d);
277 return m_d->create(f);
281 return m_d->createResolveToPromise(f);
285 return m_d->createResolved(v);
288 template<
class RangeT>
292 if (promises.empty()) {
295 auto p1 = *(promises.
begin());
296 promises.erase(promises.
begin());
297 if (promises.empty()) {
300 return p1.then([*
this, promises=std::move(promises)](
DataT val)
mutable {
301 return all(std::move(promises))
311 virtual ~Concept() =
default;
317 template<
class DeriveT>
318 struct Model :
public Concept
320 static_assert(std::is_same_v<std::decay_t<DeriveT>, DeriveT>,
"DeriveT must not be a reference");
321 Model(DeriveT obj) : instance(
std::move(obj)) {}
322 ~Model()
override =
default;
324 return instance.template create<DataT>(f);
327 return instance.template create<DataT>(f);
330 return instance.createResolved(v);
335 std::shared_ptr<Concept> m_d;
Definition: promise-interface.hpp:74
AbstractPromise(DeriveT< T > *obj)
Definition: promise-interface.hpp:80
auto then(FuncT &&func) -> DeriveT< PromiseThenResult< FuncT, DataT >>
Definition: promise-interface.hpp:83
T DataT
Definition: promise-interface.hpp:76
Definition: promise-interface.hpp:210
PromiseInterface(DeriveT *obj)
Definition: promise-interface.hpp:212
auto create(FuncT &&func) -> PromiseT< T >
Definition: promise-interface.hpp:226
auto createResolved(T &&val) -> PromiseT< T >
Definition: promise-interface.hpp:232
Definition: promise-interface.hpp:238
PromiseT all(RangeT promises) const
Definition: promise-interface.hpp:289
SingleTypePromiseInterface(DeriveT obj)
Definition: promise-interface.hpp:246
SingleTypePromiseInterface(SingleTypePromiseInterface &&that)
Definition: promise-interface.hpp:259
PromiseT createResolveToPromise(std::function< void(ResolveToPromiseT)> f) const
Definition: promise-interface.hpp:280
SingleTypePromiseInterface & operator=(const SingleTypePromiseInterface &that)
Definition: promise-interface.hpp:266
PromiseT create(std::function< void(ResolveT)> f) const
Definition: promise-interface.hpp:276
PromiseT createResolved(DataT v) const
Definition: promise-interface.hpp:284
SingleTypePromise< DataT > PromiseT
Definition: promise-interface.hpp:241
std::function< void(DataT)> ResolveT
Definition: promise-interface.hpp:242
std::function< void(PromiseT)> ResolveToPromiseT
Definition: promise-interface.hpp:243
SingleTypePromiseInterface(const SingleTypePromiseInterface &that)
Definition: promise-interface.hpp:253
SingleTypePromiseInterface & operator=(SingleTypePromiseInterface &&that)
Definition: promise-interface.hpp:271
T DataT
Definition: promise-interface.hpp:240
Definition: promise-interface.hpp:122
DataT get() const
Definition: promise-interface.hpp:160
T DataT
Definition: promise-interface.hpp:124
bool ready() const
Definition: promise-interface.hpp:159
SingleTypePromise & operator=(SingleTypePromise &&that)
Definition: promise-interface.hpp:143
SingleTypePromise(const SingleTypePromise &that)
Definition: promise-interface.hpp:130
SingleTypePromise then(F &&f)
Definition: promise-interface.hpp:149
SingleTypePromise(DeriveT obj)
Definition: promise-interface.hpp:127
SingleTypePromise & operator=(const SingleTypePromise &that)
Definition: promise-interface.hpp:138
SingleTypePromise(SingleTypePromise &&that)
Definition: promise-interface.hpp:134
Definition: promise-interface.hpp:16
A RangeT is an ordered collection that can be iterated through.
Definition: range-t.hpp:21
auto begin() const
The beginning iterator of this range.
constexpr bool dataCombineNone(bool)
Definition: promise-interface.hpp:96
constexpr bool createDefaultForPromiseThen(bool)
Definition: promise-interface.hpp:94
constexpr T defaultForPromiseThen(T)
Definition: promise-interface.hpp:113
constexpr bool dataCombine(bool a, bool b)
Definition: promise-interface.hpp:95
typename PromiseParameterT< T >::type PromiseParameter
Definition: promise-interface.hpp:38
typename PromiseResult< T >::type PromiseResultT
Definition: promise-interface.hpp:54
Definition: location.hpp:10
EffectStatus dataCombineNone(EffectStatus)
Definition: context.hpp:121
typename detail::PromiseThenResultT< T, P >::type PromiseThenResult
Definition: promise-interface.hpp:67
constexpr auto isPromise
Definition: promise-interface.hpp:70
EffectStatus dataCombine(EffectStatus a, EffectStatus b)
Definition: context.hpp:105
Definition: clientutil.hpp:217
constexpr T operator()() const
Definition: promise-interface.hpp:109
Definition: promise-interface.hpp:100
constexpr T operator()() const
Definition: promise-interface.hpp:101