From ed9e57c5e81805835b3356f2988fbd7ef6ddea84 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sun, 21 Apr 2024 00:43:18 +0800 Subject: [PATCH] feat use meta --- src/sled/error/error.h | 14 ++++ src/sled/futures/future.h | 84 ++++++++++---------- src/sled/futures/future_test.cc | 13 ++- src/sled/futures/internal/failure_handling.h | 8 +- src/sled/meta/type_traits.h | 34 ++++++++ 5 files changed, 99 insertions(+), 54 deletions(-) create mode 100644 src/sled/error/error.h create mode 100644 src/sled/meta/type_traits.h diff --git a/src/sled/error/error.h b/src/sled/error/error.h new file mode 100644 index 0000000..7c94fed --- /dev/null +++ b/src/sled/error/error.h @@ -0,0 +1,14 @@ +#ifndef SLED_ERROR_ERROR_H +#define SLED_ERROR_ERROR_H + +#pragma once + +namespace sled { +class Error { +public: + template + static Error Wrap(); +}; +}// namespace sled + +#endif// SLED_ERROR_ERROR_H diff --git a/src/sled/futures/future.h b/src/sled/futures/future.h index a52bb07..ae7111d 100644 --- a/src/sled/futures/future.h +++ b/src/sled/futures/future.h @@ -8,6 +8,7 @@ #include "sled/futures/internal/promise.h" #include "sled/lang/attributes.h" #include "sled/log/log.h" +#include "sled/meta/type_traits.h" #include "sled/synchronization/event.h" #include "sled/synchronization/mutex.h" #include "sled/task_queue/task_queue_base.h" @@ -17,16 +18,16 @@ namespace sled { namespace future_detail { -template -struct is_invocable : std::is_constructible, - std::reference_wrapper::type>> {}; - -template -struct is_invocable_r : std::is_constructible, - std::reference_wrapper::type>> {}; - -template -using enable_if_t = typename std::enable_if::type; +// template +// struct is_invocable : std::is_constructible, +// std::reference_wrapper::type>> {}; +// +// template +// struct is_invocable_r : std::is_constructible, +// std::reference_wrapper::type>> {}; +// +// template +// using enable_if_t = typename std::enable_if::type; enum FutureState { kNotCompletedFuture = 0, @@ -83,27 +84,27 @@ 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); - } + // 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_; } @@ -147,8 +148,7 @@ public: return IsCompleted(); } - template::value, Dummy>::type> + template::value, Dummy>> T Result() const noexcept { SLED_ASSERT(data_ != nullptr, "Future is not valid"); @@ -183,7 +183,7 @@ public: return FailureT(); } - template::value>::type> + template::value>> Future OnSuccess(Func &&f) const noexcept { SLED_ASSERT(data_ != nullptr, "Future is not valid"); @@ -211,7 +211,7 @@ public: return Future(data_); } - template::value>> + template::value>> Future OnFailure(Func &&f) const noexcept { SLED_ASSERT(data_ != nullptr, "Future is not valid"); @@ -239,7 +239,7 @@ public: return Future(data_); } - template::value>> + template::value>> Future OnComplete(Func &&f) const noexcept { SLED_ASSERT(data_ != nullptr, "Future is not valid"); @@ -248,7 +248,7 @@ public: return Future(data_); } - template::value>> + template::value>> Future Filter(Func &&f, const FailureT &rejected = failure::FailureFromString("Result wasn't good enough")) const noexcept @@ -271,7 +271,7 @@ public: return result; } - template> + template> Future Map(Func &&f) const noexcept { Future result = Future::Create(); @@ -288,7 +288,7 @@ public: return result; } - template> + template> Future MapFailure(Func &&f) const noexcept { Future result = Future::Create(); @@ -305,7 +305,7 @@ public: return result; } - template>().Result())> + template>().Result())> Future FlatMap(Func &&f) const noexcept { Future result = Future::Create(); @@ -324,7 +324,7 @@ public: return result; } - template().Result()>)> + template().Result()>)> Future AndThen(Func &&f) const noexcept { return FlatMap([f](const T &) { return f(); }); @@ -353,7 +353,7 @@ public: return result; } - template::value>> + template::value>> static Future Async(Func &&f) noexcept { Future result = Future::Create(); diff --git a/src/sled/futures/future_test.cc b/src/sled/futures/future_test.cc index 0814648..710dd53 100644 --- a/src/sled/futures/future_test.cc +++ b/src/sled/futures/future_test.cc @@ -1,6 +1,5 @@ #include #include -#include TEST_SUITE("future") { @@ -122,8 +121,8 @@ TEST_SUITE("future") TEST_CASE("Constructor") { - sled::Future f1 = 1; - sled::Future f2 = std::string("1"); + auto f1 = sled::Future::Successful(1); + auto f2 = sled::Future::Failed(std::string("1")); REQUIRE(f1.IsCompleted()); REQUIRE(f2.IsFailed()); REQUIRE_EQ(f1.Result(), 1); @@ -132,15 +131,15 @@ TEST_SUITE("future") TEST_CASE("MapFailure") { - sled::Future f = false; - auto f1 = f.MapFailure([](bool) { return std::string("error"); }); + auto f = sled::Future::Failed(false); + auto f1 = f.MapFailure([](bool) { return std::string("error"); }); CHECK_EQ(f1.FailureReason(), "error"); } TEST_CASE("Chain") { - sled::Future f = 1; - auto f1 = f.Map([](int i) { return i + 1; }) + auto f = sled::Future::Successful(1); + auto f1 = f.Map([](int i) { return i + 1; }) .FlatMap([](int i) { sled::Promise p; p.Success(std::to_string(i)); diff --git a/src/sled/futures/internal/failure_handling.h b/src/sled/futures/internal/failure_handling.h index fa90b8d..fda0063 100644 --- a/src/sled/futures/internal/failure_handling.h +++ b/src/sled/futures/internal/failure_handling.h @@ -3,7 +3,7 @@ #pragma once #include "sled/any.h" -#include "sled/nonstd/string_view.h" +#include "sled/meta/type_traits.h" #include namespace sled { @@ -25,16 +25,14 @@ private: std::string message_; }; -template::value>::type> +template::value>> inline FailureT FailureFromString(std::string &&str) { return FailureT(); } -template::value>::type> +template::value>> inline FailureT FailureFromString(const std::string &str) { diff --git a/src/sled/meta/type_traits.h b/src/sled/meta/type_traits.h new file mode 100644 index 0000000..366e473 --- /dev/null +++ b/src/sled/meta/type_traits.h @@ -0,0 +1,34 @@ +#ifndef SLED_META_TYPE_TRAITS_H +#define SLED_META_TYPE_TRAITS_H + +#pragma once + +#include "sled/exec/detail/invoke_result.h" + +namespace sled { + +template +using InvokeResultT = typename eggs::invoke_result_t; + +template +using IsInvocable = eggs::is_invocable; + +template +using IsInvocableR = eggs::is_invocable_r; + +template +using IsNothrowInvocable = eggs::is_nothrow_invocable; + +template +using IsNothrowInvocableR = eggs::is_nothrow_invocable_r; + +// EnableIfT +template +using EnableIfT = typename std::enable_if::type; + +template +using DecayT = typename std::decay::type; + +}// namespace sled + +#endif// SLED_META_TYPE_TRAITS_H