userver: engine::Promise< T > Class Template Reference
Loading...
Searching...
No Matches
engine::Promise< T > Class Template Reference

Detailed Description

template<typename T>
class engine::Promise< T >

std::promise replacement for asynchronous tasks that works in pair with engine::Future

engine::Promise can be used both from coroutines and from non-coroutine threads.

Example usage:

engine::Promise<int> int_promise;
auto deadline = GetDeadline();
constexpr auto kBadValue = -1;
constexpr auto kFallbackString = "Bad string";
constexpr auto kTestValue = 777;
constexpr auto kTestString = "Test string value";
auto int_future = int_promise.get_future();
auto string_future = string_promise.get_future();
auto calc_task =
utils::Async("calc", [int_promise = std::move(int_promise),
string_promise = std::move(string_promise),
kTestValue]() mutable {
// Emulating long calculation of x and s...
int_promise.set_value(kTestValue);
string_promise.set_value(kTestString);
// Other calculations.
});
auto int_consumer = utils::Async(
"int_consumer",
[deadline, int_future = std::move(int_future), kTestValue]() mutable {
auto status = int_future.wait_until(deadline);
auto x = kBadValue;
switch (status) {
x = int_future.get();
ASSERT_EQ(x, kTestValue);
// ...
break;
// Timeout Handling
break;
// Handling cancellation of calculations
// (example, return to queue)
return;
}
});
auto string_consumer = utils::Async(
"string_consumer",
[string_future = std::move(string_future), kTestString]() mutable {
std::string s;
try {
s = string_future.get();
ASSERT_EQ(s, kTestString);
} catch (const std::exception& ex) {
// Exception Handling
s = kFallbackString;
}
// ...
});
calc_task.Get();
int_consumer.Get();
string_consumer.Get();
See also
Synchronization Primitives

Definition at line 122 of file future.hpp.

Public Member Functions

 Promise ()
 Creates a new asynchronous value store.
 
 Promise (const Promise &)=delete
 
 Promise (Promise &&) noexcept=default
 
Promiseoperator= (const Promise &)=delete
 
Promiseoperator= (Promise &&) noexcept
 
Future< T > get_future ()
 
void set_value (const T &)
 
void set_value (T &&)
 
void set_exception (std::exception_ptr ex)
 

Constructor & Destructor Documentation

◆ Promise()

template<typename T >
engine::Promise< T >::Promise ( )

Creates a new asynchronous value store.

Definition at line 235 of file future.hpp.

◆ ~Promise()

template<typename T >
engine::Promise< T >::~Promise ( )

Definition at line 246 of file future.hpp.

Member Function Documentation

◆ get_future()

template<typename T >
Future< T > engine::Promise< T >::get_future ( )

Retrieves the Future associated with this value store.

Exceptions
std::future_errorif the Future has already been retrieved.

Definition at line 258 of file future.hpp.

◆ operator=()

template<typename T >
Promise< T > & engine::Promise< T >::operator= ( Promise< T > && other)
noexcept

Definition at line 238 of file future.hpp.

◆ set_exception()

template<typename T >
void engine::Promise< T >::set_exception ( std::exception_ptr ex)

Stores an exception to be thrown on retrieval.

Exceptions
std::future_errorif a value or an exception has already been set.

Definition at line 273 of file future.hpp.

◆ set_value() [1/2]

template<typename T >
void engine::Promise< T >::set_value ( const T & value)

Stores a value for retrieval.

Exceptions
std::future_errorif a value or an exception has already been set.

Definition at line 263 of file future.hpp.

◆ set_value() [2/2]

template<typename T >
void engine::Promise< T >::set_value ( T && value)

Stores a value for retrieval.

Exceptions
std::future_errorif a value or an exception has already been set.

Definition at line 268 of file future.hpp.


The documentation for this class was generated from the following file: