feat update
All checks were successful
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m41s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 56s

This commit is contained in:
tqcq 2024-03-25 18:24:09 +08:00
parent 06451b44a2
commit 4077bebfc7
2 changed files with 66 additions and 1 deletions

View File

@ -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 <memory>
#include <type_traits>
namespace sled {
template<typename T>
class Promise;
template<typename T>
class FutureState {
public:
T Get()
{
sled::MutexLock lock(&mutex_);
cv_.Wait(&mutex_, [this]() { return done_; });
return sled::any_cast<T>(value_);
}
void SetError(std::exception_ptr e)
{
sled::MutexLock lock(&mutex_);
value_ = e;
done_ = true;
};
template<typename U = T>
typename std::enable_if<!std::is_void<T>::value && std::is_convertible<U, T>::value>::type SetValue(U &&value)
{
sled::MutexLock lock(&mutex_);
value_ = static_cast<T>(std::forward<U>(value));
done_ = true;
}
template<typename U = T>
typename std::enable_if<std::is_void<T>::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<typename T>
class BaseFuture {
public:
template<typename T>
T Get() const
{
return state_->Get();
}
private:
std::shared_ptr<FutureState<T>> state_;
};
}// namespace sled
#endif// SLED_FUTURES_DETAIL_BASE_FUTURE_H

View File

@ -1,5 +1,4 @@
#include <gtest/gtest.h>
#include <sled/futures/promise.h>
TEST(Promise, Basic)
{