From 43b5d469b3370d64656b446f7021976f89bf7e12 Mon Sep 17 00:00:00 2001 From: szdytom Date: Wed, 19 Jul 2023 21:11:19 +0800 Subject: [PATCH] [libvmake] refactor group types --- libvmake/vmake.hpp | 70 ++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/libvmake/vmake.hpp b/libvmake/vmake.hpp index bbb3fc6..46146b7 100644 --- a/libvmake/vmake.hpp +++ b/libvmake/vmake.hpp @@ -9,6 +9,8 @@ namespace vmake { +using std::size_t; + struct require_unique_t {} require_unique; struct sequence_terminated_error : std::exception { @@ -19,20 +21,20 @@ struct sequence_terminated_error : std::exception { namespace polyfill { -template -struct as_const_reference_t { - using type = const typename std::remove_reference::type&; -}; - -template -inline constexpr auto as_const_reference(typename as_const_reference_t::type x) noexcept { - return x; -} #if __cplusplus >= 201703L + using std::optional; +using std::as_const; + #else + using nonstd::optional; + +template struct add_const_t { using type = const T; }; +template constexpr typename add_const_t::type& as_const(T& t) noexcept { return t; } +template void as_const(const T&&) = delete; + #endif } // namespace polyfill @@ -201,7 +203,7 @@ namespace details { template struct generator { - using result = typename std::result_of::type; + using result = decltype(std::declval()()); Func g; @@ -332,7 +334,7 @@ namespace details { template struct transformer { - using result = typename std::result_of::type; + using result = decltype(std::declval()(std::declval())); using core = Gen; Gen g; @@ -397,7 +399,7 @@ private: return; } preview.emplace(std::move(g())); - } while (!p(polyfill::as_const_reference(*preview))); + } while (!p(polyfill::as_const(*preview))); } }; @@ -408,40 +410,30 @@ inline filteror::type, typename std::decay::type> namespace details { -template -using tuple_cat_t = decltype(std::tuple_cat(std::declval()...)); +//template +//using tuple_cat_t = decltype(std::tuple_cat(std::declval()...)); + +template +auto repeat_tuple_builder(std::index_sequence seq) + -> std::tuple::type...>; template -struct repeat_tuple { - static_assert(n > 0, ""); - using type = tuple_cat_t, typename repeat_tuple::type>; +struct repeat_tuple_t { + using type = decltype(repeat_tuple_builder(std::make_index_sequence())); }; -template -struct repeat_tuple { - using type = std::tuple; -}; - -template -struct group_helper { - static auto load(Gen &g) { - auto x = g(); - auto y = group_helper::load(g); - return std::tuple_cat(std::make_tuple(std::move(x)), std::move(y)); - } -}; - -template -struct group_helper { - static auto load(Gen &g) { - return std::make_tuple(g()); - } -}; +template +inline auto group_builder(Gen &g, std::index_sequence seq) { + auto val = std::array{ + (void(index), g())... + }; + return std::make_tuple(std::move(val[index])...); +} template struct grouper { static_assert(n > 0, ""); - using result = typename repeat_tuple::type; + using result = typename repeat_tuple_t::type; using core = Gen; Gen g; @@ -455,7 +447,7 @@ struct grouper { } result operator()() { - return group_helper::load(g); + return group_builder(g, std::make_index_sequence()); } };