class scope
Defined at line 63 of file ../../sdk/lib/fit-promise/include/lib/fpromise/scope.h
Provides a mechanism for binding promises to the lifetime of another object
such that they are destroyed before that object goes out of scope. It is
particularly useful for ensuring that the lifetime of a promise does not
exceed the lifetime of any variables that it has captured by reference.
A scope is thread-safe but non-reentrant: it must not be destroyed while
any of its associated promises are running.
EXAMPLE
Define a |fpromise::scope| as a member of the object to whose lifetime the
promises should be bound.
// We mark this class final because its destructor has side-effects
// that rely on the order of destruction. If this object were
// subclassed there would be a possibility for promises bound to its
// scope to inadvertently access the subclass's state while the object
// was being destroyed.
class accumulator final {
public:
accumulator() = default;
~accumulator() = default;
fpromise::promise
<int
> accumulate(int value);
private:
int prior_total_ = 0;
// This member is last so that the scope is exited before all
// other members of the object are destroyed. Alternately, we
// could enforce this ordering by explicitly invoking
// |fpromise::scope::exit()| where appropriate.
fpromise::scope scope_;
};
Use |fpromise::promise::wrap_with()| to wrap up promises that capture pointers
to the object. In this example, the captured pointer is "this".
fpromise::promise
<int
> accumulator::accumulate(int value) {
return fpromise::make_promise([this, value] {
prior_total_ += value;
return fpromise::ok(prior_total_);
}).wrap_with(scope_); /* binding to scope happens here */
}
Public Methods
void scope ()
Creates a new scope.
Defined at line 9 of file ../../sdk/lib/fit-promise/scope.cc
void ~scope ()
Exits the scope and destroys all of its wrapped promises.
Asserts that no promises are currently running.
Defined at line 11 of file ../../sdk/lib/fit-promise/scope.cc
bool exited ()
Returns true if the scope has been exited.
This method is thread-safe.
Defined at line 75 of file ../../sdk/lib/fit-promise/include/lib/fpromise/scope.h
void exit ()
Exits the scope and destroys all of its wrapped promises.
Assets that no promises are currently running.
This method is thread-safe.
Defined at line 81 of file ../../sdk/lib/fit-promise/include/lib/fpromise/scope.h
template <typename Promise>
decltype(auto) wrap (Promise promise)
Returns a promise which wraps the specified |promise| and binds the
promise to this scope.
The specified promise will automatically be destroyed when its wrapper
is destroyed or when the scope is exited. If the scope has already
exited then the wrapped promise will be immediately destroyed.
When the returned promise is invoked before the scope is exited,
the promise that it wraps will be invoked as usual. However, when
the returned promise is invoked after the scope is exited, it
immediately returns a pending result (since the promise that it
previously wrapped has already been destroyed). By returning a
pending result, the return promise effectively indicates to the
executor that the task has been "abandoned" due to the scope being
exited.
This method is thread-safe.
Defined at line 101 of file ../../sdk/lib/fit-promise/include/lib/fpromise/scope.h
void scope (const scope & )
Defined at line 107 of file ../../sdk/lib/fit-promise/include/lib/fpromise/scope.h
void scope (scope && )
Defined at line 108 of file ../../sdk/lib/fit-promise/include/lib/fpromise/scope.h
scope & operator= (const scope & )
Defined at line 109 of file ../../sdk/lib/fit-promise/include/lib/fpromise/scope.h
scope & operator= (scope && )
Defined at line 110 of file ../../sdk/lib/fit-promise/include/lib/fpromise/scope.h