25 StateBase(Alloc&& a) :
SharedObj(forward<Alloc>(a)),
ready(false), futureRetrieved(false) {}
30 if (!
ready)
for (
auto& e : onReady) e(*
this);
33 void setException(
const Exception::ConstPtr& e,
bool setReady =
true)
38 if (setReady) setReady_();
44 void addOnReady(Func&& f)
46 SharedPtr<StateBase> this_;
57 Exception::ConstPtr ex;
60 ConditionLock waiters;
63 vector<mt::Funcptr<void (StateBase& src)>> onReady;
71 for (
auto& e : onReady) e(*
this);
77 struct State : StateBase
80 State(Alloc&& a) : StateBase(forward<Alloc>(a))
debug_if(, _result(&result())) {}
85 if (ready && !ex) result().~R();
89 void setValue(T&& val,
bool setReady =
true)
92 if (ready)
throw_ AlreadySatisfied();
93 new (&_storage) R(forward<T>(val));
94 if (setReady) setReady_();
97 R& result() {
return reinterpret_cast<R&
>(_storage); }
98 const R& result()
const {
return reinterpret_cast<const R&
>(_storage); }
101 typedef typename std::aligned_storage<sizeof(R), alignof(R)>::type Storage;
108 struct State<R&> : StateBase
110 template<
class Alloc>
111 State(Alloc&& a) : StateBase(forward<Alloc>(a)), _result(nullptr) {}
114 void setValue(T& val,
bool setReady =
true)
117 if (ready)
throw_ AlreadySatisfied();
119 if (setReady) setReady_();
122 R& result() {
assert(_result);
return *_result; }
123 const R& result()
const {
assert(_result);
return *_result; }
130 struct State<void> : StateBase
132 template<
class Alloc>
133 State(Alloc&& a) : StateBase(forward<Alloc>(a)) {}
135 void setValue(
bool setReady =
true)
138 if (ready)
throw_ AlreadySatisfied();
139 if (setReady) setReady_();
147 typedef State<R> State;
149 template<
class F,
class... Args>
150 void operator()(State& state,
bool setReady, F&& f, Args&&... args)
152 try { state.setValue(f(forward<Args>(args)...), setReady); }
161 typedef State<void> State;
163 template<
class F,
class... Args>
164 void operator()(State& state,
bool setReady, F&& f, Args&&... args)
166 try { f(forward<Args>(args)...); state.setValue(setReady); }
174 template<
class R>
class Future;
186 typedef future::priv::State<R>
State;
189 template<
class Alloc = std::allocator<State>>
193 Alloc_ a_ = forward<Alloc>(a);
194 _state.set(
new (a_.allocate(1)) State(a_));
212 _state->futureRetrieved =
true;
239 bool valid()
const {
return _state; }
247 void finalize() {
if (valid() && !_state->ready) setException(
new future::Broken()); }
249 SharedPtr<State> _state;
A scoped lock that references any lockable. Locks on construction and unlocks on destruction.
Definition: Mutex.h:10
Promise(Alloc &&a=Alloc())
Construct with allocator for shared state.
Definition: Promise.h:190
Unique future, guarantees sole access to a future function result.
Definition: Future.h:53
Inherit to declare that class is not copyable.
Definition: Meta.h:286
the future result is ready
Future< R > future()
Get future from which delayed result can be retrieved.
Definition: Promise.h:207
Promise(Promise &&rhs) noexcept
Definition: Promise.h:197
Reference-counted object for intrusive shared pointers.
Definition: SharedPtr.h:93
Container to hold a delayed function result.
Definition: Promise.h:181
State & __state() const
Get the shared state.
Definition: Promise.h:242
static auto _
Definition: Module.cpp:8
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
Promise & operator=(Promise &&rhs)
Definition: Promise.h:200
void setException(const Exception::ConstPtr &e)
Set stored exception.
Definition: Promise.h:236
#define EXCEPTION(Class)
Declares methods required for every subclass of honey::Exception.
Definition: Exception.h:17
std::enable_if< mt::True< T >::value &&mt::isRef< R >::value >::type setValue(T &val)
Set stored result for ref result type.
Definition: Promise.h:227
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
void setValue()
Set stored result for void result type.
Definition: Promise.h:229
Always returns true. Can be used to force a clause to be type dependent.
Definition: Meta.h:31
A container that wraps a function so that its result is stored in a future when invoked.
Definition: PackagedTask.h:10
~Promise()
Definition: Promise.h:198
Base exception class. Exceptions inherited from this class provide debug info and can be thrown polym...
Definition: Exception.h:45
#define throw_
Use in place of throw keyword to throw a honey::Exception object polymorphically and provide debug in...
Definition: Exception.h:11
Functor to delete a pointer.
Definition: Allocator.h:161
Exceptions.
Definition: Promise.h:14
#define debug_if(...)
Evaluate expression in debug mode only, does nothing in final mode.
Definition: Debug.h:30
bool valid() const
Check if this instance has state and can be used. State can be transferred out to another instance th...
Definition: Promise.h:239
UniqueLock< ConditionLock > Scoped
Definition: Lock.h:36
static Ptr current()
Create a clone of the current exception caught with (...)
Definition: Exception.h:154
future::priv::State< R > State
Definition: Promise.h:186
Global Honeycomb namespace.