31 void wait()
const {
wait(MonoClock::TimePoint::max()); }
33 template<
class Rep,
class Period>
36 template<
class Clock,
class Dur>
57 namespace future {
namespace priv
59 template<
class R>
struct wrappedResult {
typedef mt::tag<0> type; };
60 template<
class R>
struct wrappedResult<
Future<R>> {
typedef R type; };
62 template<
class R>
struct unwrapOnReady;
66 unwrapOnReady(Promise<R>&& promise) : promise(move(promise)) {}
67 void operator()(StateBase& src)
69 if (src.ready) src.ex ? promise.setException(src.ex) : promise.setValue(move(
static_cast<State<R>&
>(src).result()));
77 unwrapOnReady(Promise<void>&& promise) : promise(move(promise)) {}
78 void operator()(StateBase& src)
80 if (src.ready) src.ex ? promise.setException(src.ex) : promise.setValue();
83 Promise<void> promise;
88 template<
class Sub
class,
class R>
94 template<
class Sched,
class Func>
96 then(Sched&& sched, Func&& f);
106 template< class R2 = typename future::priv::wrappedResult<R>::type,
107 typename std::enable_if<mt::True<R2>::value && std::is_base_of<FutureBase, R>::value,
int>::type=0>
110 using namespace future::priv;
113 auto future = promise.future();
117 onReady(
Promise<R2>&& promise) : promise(move(promise)) {}
118 void operator()(StateBase& src)
123 this->promise.setException(src.ex);
126 auto& wrapped =
static_cast<State<R>&
>(src).result();
127 wrapped._state->addOnReady(*
new future::priv::unwrapOnReady<R>(move(this->promise)));
135 subc()._state->addOnReady(*
new onReady(move(promise)));
141 const Subclass& subc()
const {
return static_cast<const Subclass&
>(*this); }
142 Subclass& subc() {
return static_cast<Subclass&
>(*this); }
156 typedef future::priv::State<R>
State;
159 Future(
Future&& rhs) noexcept : _state(
nullptr) { operator=(move(rhs)); }
182 struct getResult {
static R_&& func(
const SharedPtr<State>& state) {
return move(state->result()); } };
184 struct getResult<R_&> {
static R_& func(
const SharedPtr<State>& state) {
return state->result(); } };
186 SharedPtr<State> _state;
193 R res = getResult<R>::func(_state);
194 auto ex = _state->ex;
204 auto ex = _state->ex;
210 template<class T, class R = typename std::decay<T>::type>
A scoped lock that references any lockable. Locks on construction and unlocks on destruction.
Definition: Mutex.h:10
R get()
Get the future result, waiting if necessary. Throws any exception stored in the result.
Definition: Future.h:190
TimePoint represented by a duration since a clock's epoch time.
Definition: TimePoint.h:11
virtual StateBase * stateBase() const
Definition: Future.h:176
static TimePoint now()
Get current time.
Definition: Clock.h:48
Future< typename std::result_of< Func(Subclass)>::type > then(Sched &&sched, Func &&f)
Append a continuation function that will be called when this future is ready. The ready future is pas...
future::Status wait(TimePoint< Clock, Dur > time) const
Wait until result is ready or until a certain time.
Definition: Future.h:37
Unique future, guarantees sole access to a future function result.
Definition: Future.h:53
bool valid() const
Check if this instance has state and can be used. State can be transferred out to another instance th...
Definition: Future.h:26
future::priv::State< R > State
Definition: Future.h:156
Inherit to declare that class is not copyable.
Definition: Meta.h:286
Shared future, allows multiple access to a future function result.
Definition: Future.h:54
void delete_(T *&p)
Destruct object, free memory and set pointer to null.
Definition: Allocator.h:75
State & __state() const
Get the shared state.
Definition: Future.h:173
Future< R2 > unwrap()
For wrapped futures Future>, returns a proxy Future that will be ready when the inner fu...
Definition: Future.h:108
the future result is ready
Future< R > future()
Get future from which delayed result can be retrieved.
Definition: Promise.h:207
Container to hold a delayed function result.
Definition: Promise.h:181
AllocatorObject< SmallAllocator > SmallAllocatorObject
Inherit from this class to use the small block allocator.
Definition: SmallAllocator.h:34
static auto _
Definition: Module.cpp:8
Future()
Definition: Future.h:158
mt::disable_if< mt::True< T >::value &&(mt::isRef< R >::value||std::is_void< R >::value)>::type setValue(T &&val)
Set stored result. Result is copy/move constructed from value.
Definition: Promise.h:223
Mixin for common future methods.
Definition: Future.h:90
SharedFuture< R > share()
Share the future between multiple future objects. This future is rendered invalid.
Definition: Future.h:164
bool ready() const
Check if result is ready.
Definition: Future.h:28
Objects that inherit from this class will use Alloc for new/delete ops.
Definition: Allocator.h:129
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Future & operator=(Future &&rhs)
Definition: Future.h:161
Future< R > FutureCreate(T &&val)
Create a future that is immediately ready with the value.
Definition: Future.h:211
static constexpr Duration max()
Maximum duration (positive reps)
Definition: Duration.h:86
Status
Definition: Future.h:12
Duration represented by repetitions of a period. The period must be a ratio.
Definition: Duration.h:7
Use to differentiate an overloaded function by type. Accepts dummy parameter default value: func(tag<...
Definition: Meta.h:39
#define throw_
Use in place of throw keyword to throw a honey::Exception object polymorphically and provide debug in...
Definition: Exception.h:11
R Result
Definition: Future.h:155
Future(Future &&rhs) noexcept
Definition: Future.h:159
future::priv::StateBase StateBase
Definition: Future.h:23
Global allocator for small memory blocks. To provide a custom pool define SmallAllocator_createSingle...
Definition: SmallAllocator.h:23
void wait() const
Wait until result is ready.
Definition: Future.h:31
future::Status wait(Duration< Rep, Period > time) const
Wait until result is ready or until an amount of time has passed.
Definition: Future.h:34
StateBase & __stateBase() const
Get the shared state.
Definition: Future.h:47
Base class for Future types.
Definition: Future.h:20
Inherit to enable non-virtual functor calling.
Definition: Meta.h:231
Global Honeycomb namespace.
virtual StateBase * stateBase() const =0