template<class T>
class FutureState : public FutureStateBase {
public:
-
void set_value(T value)
{
if (this->get_status() != FutureStatus::not_ready)
template<class T>
class Promise {
public:
- Promise() : state_(std::make_shared<FutureState<T>>()) {}
+ Promise() = default;
explicit Promise(std::shared_ptr<FutureState<T>> state) : state_(std::move(state)) {}
// Move type
Promise(Promise const&) = delete;
Promise& operator=(Promise const&) = delete;
- Promise(Promise&& that) :
- state_(std::move(that.state_)), future_get_(that.future_get_)
- {
- that.future_get_ = false;
- }
+ Promise(Promise&& that) : state_(std::move(that.state_)) { std::swap(future_get_, that.future_get_); }
Promise& operator=(Promise&& that)
{
}
private:
- std::shared_ptr<FutureState<T>> state_;
+ std::shared_ptr<FutureState<T>> state_{new FutureState<T>()};
bool future_get_ = false;
};
template<>
class Promise<void> {
public:
- Promise() : state_(std::make_shared<FutureState<void>>()) {}
+ Promise() = default;
explicit Promise(std::shared_ptr<FutureState<void>> state) : state_(std::move(state)) {}
~Promise()
{
// Move type
Promise(Promise const&) = delete;
Promise& operator=(Promise const&) = delete;
- Promise(Promise&& that) :
- state_(std::move(that.state_)), future_get_(that.future_get_)
- {
- that.future_get_ = false;
- }
+ Promise(Promise&& that) : state_(std::move(that.state_)) { std::swap(future_get_, that.future_get_); }
Promise& operator=(Promise&& that)
{
this->state_ = std::move(that.state_);
}
private:
- std::shared_ptr<FutureState<void>> state_;
+ std::shared_ptr<FutureState<void>> state_{new FutureState<void>()};
bool future_get_ = false;
};