diff --git a/src/sled/futures/future.h b/src/sled/futures/future.h index b874cec..9cedbed 100644 --- a/src/sled/futures/future.h +++ b/src/sled/futures/future.h @@ -1,6 +1,7 @@ #ifndef SLED_FUTURES_FUTURE_H #define SLED_FUTURES_FUTURE_H +#include #pragma once #include "sled/exec/detail/invoke_result.h" #include "sled/futures/internal/failure_handling.h" @@ -82,6 +83,28 @@ public: Future &operator=(Future &&) noexcept = default; ~Future() = default; + Future(const T &value) noexcept + { + static_assert(!std::is_same::value, "T and FailureT must be different types"); + data_ = Future::Create().data_; + FillSuccess(value); + } + + Future(T &&value) noexcept + { + static_assert(!std::is_same::value, "T and FailureT must be different types"); + data_ = Future::Create().data_; + FillSuccess(std::move(value)); + } + + template::value>> + Future(const FailureT &failure) noexcept + { + static_assert(!std::is_same::value, "T and FailureT must be different types"); + data_ = Future::Create().data_; + FillFailure(failure); + } + bool operator==(const Future &other) const noexcept { return data_ == other.data_; } bool operator!=(const Future &other) const noexcept { return !operator==(other); } @@ -359,7 +382,7 @@ public: return result; } - static Future successful(const T &value) noexcept + static Future Successful(const T &value) noexcept { Future result = Future::Create(); result.FillSuccess(value); diff --git a/src/sled/futures/future_test.cc b/src/sled/futures/future_test.cc index 36ba3c9..67a1878 100644 --- a/src/sled/futures/future_test.cc +++ b/src/sled/futures/future_test.cc @@ -115,5 +115,13 @@ TEST_SUITE("future") CHECK_FALSE(f2.IsFailed()); auto f3 = sled::Future::AsyncValue("11"); CHECK_EQ(f3.Result(), "11"); + TEST_CASE("Constructor") + { + sled::Future f1 = 1; + sled::Future f2 = std::string("1"); + REQUIRE(f1.IsCompleted()); + REQUIRE(f2.IsFailed()); + REQUIRE_EQ(f1.Result(), 1); + REQUIRE_EQ(f2.FailureReason(), "1"); } } diff --git a/src/sled/futures/internal/promise.h b/src/sled/futures/internal/promise.h index 642f727..79201ca 100644 --- a/src/sled/futures/internal/promise.h +++ b/src/sled/futures/internal/promise.h @@ -11,32 +11,32 @@ template class Future; template -class Promise { +class Promise final { static_assert(!std::is_same::value, "Promise is not allowed. Use Promise instead"); static_assert(!std::is_same::value, "Promise<_, void> is not allowed. Use Promise<_, bool> instead"); public: - using Value = T; - Promise() = default; - Promise(const Promise &) noexcept = default; - Promise(Promise &&) noexcept = default; - Promise &operator=(const Promise &) noexcept = default; - Promise &operator=(Promise &&) noexcept = default; - ~Promise() = default; + using Value = T; + Promise() = default; + Promise(const Promise &) = default; + Promise(Promise &&) noexcept = default; + Promise &operator=(const Promise &) = default; + Promise &operator=(Promise &&) noexcept = default; + ~Promise() = default; Future GetFuture() const { return future_; }; 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 future_ = Future::Create(); + mutable Future future_ = Future::Create(); }; }// namespace sled diff --git a/src/sled/nonstd/fsm.h b/src/sled/nonstd/fsm.h index ba482ad..2e72ba6 100644 --- a/src/sled/nonstd/fsm.h +++ b/src/sled/nonstd/fsm.h @@ -61,7 +61,8 @@ struct is_invocable_test { }; template -using is_invocable = typename std::integral_constant(0)) == 1>::type; +using fsm_is_invocable = + typename std::integral_constant(0)) == 1>::type; #else #error "fsmlite requires C++11 support." #endif @@ -101,10 +102,10 @@ invoke(M T::*f, T1 &&obj, Args &&...args) template::value, - bool f2 = is_invocable::value, - bool f3 = is_invocable::value, - bool f4 = is_invocable::value> + bool f1 = fsm_is_invocable::value, + bool f2 = fsm_is_invocable::value, + bool f3 = fsm_is_invocable::value, + bool f4 = fsm_is_invocable::value> struct binary_fn_helper; template diff --git a/src/sled/uri.h b/src/sled/uri.h index 1658988..82c7322 100644 --- a/src/sled/uri.h +++ b/src/sled/uri.h @@ -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