Merge remote-tracking branch 'origin/master'

This commit is contained in:
tqcq 2024-04-19 19:09:48 +08:00
commit 050ca0c0cc
5 changed files with 55 additions and 18 deletions

View File

@ -1,6 +1,7 @@
#ifndef SLED_FUTURES_FUTURE_H
#define SLED_FUTURES_FUTURE_H
#include <exception>
#pragma once
#include "sled/exec/detail/invoke_result.h"
#include "sled/futures/internal/failure_handling.h"
@ -82,6 +83,28 @@ public:
Future<T, FailureT> &operator=(Future<T, FailureT> &&) noexcept = default;
~Future() = default;
Future(const T &value) noexcept
{
static_assert(!std::is_same<T, FailureT>::value, "T and FailureT must be different types");
data_ = Future<T, FailureT>::Create().data_;
FillSuccess(value);
}
Future(T &&value) noexcept
{
static_assert(!std::is_same<T, FailureT>::value, "T and FailureT must be different types");
data_ = Future<T, FailureT>::Create().data_;
FillSuccess(std::move(value));
}
template<typename = typename std::enable_if<!std::is_same<T, FailureT>::value>>
Future(const FailureT &failure) noexcept
{
static_assert(!std::is_same<T, FailureT>::value, "T and FailureT must be different types");
data_ = Future<T, FailureT>::Create().data_;
FillFailure(failure);
}
bool operator==(const Future<T, FailureT> &other) const noexcept { return data_ == other.data_; }
bool operator!=(const Future<T, FailureT> &other) const noexcept { return !operator==(other); }
@ -359,7 +382,7 @@ public:
return result;
}
static Future<T, FailureT> successful(const T &value) noexcept
static Future<T, FailureT> Successful(const T &value) noexcept
{
Future<T, FailureT> result = Future<T, FailureT>::Create();
result.FillSuccess(value);

View File

@ -115,5 +115,13 @@ TEST_SUITE("future")
CHECK_FALSE(f2.IsFailed());
auto f3 = sled::Future<std::string>::AsyncValue("11");
CHECK_EQ(f3.Result(), "11");
TEST_CASE("Constructor")
{
sled::Future<int, std::string> f1 = 1;
sled::Future<int, std::string> f2 = std::string("1");
REQUIRE(f1.IsCompleted());
REQUIRE(f2.IsFailed());
REQUIRE_EQ(f1.Result(), 1);
REQUIRE_EQ(f2.FailureReason(), "1");
}
}

View File

@ -11,7 +11,7 @@ template<typename T, typename FailureT>
class Future;
template<typename T, typename FailureT = std::exception>
class Promise {
class Promise final {
static_assert(!std::is_same<T, void>::value, "Promise<void, _> is not allowed. Use Promise<bool, _> instead");
static_assert(!std::is_same<FailureT, void>::value,
"Promise<_, void> is not allowed. Use Promise<_, bool> instead");
@ -19,9 +19,9 @@ class Promise {
public:
using Value = T;
Promise() = default;
Promise(const Promise &) noexcept = default;
Promise(const Promise &) = default;
Promise(Promise &&) noexcept = default;
Promise &operator=(const Promise &) noexcept = default;
Promise &operator=(const Promise &) = default;
Promise &operator=(Promise &&) noexcept = default;
~Promise() = default;
@ -29,14 +29,14 @@ public:
bool IsFilled() const noexcept { return future_.IsCompleted(); }
void Failure(const FailureT &reason) { return future_.FillFailure(reason); }
void Failure(const FailureT &reason) const noexcept { return future_.FillFailure(reason); }
void Success(const T &value) { return future_.FillSuccess(value); }
void Success(const T &value) const noexcept { return future_.FillSuccess(value); }
void Success(T &&value) { return future_.FillSuccess(std::move(value)); }
void Success(T &&value) const noexcept { return future_.FillSuccess(std::move(value)); }
private:
Future<T, FailureT> future_ = Future<T, FailureT>::Create();
mutable Future<T, FailureT> future_ = Future<T, FailureT>::Create();
};
}// namespace sled

View File

@ -61,7 +61,8 @@ struct is_invocable_test {
};
template<class F, class... Args>
using is_invocable = typename std::integral_constant<bool, sizeof(is_invocable_test::test<F, Args...>(0)) == 1>::type;
using fsm_is_invocable =
typename std::integral_constant<bool, sizeof(is_invocable_test::test<F, Args...>(0)) == 1>::type;
#else
#error "fsmlite requires C++11 support."
#endif
@ -101,10 +102,10 @@ invoke(M T::*f, T1 &&obj, Args &&...args)
template<class F,
class Arg1,
class Arg2,
bool f1 = is_invocable<F>::value,
bool f2 = is_invocable<F, Arg1>::value,
bool f3 = is_invocable<F, Arg2>::value,
bool f4 = is_invocable<F, Arg1, Arg2>::value>
bool f1 = fsm_is_invocable<F>::value,
bool f2 = fsm_is_invocable<F, Arg1>::value,
bool f3 = fsm_is_invocable<F, Arg2>::value,
bool f4 = fsm_is_invocable<F, Arg1, Arg2>::value>
struct binary_fn_helper;
template<class F, class Arg1, class Arg2>

View File

@ -70,6 +70,11 @@ private:
bool has_authority_ = false;
};
#undef __SLED_URI_GETTER_AND_SETTER
#undef __SLED_URI_GETTER
#undef __SLED_URI_SETTER
}// namespace sled
#endif// SLED_URI_H