diff --git a/include/sled/futures/detail/base_future.h b/include/sled/futures/detail/base_future.h new file mode 100644 index 0000000..bbaff54 --- /dev/null +++ b/include/sled/futures/detail/base_future.h @@ -0,0 +1,66 @@ +#ifndef SLED_FUTURES_DETAIL_BASE_FUTURE_H +#define SLED_FUTURES_DETAIL_BASE_FUTURE_H +#include "sled/any.h" +#include "sled/synchronization/mutex.h" +#include +#include + +namespace sled { + +template +class Promise; + +template +class FutureState { +public: + T Get() + { + sled::MutexLock lock(&mutex_); + cv_.Wait(&mutex_, [this]() { return done_; }); + return sled::any_cast(value_); + } + + void SetError(std::exception_ptr e) + { + sled::MutexLock lock(&mutex_); + value_ = e; + done_ = true; + }; + + template + typename std::enable_if::value && std::is_convertible::value>::type SetValue(U &&value) + { + sled::MutexLock lock(&mutex_); + value_ = static_cast(std::forward(value)); + done_ = true; + } + + template + typename std::enable_if::value>::type SetValue(U &&value) + { + sled::MutexLock lock(&mutex_); + done_ = true; + } + +private: + sled::Mutex mutex_; + sled::ConditionVariable cv_; + sled::any value_; + bool done_{false}; +}; + +template +class BaseFuture { +public: + template + T Get() const + { + return state_->Get(); + } + +private: + std::shared_ptr> state_; +}; + +}// namespace sled +#endif// SLED_FUTURES_DETAIL_BASE_FUTURE_H diff --git a/src/futures/promise_test.cc b/src/futures/promise_test.cc index db6f6dc..fffe6f1 100644 --- a/src/futures/promise_test.cc +++ b/src/futures/promise_test.cc @@ -1,5 +1,4 @@ #include -#include TEST(Promise, Basic) {