mirror of
https://github.com/matrix-construct/construct
synced 2024-12-27 07:54:05 +01:00
ircd::ctx: Add nodejs-style future::then() rather than libstd experimental TS.
This commit is contained in:
parent
6d0c74a064
commit
37569559cf
3 changed files with 42 additions and 0 deletions
|
@ -58,6 +58,8 @@ class ircd::ctx::future
|
|||
T get();
|
||||
operator T() { return get(); }
|
||||
|
||||
void then(decltype(shared_state<T>::callback));
|
||||
|
||||
future();
|
||||
future(promise<T> &promise);
|
||||
};
|
||||
|
@ -82,6 +84,8 @@ class ircd::ctx::future<void>
|
|||
template<class duration> future_status wait(const duration &d) const;
|
||||
void wait() const;
|
||||
|
||||
void then(decltype(shared_state<void>::callback));
|
||||
|
||||
future();
|
||||
future(promise<void> &promise);
|
||||
};
|
||||
|
@ -136,6 +140,21 @@ ircd::ctx::future<T>::future(promise<T> &promise)
|
|||
{
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void
|
||||
ircd::ctx::future<T>::then(decltype(shared_state<T>::callback) cb)
|
||||
{
|
||||
assert(valid());
|
||||
st->callback = std::move(cb);
|
||||
}
|
||||
|
||||
inline void
|
||||
ircd::ctx::future<void>::then(decltype(shared_state<void>::callback) cb)
|
||||
{
|
||||
assert(valid());
|
||||
st->callback = std::move(cb);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T
|
||||
ircd::ctx::future<T>::get()
|
||||
|
|
|
@ -147,6 +147,10 @@ ircd::ctx::promise<T>::set_value(T&& val)
|
|||
st->val = std::move(val);
|
||||
st->finished = true;
|
||||
st->cond.notify_all();
|
||||
if(st->callback) ircd::post([st(st)]
|
||||
{
|
||||
st->callback(st->eptr, st->val);
|
||||
});
|
||||
}
|
||||
|
||||
inline void
|
||||
|
@ -156,6 +160,10 @@ ircd::ctx::promise<void>::set_value()
|
|||
assert(!finished());
|
||||
st->finished = true;
|
||||
st->cond.notify_all();
|
||||
if(st->callback) ircd::post([st(st)]
|
||||
{
|
||||
st->callback(st->eptr);
|
||||
});
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -167,6 +175,10 @@ ircd::ctx::promise<T>::set_value(const T &val)
|
|||
st->val = val;
|
||||
st->finished = true;
|
||||
st->cond.notify_all();
|
||||
if(st->callback) ircd::post([st(st)]
|
||||
{
|
||||
st->callback(st->eptr, st->val);
|
||||
});
|
||||
}
|
||||
|
||||
inline void
|
||||
|
@ -177,6 +189,10 @@ ircd::ctx::promise<void>::set_exception(std::exception_ptr eptr)
|
|||
st->eptr = std::move(eptr);
|
||||
st->finished = true;
|
||||
st->cond.notify_all();
|
||||
if(st->callback) ircd::post([st(st)]
|
||||
{
|
||||
st->callback(st->eptr);
|
||||
});
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -188,4 +204,8 @@ ircd::ctx::promise<T>::set_exception(std::exception_ptr eptr)
|
|||
st->eptr = std::move(eptr);
|
||||
st->finished = true;
|
||||
st->cond.notify_all();
|
||||
if(st->callback) ircd::post([st(st)]
|
||||
{
|
||||
st->callback(st->eptr, st->val);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ struct ircd::ctx::shared_state
|
|||
using reference_type = T &;
|
||||
|
||||
T val;
|
||||
std::function<void (std::exception_ptr, T) noexcept> callback;
|
||||
|
||||
std::shared_ptr<const shared_state<T>> share() const;
|
||||
std::shared_ptr<shared_state<T>> share();
|
||||
|
@ -48,6 +49,8 @@ struct ircd::ctx::shared_state<void>
|
|||
{
|
||||
using value_type = void;
|
||||
|
||||
std::function<void (std::exception_ptr) noexcept> callback;
|
||||
|
||||
std::shared_ptr<const shared_state<void>> share() const;
|
||||
std::shared_ptr<shared_state<void>> share();
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue