From dfd2a5bee4edf59e62ca748ce30d1ada9ee730d7 Mon Sep 17 00:00:00 2001 From: Bronek Kozicki Date: Wed, 17 Jan 2024 14:22:47 +0000 Subject: [PATCH 1/4] Add FWD macro --- include/CMakeLists.txt | 1 + include/functional/and_then.hpp | 18 ++--- include/functional/detail/not_tuple.hpp | 13 +++ include/functional/detail/traits.hpp | 13 --- include/functional/fail.hpp | 34 ++++---- include/functional/functor.hpp | 8 +- include/functional/inspect.hpp | 81 +++++++++---------- include/functional/or_else.hpp | 10 +-- include/functional/recover.hpp | 36 ++++----- include/functional/transform.hpp | 18 ++--- include/functional/transform_error.hpp | 11 ++- include/functional/utility.hpp | 32 ++++++++ tests/examples/simple.cpp | 3 +- tests/tests/CMakeLists.txt | 2 +- tests/tests/functor.cpp | 8 +- .../tests/{detail/traits.cpp => utility.cpp} | 43 +++++++++- 16 files changed, 186 insertions(+), 145 deletions(-) create mode 100644 include/functional/utility.hpp rename tests/tests/{detail/traits.cpp => utility.cpp} (82%) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 3effdfeb..bba29557 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -19,6 +19,7 @@ set(INCLUDE_SOURCE_FILES functional/recover.hpp functional/transform_error.hpp functional/transform.hpp + functional/utility.hpp ) add_library(${PROJECT_NAME} INTERFACE ${INCLUDE_SOURCE_FILES}) diff --git a/include/functional/and_then.hpp b/include/functional/and_then.hpp index 8e54c4e8..fab2d1a3 100644 --- a/include/functional/and_then.hpp +++ b/include/functional/and_then.hpp @@ -9,6 +9,7 @@ #include "functional/detail/concepts.hpp" #include "functional/functor.hpp" #include "functional/fwd.hpp" +#include "functional/utility.hpp" #include #include @@ -18,7 +19,7 @@ namespace fn { constexpr inline struct and_then_t final { auto operator()(auto &&fn) const noexcept -> functor { - return {std::forward(fn)}; + return {FWD(fn)}; } struct apply; @@ -27,12 +28,10 @@ constexpr inline struct and_then_t final { struct and_then_t::apply final { static auto operator()(some_expected auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).value())> + requires std::invocable && (!std::is_void_v) { - return std::forward(v).and_then( - std::forward(fn)); + return FWD(v).and_then(FWD(fn)); } static auto operator()(some_expected auto &&v, auto &&fn) noexcept @@ -40,17 +39,14 @@ struct and_then_t::apply final { requires std::invocable && (std::is_void_v) { - return std::forward(v).and_then( - std::forward(fn)); + return FWD(v).and_then(FWD(fn)); } static auto operator()(some_optional auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).value())> + requires std::invocable { - return std::forward(v).and_then( - std::forward(fn)); + return FWD(v).and_then(FWD(fn)); } }; diff --git a/include/functional/detail/not_tuple.hpp b/include/functional/detail/not_tuple.hpp index fbd642dc..3feb7cf6 100644 --- a/include/functional/detail/not_tuple.hpp +++ b/include/functional/detail/not_tuple.hpp @@ -8,6 +8,8 @@ namespace fn::detail { +// TODO replace not_tuple and remove apply_const from this header + template struct not_tuple; template struct not_tuple final { @@ -35,6 +37,17 @@ constexpr bool _is_not_tuple const &> = true; template concept some_not_tuple = _is_not_tuple; +template +using apply_const_t = decltype(detail::_apply_const); + +// NOTE: Unlike apply_const_t above this is not exact: prvalue parameters are +// changed to xvalue. This is meant to disable copying of the return value. +template +constexpr auto apply_const(auto &&v) noexcept -> decltype(auto) +{ + return static_cast>(v); +} + template auto get(some_not_tuple auto &&v) noexcept -> decltype(auto) // requires(I == 0) && (std::decay_t::size >= 1) diff --git a/include/functional/detail/traits.hpp b/include/functional/detail/traits.hpp index 4cc4bc80..5909bee0 100644 --- a/include/functional/detail/traits.hpp +++ b/include/functional/detail/traits.hpp @@ -26,8 +26,6 @@ template requires(!std::is_empty_v) extern T const &_as_value; -template using as_value_t = decltype(_as_value); - // Add const to second type, if first type is const template extern V _apply_const; template extern V const _apply_const; @@ -35,17 +33,6 @@ template extern V const &_apply_const; template extern V const &&_apply_const; -template -using apply_const_t = decltype(_apply_const); - -// NOTE: Unlike apply_const_t above this is not exact: prvalue parameters are -// changed to xvalue. This is meant to disable copying of the return value. -template -constexpr auto apply_const(auto &&v) noexcept -> decltype(auto) -{ - return static_cast>(v); -} - } // namespace fn::detail #endif // INCLUDE_FUNCTIONAL_DETAIL_TRAITS diff --git a/include/functional/fail.hpp b/include/functional/fail.hpp index 93ef827d..f78aae7b 100644 --- a/include/functional/fail.hpp +++ b/include/functional/fail.hpp @@ -9,6 +9,7 @@ #include "functional/detail/concepts.hpp" #include "functional/functor.hpp" #include "functional/fwd.hpp" +#include "functional/utility.hpp" #include #include @@ -19,7 +20,7 @@ namespace fn { constexpr inline struct fail_t final { auto operator()(auto &&fn) const noexcept -> functor { - return {std::forward(fn)}; + return {FWD(fn)}; } struct apply; @@ -28,20 +29,16 @@ constexpr inline struct fail_t final { struct fail_t::apply final { static auto operator()(some_expected auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).value())> + requires std::invocable && (!std::is_void_v) { using type = std::remove_cvref_t; - static_assert( - std::is_convertible_v< - std::invoke_result_t< - decltype(fn), decltype(std::forward(v).value())>, - typename type::error_type>); - return std::forward(v).and_then( // + static_assert(std::is_convertible_v< + std::invoke_result_t, + typename type::error_type>); + return FWD(v).and_then( // [&fn](auto &&arg) noexcept -> type { - return std::unexpected{ - std::forward(fn)(std::forward(arg))}; + return std::unexpected{FWD(fn)(FWD(arg))}; }); } @@ -53,25 +50,22 @@ struct fail_t::apply final { using type = std::remove_cvref_t; static_assert(std::is_convertible_v, typename type::error_type>); - return std::forward(v).and_then( // + return FWD(v).and_then( // [&fn]() noexcept -> type { - return std::unexpected{ - std::forward(fn)()}; + return std::unexpected{FWD(fn)()}; }); } static auto operator()(some_optional auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).value())> + requires std::invocable { using type = std::remove_cvref_t; static_assert( - std::is_void_v(v).value())>>); + std::is_void_v< + std::invoke_result_t>); if (v.has_value()) { - std::forward(fn)( - std::forward(v).value()); // side-effects only + FWD(fn)(FWD(v).value()); // side-effects only } return type{std::nullopt}; } diff --git a/include/functional/functor.hpp b/include/functional/functor.hpp index 8a99bef1..c89a586a 100644 --- a/include/functional/functor.hpp +++ b/include/functional/functor.hpp @@ -8,6 +8,7 @@ #include "functional/detail/concepts.hpp" #include "functional/detail/not_tuple.hpp" +#include "functional/utility.hpp" #include #include @@ -32,7 +33,7 @@ concept monadic_invocable template struct functor final { using functor_type = Functor; constexpr static unsigned size = sizeof...(Args); - detail::not_tuple...> data; + detail::not_tuple...> data; static_assert(sizeof...(Args) > 0); // NOTE Consider relaxing static_assert(std::is_empty_v @@ -48,9 +49,8 @@ template struct functor final { return [&v, &self]( std::integer_sequence) noexcept -> decltype(auto) { - return Functor::apply::template operator()( // - std::forward(v), // - detail::get(std::forward(self).data)...); + return Functor::apply::template operator()( + FWD(v), detail::get(FWD(self).data)...); }(std::make_integer_sequence{}); } }; diff --git a/include/functional/inspect.hpp b/include/functional/inspect.hpp index 52a6bb58..2782433e 100644 --- a/include/functional/inspect.hpp +++ b/include/functional/inspect.hpp @@ -9,6 +9,7 @@ #include "functional/detail/concepts.hpp" #include "functional/functor.hpp" #include "functional/fwd.hpp" +#include "functional/utility.hpp" #include #include @@ -18,9 +19,9 @@ namespace fn { constexpr inline struct inspect_t final { auto operator()(auto &&...fn) const noexcept -> functor - requires(sizeof...(fn) > 0) + requires(sizeof...(fn) > 0) && (sizeof...(fn) <= 2) { - return {std::forward(fn)...}; + return {FWD(fn)...}; } struct apply; @@ -34,18 +35,16 @@ struct inspect_t::apply final { decltype(std::as_const(v.error()))> && (!std::is_void_v) { - static_assert( - std::is_void_v(v).value())>>); - static_assert( - std::is_void_v(v).error())>>); + static_assert(std::is_void_v>); + static_assert(std::is_void_v>); if (v.has_value()) { - std::forward(fn1)(std::as_const(v.value())); + FWD(fn1)(std::as_const(v.value())); } else { - std::forward(fn2)(std::as_const(v.error())); + FWD(fn2)(std::as_const(v.error())); } - return std::forward(v); + return FWD(v); } static auto operator()(some_expected auto &&v, auto &&fn1, @@ -56,15 +55,14 @@ struct inspect_t::apply final { && (std::is_void_v) { static_assert(std::is_void_v>); - static_assert( - std::is_void_v(v).error())>>); + static_assert(std::is_void_v>); if (v.has_value()) { - std::forward(fn1)(); + FWD(fn1)(); } else { - std::forward(fn2)(std::as_const(v.error())); + FWD(fn2)(std::as_const(v.error())); } - return std::forward(v); + return FWD(v); } static auto operator()(some_expected auto &&v, auto &&fn) noexcept @@ -73,18 +71,16 @@ struct inspect_t::apply final { && std::invocable && (!std::is_void_v) { - static_assert( - std::is_void_v(v).value())>>); - static_assert( - std::is_void_v(v).error())>>); + static_assert(std::is_void_v>); + static_assert(std::is_void_v>); if (v.has_value()) { - std::forward(fn)(std::as_const(v.value())); + FWD(fn)(std::as_const(v.value())); } else { - std::forward(fn)(std::as_const(v.error())); + FWD(fn)(std::as_const(v.error())); } - return std::forward(v); + return FWD(v); } static auto operator()(some_expected auto &&v, auto &&fn) noexcept @@ -94,15 +90,14 @@ struct inspect_t::apply final { && (std::is_void_v) { static_assert(std::is_void_v>); - static_assert( - std::is_void_v(v).error())>>); + static_assert(std::is_void_v>); if (v.has_value()) { - std::forward(fn)(); + FWD(fn)(); } else { - std::forward(fn)(std::as_const(v.error())); + FWD(fn)(std::as_const(v.error())); } - return std::forward(v); + return FWD(v); } static auto operator()(some_optional auto &&v, auto &&fn1, @@ -110,16 +105,15 @@ struct inspect_t::apply final { requires std::invocable && std::invocable { - static_assert( - std::is_void_v(v).value())>>); + static_assert(std::is_void_v>); static_assert(std::is_void_v>); if (v.has_value()) { - std::forward(fn1)(std::as_const(v.value())); + FWD(fn1)(std::as_const(v.value())); } else { - std::forward(fn2)(); + FWD(fn2)(); } - return std::forward(v); + return FWD(v); } static auto operator()(some_optional auto &&v, auto &&fn) noexcept @@ -127,16 +121,15 @@ struct inspect_t::apply final { requires std::invocable && std::invocable { - static_assert( - std::is_void_v(v).value())>>); + static_assert(std::is_void_v>); static_assert(std::is_void_v>); if (v.has_value()) { - std::forward(fn)(std::as_const(v.value())); + FWD(fn)(std::as_const(v.value())); } else { - std::forward(fn)(); + FWD(fn)(); } - return std::forward(v); + return FWD(v); } }; diff --git a/include/functional/or_else.hpp b/include/functional/or_else.hpp index 9d48e753..8354c7b3 100644 --- a/include/functional/or_else.hpp +++ b/include/functional/or_else.hpp @@ -9,6 +9,7 @@ #include "functional/detail/concepts.hpp" #include "functional/functor.hpp" #include "functional/fwd.hpp" +#include "functional/utility.hpp" #include #include @@ -18,7 +19,7 @@ namespace fn { constexpr inline struct or_else_t final { auto operator()(auto &&fn) const noexcept -> functor { - return {std::forward(fn)}; + return {FWD(fn)}; } struct apply; @@ -27,17 +28,16 @@ constexpr inline struct or_else_t final { struct or_else_t::apply final { static auto operator()(some_expected auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).error())> + requires std::invocable { - return std::forward(v).or_else(std::forward(fn)); + return FWD(v).or_else(FWD(fn)); } static auto operator()(some_optional auto &&v, auto &&fn) noexcept -> decltype(auto) requires std::invocable { - return std::forward(v).or_else(std::forward(fn)); + return FWD(v).or_else(FWD(fn)); } }; diff --git a/include/functional/recover.hpp b/include/functional/recover.hpp index 48fd6102..e56c6c8d 100644 --- a/include/functional/recover.hpp +++ b/include/functional/recover.hpp @@ -10,6 +10,7 @@ #include "functional/detail/traits.hpp" #include "functional/functor.hpp" #include "functional/fwd.hpp" +#include "functional/utility.hpp" #include #include @@ -20,7 +21,7 @@ namespace fn { constexpr inline struct recover_t final { auto operator()(auto &&fn) const noexcept -> functor { - return {std::forward(fn)}; + return {FWD(fn)}; } struct apply; @@ -29,35 +30,29 @@ constexpr inline struct recover_t final { struct recover_t::apply final { static auto operator()(some_expected auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).error())> + requires std::invocable && (!std::is_void_v) { using type = std::remove_cvref_t; - static_assert( - std::is_convertible_v< - std::invoke_result_t< - decltype(fn), decltype(std::forward(v).error())>, - typename type::value_type>); - return std::forward(v).or_else([&fn](auto &&arg) noexcept { - return type{std::in_place, std::forward(fn)( - std::forward(arg))}; + static_assert(std::is_convertible_v< + std::invoke_result_t, + typename type::value_type>); + return FWD(v).or_else([&fn](auto &&arg) noexcept -> decltype(auto) { + return type{std::in_place, FWD(fn)(FWD(arg))}; }); } static auto operator()(some_expected auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).error())> + requires std::invocable && (std::is_void_v) { using type = std::remove_cvref_t; static_assert( - std::is_void_v(v).error())>>); - return std::forward(v).or_else([&fn](auto &&arg) noexcept { - std::forward(fn)( - std::forward(arg)); // side-effects only + std::is_void_v< + std::invoke_result_t>); + return FWD(v).or_else([&fn](auto &&arg) noexcept -> decltype(auto) { + FWD(fn)(FWD(arg)); // side-effects only return type{std::in_place}; }); } @@ -69,9 +64,8 @@ struct recover_t::apply final { using type = std::remove_cvref_t; static_assert(std::is_convertible_v, typename type::value_type>); - - return std::forward(v).or_else([&fn]() noexcept { - return type{std::in_place, std::forward(fn)()}; + return FWD(v).or_else([&fn]() noexcept -> decltype(auto) { + return type{std::in_place, FWD(fn)()}; }); } }; diff --git a/include/functional/transform.hpp b/include/functional/transform.hpp index f4e1584a..32535a51 100644 --- a/include/functional/transform.hpp +++ b/include/functional/transform.hpp @@ -9,6 +9,7 @@ #include "functional/detail/concepts.hpp" #include "functional/functor.hpp" #include "functional/fwd.hpp" +#include "functional/utility.hpp" #include #include @@ -19,7 +20,7 @@ constexpr static struct transform_t final { auto operator()(auto &&fn) const noexcept -> functor { - return {std::forward(fn)}; + return {FWD(fn)}; } struct apply; @@ -28,12 +29,10 @@ constexpr static struct transform_t final { struct transform_t::apply final { static auto operator()(some_expected auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).value())> + requires std::invocable && (!std::is_void_v) { - return std::forward(v).transform( - std::forward(fn)); + return FWD(v).transform(FWD(fn)); } static auto operator()(some_expected auto &&v, auto &&fn) noexcept @@ -41,17 +40,14 @@ struct transform_t::apply final { requires std::invocable && (std::is_void_v) { - return std::forward(v).transform( - std::forward(fn)); + return FWD(v).transform(FWD(fn)); } static auto operator()(some_optional auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).value())> + requires std::invocable { - return std::forward(v).transform( - std::forward(fn)); + return FWD(v).transform(FWD(fn)); } }; diff --git a/include/functional/transform_error.hpp b/include/functional/transform_error.hpp index 53f7724f..1c46a626 100644 --- a/include/functional/transform_error.hpp +++ b/include/functional/transform_error.hpp @@ -9,6 +9,7 @@ #include "functional/detail/concepts.hpp" #include "functional/functor.hpp" #include "functional/fwd.hpp" +#include "functional/utility.hpp" #include #include @@ -19,7 +20,7 @@ constexpr inline struct transform_error_t final { auto operator()(auto &&fn) const noexcept -> functor { - return {std::forward(fn)}; + return {FWD(fn)}; } struct apply; @@ -28,14 +29,12 @@ constexpr inline struct transform_error_t final { struct transform_error_t::apply final { static auto operator()(some_expected auto &&v, auto &&fn) noexcept -> decltype(auto) - requires std::invocable(v).error())> + requires std::invocable { - return std::forward(v).transform_error( - std::forward(fn)); + return FWD(v).transform_error(FWD(fn)); } - // No support for optional, since there's no error state + // No support for optional since there's no error state to operate on static auto operator()(some_optional auto &&v, auto &&...args) noexcept = delete; }; diff --git a/include/functional/utility.hpp b/include/functional/utility.hpp new file mode 100644 index 00000000..67fbbdcb --- /dev/null +++ b/include/functional/utility.hpp @@ -0,0 +1,32 @@ +// Copyright (c) 2024 Bronek Kozicki +// +// Distributed under the ISC License. See accompanying file LICENSE.md +// or copy at https://opensource.org/licenses/ISC + +#ifndef INCLUDE_FUNCTIONAL_UTILITY +#define INCLUDE_FUNCTIONAL_UTILITY + +#include "functional/detail/traits.hpp" + +// This FWD macro is a functional equivalent to std::forward(v), +// but it saves compilation time (and typing) when used frequently. +#define FWD(...) static_cast(__VA_ARGS__) + +namespace fn { + +template using as_value_t = decltype(detail::_as_value); + +template +using apply_const_t = decltype(detail::_apply_const); + +// NOTE: Unlike apply_const_t above this is not exact: prvalue parameters are +// changed to xvalue. This is meant to disable copying of the return value. +template +constexpr auto apply_const(auto &&v) noexcept -> decltype(auto) +{ + return static_cast>(v); +} + +} // namespace fn + +#endif // INCLUDE_FUNCTIONAL_UTILITY diff --git a/tests/examples/simple.cpp b/tests/examples/simple.cpp index fbf07488..0f370559 100644 --- a/tests/examples/simple.cpp +++ b/tests/examples/simple.cpp @@ -11,6 +11,7 @@ #include "functional/recover.hpp" #include "functional/transform.hpp" #include "functional/transform_error.hpp" +#include "functional/utility.hpp" #include @@ -41,7 +42,7 @@ template struct ImmovableFn final { constexpr auto operator()(auto &&...args) const noexcept -> decltype(auto) requires requires { fn(args...); } { - return fn(std::forward(args)...); + return fn(FWD(args)...); } constexpr ImmovableFn(ImmovableFn const &) = delete; diff --git a/tests/tests/CMakeLists.txt b/tests/tests/CMakeLists.txt index 98130524..506c9596 100644 --- a/tests/tests/CMakeLists.txt +++ b/tests/tests/CMakeLists.txt @@ -8,7 +8,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Pls keep the filenames sorted set(TESTS_SOURCE_FILES detail/not_tuple.cpp - detail/traits.cpp and_then.cpp fail.cpp functor.cpp @@ -17,6 +16,7 @@ set(TESTS_SOURCE_FILES recover.cpp transform_error.cpp transform.cpp + utility.cpp ) add_executable(${PROJECT_NAME} ${TESTS_SOURCE_FILES}) target_link_libraries(${PROJECT_NAME} include catch2_main Catch2::Catch2) diff --git a/tests/tests/functor.cpp b/tests/tests/functor.cpp index bba28a12..35d01929 100644 --- a/tests/tests/functor.cpp +++ b/tests/tests/functor.cpp @@ -4,6 +4,7 @@ // or copy at https://opensource.org/licenses/ISC #include "functional/functor.hpp" +#include "functional/utility.hpp" #include @@ -12,7 +13,7 @@ constexpr inline struct dummy_t final { auto operator()(auto &&fn) const noexcept -> fn::functor { - return {std::forward(fn)}; + return {FWD(fn)}; } struct apply final { @@ -20,9 +21,8 @@ constexpr inline struct dummy_t final { -> decltype(auto) requires requires { fn(v.value()); } { - return std::forward(v).transform([&fn](auto &&v) noexcept { - return std::forward(fn)(std::forward(v)); - }); + return FWD(v).transform( + [&fn](auto &&v) noexcept { return FWD(fn)(FWD(v)); }); } }; } dummy = {}; diff --git a/tests/tests/detail/traits.cpp b/tests/tests/utility.cpp similarity index 82% rename from tests/tests/detail/traits.cpp rename to tests/tests/utility.cpp index cee30d31..ab419541 100644 --- a/tests/tests/detail/traits.cpp +++ b/tests/tests/utility.cpp @@ -3,11 +3,13 @@ // Distributed under the ISC License. See accompanying file LICENSE.md // or copy at https://opensource.org/licenses/ISC -#include "functional/detail/traits.hpp" +#include "functional/utility.hpp" #include -namespace fn::detail { +#include + +namespace fn { // clang-format off static_assert(std::is_same_v, int>); static_assert(std::is_same_v, int const>); @@ -63,12 +65,45 @@ static_assert(std::is_same_v(std::declval (std::declval())), int &&>); static_assert(std::is_same_v(std::declval())), int const &&>); // clang-format on -} // namespace fn::detail +} // namespace fn + +extern int value; +extern int const const_value; +static_assert( // + std::is_same_v< // + decltype(FWD(value)), // + decltype(std::forward(value))>); +static_assert( // + std::is_same_v< // + decltype(FWD(const_value)), + decltype(std::forward(const_value))>); + +extern int &lvalue; +extern int const &const_lvalue; +static_assert( // + std::is_same_v< // + decltype(FWD(lvalue)), + decltype(std::forward(lvalue))>); +static_assert( // + std::is_same_v< // + decltype(FWD(const_lvalue)), + decltype(std::forward(const_lvalue))>); + +extern int &&rvalue; +extern int const &&const_rvalue; +static_assert( // + std::is_same_v< // + decltype(FWD(rvalue)), + decltype(std::forward(rvalue))>); +static_assert( // + std::is_same_v< // + decltype(FWD(const_rvalue)), + decltype(std::forward(const_rvalue))>); TEST_CASE("detail::apply_const", "[apply_const]") { struct A final {}; - using namespace fn::detail; + using namespace fn; CHECK(std::is_same_v(A{})), A &&>); CHECK(std::is_same_v(A{})), A const &&>); From c3b3454925c88631d6bbb5a899d664cc430efe06 Mon Sep 17 00:00:00 2001 From: Bronek Kozicki Date: Wed, 17 Jan 2024 15:57:17 +0000 Subject: [PATCH 2/4] Rewrite not_tuple --- CMakeLists.txt | 2 +- include/functional/detail/not_tuple.hpp | 67 +++--------- include/functional/detail/traits.hpp | 3 + include/functional/functor.hpp | 20 +--- include/functional/utility.hpp | 21 +++- tests/tests/CMakeLists.txt | 1 - tests/tests/detail/not_tuple.cpp | 106 ------------------ tests/tests/functor.cpp | 29 ----- tests/tests/utility.cpp | 139 +++++++++++++++++++----- 9 files changed, 157 insertions(+), 231 deletions(-) delete mode 100644 tests/tests/detail/not_tuple.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4df2e6dd..af18080e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ add_compile_options( if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Workaround for std::expected not available in clang add_compile_options( - -stdlib=libstdc++ -D__cpp_concepts=202002 -Wno-builtin-macro-redefined + -stdlib=libstdc++ -D__cpp_concepts=202002 -Wno-builtin-macro-redefined -Wno-missing-braces ) endif() diff --git a/include/functional/detail/not_tuple.hpp b/include/functional/detail/not_tuple.hpp index 3feb7cf6..bc8698ef 100644 --- a/include/functional/detail/not_tuple.hpp +++ b/include/functional/detail/not_tuple.hpp @@ -8,60 +8,29 @@ namespace fn::detail { -// TODO replace not_tuple and remove apply_const from this header +template struct _element { + T v; -template struct not_tuple; - -template struct not_tuple final { - constexpr static unsigned size = 1; - Arg0 v0; - - static_assert(!std::is_rvalue_reference_v); + static_assert(not std::is_rvalue_reference_v); }; -template struct not_tuple final { - constexpr static unsigned size = 2; - Arg0 v0; - Arg1 v1; - - static_assert(!std::is_rvalue_reference_v - && !std::is_rvalue_reference_v); +template struct not_tuple_base; + +template +struct not_tuple_base, Ts...> : _element... { + template + static auto invoke(Self &&self, Fn &&fn, Args &&...args) noexcept + -> decltype(auto) + requires std::invocable< + decltype(fn), decltype(args)..., + decltype(apply_const_t(self._element::v))...> + { + return std::forward(fn)( + std::forward(args)..., + apply_const_t(self._element::v)...); + } }; -template constexpr bool _is_not_tuple = false; -template -constexpr bool _is_not_tuple &> = true; -template -constexpr bool _is_not_tuple const &> = true; - -template -concept some_not_tuple = _is_not_tuple; - -template -using apply_const_t = decltype(detail::_apply_const); - -// NOTE: Unlike apply_const_t above this is not exact: prvalue parameters are -// changed to xvalue. This is meant to disable copying of the return value. -template -constexpr auto apply_const(auto &&v) noexcept -> decltype(auto) -{ - return static_cast>(v); -} - -template -auto get(some_not_tuple auto &&v) noexcept -> decltype(auto) // - requires(I == 0) && (std::decay_t::size >= 1) -{ - return apply_const(std::forward(v).v0); -} - -template -auto get(some_not_tuple auto &&v) noexcept -> decltype(auto) // - requires(I == 1) && (std::decay_t::size >= 2) -{ - return apply_const(std::forward(v).v1); -} - } // namespace fn::detail #endif // INCLUDE_FUNCTIONAL_DETAIL_NOT_TUPLE diff --git a/include/functional/detail/traits.hpp b/include/functional/detail/traits.hpp index 5909bee0..376ba0f6 100644 --- a/include/functional/detail/traits.hpp +++ b/include/functional/detail/traits.hpp @@ -33,6 +33,9 @@ template extern V const &_apply_const; template extern V const &&_apply_const; +template +using apply_const_t = decltype(detail::_apply_const); + } // namespace fn::detail #endif // INCLUDE_FUNCTIONAL_DETAIL_TRAITS diff --git a/include/functional/functor.hpp b/include/functional/functor.hpp index c89a586a..deca579a 100644 --- a/include/functional/functor.hpp +++ b/include/functional/functor.hpp @@ -16,15 +16,6 @@ namespace fn { -template -concept some_expected = detail::_is_some_expected; - -template -concept some_optional = detail::_is_some_optional; - -template -concept some_monadic_type = some_expected || some_optional; - template concept monadic_invocable = some_monadic_type // @@ -33,7 +24,8 @@ concept monadic_invocable template struct functor final { using functor_type = Functor; constexpr static unsigned size = sizeof...(Args); - detail::not_tuple...> data; + using data_t = not_tuple...>; + data_t data; static_assert(sizeof...(Args) > 0); // NOTE Consider relaxing static_assert(std::is_empty_v @@ -46,12 +38,8 @@ template struct functor final { requires std::same_as, functor> && monadic_invocable { - return - [&v, &self]( - std::integer_sequence) noexcept -> decltype(auto) { - return Functor::apply::template operator()( - FWD(v), detail::get(FWD(self).data)...); - }(std::make_integer_sequence{}); + constexpr static typename functor_type::apply fn{}; + return data_t::invoke(FWD(self).data, FWD(fn), FWD(v)); } }; diff --git a/include/functional/utility.hpp b/include/functional/utility.hpp index 67fbbdcb..220352b0 100644 --- a/include/functional/utility.hpp +++ b/include/functional/utility.hpp @@ -6,6 +6,8 @@ #ifndef INCLUDE_FUNCTIONAL_UTILITY #define INCLUDE_FUNCTIONAL_UTILITY +#include "functional/detail/concepts.hpp" +#include "functional/detail/not_tuple.hpp" #include "functional/detail/traits.hpp" // This FWD macro is a functional equivalent to std::forward(v), @@ -14,19 +16,32 @@ namespace fn { +template +concept some_expected = detail::_is_some_expected; + +template +concept some_optional = detail::_is_some_optional; + +template +concept some_monadic_type = some_expected || some_optional; + template using as_value_t = decltype(detail::_as_value); template -using apply_const_t = decltype(detail::_apply_const); +using apply_const_t = detail::apply_const_t; -// NOTE: Unlike apply_const_t above this is not exact: prvalue parameters are -// changed to xvalue. This is meant to disable copying of the return value. +// NOTE: Unlike apply_const_t above, this is not exact: prvalue parameters are +// returned as xvalue. This is meant to disable copying of the return value. template constexpr auto apply_const(auto &&v) noexcept -> decltype(auto) { return static_cast>(v); } +template +struct not_tuple + : detail::not_tuple_base, Ts...> {}; + } // namespace fn #endif // INCLUDE_FUNCTIONAL_UTILITY diff --git a/tests/tests/CMakeLists.txt b/tests/tests/CMakeLists.txt index 506c9596..27c6dd82 100644 --- a/tests/tests/CMakeLists.txt +++ b/tests/tests/CMakeLists.txt @@ -7,7 +7,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Pls keep the filenames sorted set(TESTS_SOURCE_FILES - detail/not_tuple.cpp and_then.cpp fail.cpp functor.cpp diff --git a/tests/tests/detail/not_tuple.cpp b/tests/tests/detail/not_tuple.cpp deleted file mode 100644 index b072b47f..00000000 --- a/tests/tests/detail/not_tuple.cpp +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright (c) 2024 Bronek Kozicki -// -// Distributed under the ISC License. See accompanying file LICENSE.md -// or copy at https://opensource.org/licenses/ISC - -#include "functional/detail/not_tuple.hpp" - -#include - -namespace fn::detail { - -struct Foo final {}; -static_assert([](auto &&v) constexpr -> bool { - return requires { get<0>(v); } // - && !requires { get<1>(v); }; -}(not_tuple{})); - -static_assert([](auto &&v) constexpr -> bool { - return requires { get<0>(v); } // - && requires { get<1>(v); } // - && !requires { get<2>(v); }; - ; -}(not_tuple{})); - -// clang-format off -struct A { int v; }; -static_assert(std::is_same_v(std::declval>())), A &&>); -static_assert(std::is_same_v(std::declval const>())), A const &&>); -static_assert(std::is_same_v(std::declval &>())), A &>); -static_assert(std::is_same_v(std::declval const &>())), A const &>); -static_assert(std::is_same_v(std::declval &&>())), A &&>); -static_assert(std::is_same_v(std::declval const &&>())), A const &&>); - -static_assert(std::is_same_v(std::declval>())), A &>); -static_assert(std::is_same_v(std::declval const>())), A const &>); -static_assert(std::is_same_v(std::declval &>())), A &>); -static_assert(std::is_same_v(std::declval const &>())), A const &>); -static_assert(std::is_same_v(std::declval &&>())), A &>); -static_assert(std::is_same_v(std::declval const &&>())), A const &>); - -static_assert(std::is_same_v(std::declval>())), A const &>); -static_assert(std::is_same_v(std::declval const>())), A const &>); -static_assert(std::is_same_v(std::declval &>())), A const &>); -static_assert(std::is_same_v(std::declval const &>())), A const &>); -static_assert(std::is_same_v(std::declval &&>())), A const &>); -static_assert(std::is_same_v(std::declval const &&>())), A const &>); - -struct B { int v; }; -static_assert(std::is_same_v(std::declval const>())), B const &&>); -static_assert(std::is_same_v(std::declval const &&>())), B const &>); -static_assert(std::is_same_v(std::declval const &>())), B const &>); -static_assert(std::is_same_v(std::declval &>())), B const &>); -// clang-format on -} // namespace fn::detail - -TEST_CASE("detail::not_tuple", "[not_tuple]") -{ - using namespace fn::detail; - WHEN("holding values") - { - using T = not_tuple; - T t1 = {{1}, {2}}; - CHECK(get<0>(t1).v == 1); - CHECK(get<1>(t1).v == 2); - - static_assert(std::is_same_v(t1)), A &>); - static_assert(std::is_same_v(t1)), B const &>); - static_assert( - std::is_same_v(std::as_const(t1))), A const &>); - static_assert( - std::is_same_v(std::as_const(t1))), B const &>); - static_assert(std::is_same_v(std::move(t1))), A &&>); - static_assert(std::is_same_v(std::move(t1))), B const &&>); - static_assert(std::is_same_v< // - decltype(get<0>(std::move(std::as_const(t1)))), A const &&>); - static_assert(std::is_same_v< // - decltype(get<1>(std::move(std::as_const(t1)))), B const &&>); - static_assert(std::is_same_v(T{{}, {}})), A &&>); - static_assert(std::is_same_v(T{{}, {}})), B const &&>); - } - - WHEN("holding references") - { - A a{3}; - B const b{4}; - using T = not_tuple; - T t1 = {a, b}; - CHECK(get<0>(t1).v == 3); - CHECK(get<1>(t1).v == 4); - - static_assert(std::is_same_v(t1)), A &>); - static_assert(std::is_same_v(t1)), B const &>); - static_assert( - std::is_same_v(std::as_const(t1))), A const &>); - static_assert( - std::is_same_v(std::as_const(t1))), B const &>); - static_assert(std::is_same_v(std::move(t1))), A &>); - static_assert(std::is_same_v(std::move(t1))), B const &>); - static_assert(std::is_same_v< // - decltype(get<0>(std::move(std::as_const(t1)))), A const &>); - static_assert(std::is_same_v< // - decltype(get<1>(std::move(std::as_const(t1)))), B const &>); - static_assert(std::is_same_v(T{{a}, {b}})), A &>); - static_assert(std::is_same_v(T{{a}, {b}})), B const &>); - } -} diff --git a/tests/tests/functor.cpp b/tests/tests/functor.cpp index 35d01929..a1b674fd 100644 --- a/tests/tests/functor.cpp +++ b/tests/tests/functor.cpp @@ -63,32 +63,3 @@ TEST_CASE("user-defined monadic operation", "[functor]") == 13); CHECK((std::optional{42} | dummy(fn1)).value() == 43); } - -namespace fn { -static_assert(some_expected>); -static_assert(some_expected const>); -static_assert(some_expected &>); -static_assert(some_expected const &>); -static_assert(some_expected &&>); -static_assert(some_expected const &&>); - -static_assert(some_optional>); -static_assert(some_optional const>); -static_assert(some_optional &>); -static_assert(some_optional const &>); -static_assert(some_optional &&>); -static_assert(some_optional const &&>); - -static_assert(some_monadic_type>); -static_assert(some_monadic_type const>); -static_assert(some_monadic_type &>); -static_assert(some_monadic_type const &>); -static_assert(some_monadic_type &&>); -static_assert(some_monadic_type const &&>); -static_assert(some_monadic_type>); -static_assert(some_monadic_type const>); -static_assert(some_monadic_type &>); -static_assert(some_monadic_type const &>); -static_assert(some_monadic_type &&>); -static_assert(some_monadic_type const &&>); -} // namespace fn diff --git a/tests/tests/utility.cpp b/tests/tests/utility.cpp index ab419541..c74b5e9a 100644 --- a/tests/tests/utility.cpp +++ b/tests/tests/utility.cpp @@ -7,6 +7,7 @@ #include +#include #include namespace fn { @@ -65,6 +66,33 @@ static_assert(std::is_same_v(std::declval (std::declval())), int &&>); static_assert(std::is_same_v(std::declval())), int const &&>); // clang-format on + +static_assert(some_expected>); +static_assert(some_expected const>); +static_assert(some_expected &>); +static_assert(some_expected const &>); +static_assert(some_expected &&>); +static_assert(some_expected const &&>); + +static_assert(some_optional>); +static_assert(some_optional const>); +static_assert(some_optional &>); +static_assert(some_optional const &>); +static_assert(some_optional &&>); +static_assert(some_optional const &&>); + +static_assert(some_monadic_type>); +static_assert(some_monadic_type const>); +static_assert(some_monadic_type &>); +static_assert(some_monadic_type const &>); +static_assert(some_monadic_type &&>); +static_assert(some_monadic_type const &&>); +static_assert(some_monadic_type>); +static_assert(some_monadic_type const>); +static_assert(some_monadic_type &>); +static_assert(some_monadic_type const &>); +static_assert(some_monadic_type &&>); +static_assert(some_monadic_type const &&>); } // namespace fn extern int value; @@ -100,31 +128,90 @@ static_assert( // decltype(FWD(const_rvalue)), decltype(std::forward(const_rvalue))>); -TEST_CASE("detail::apply_const", "[apply_const]") +TEST_CASE("not_tuple", "[not_tuple]") { - struct A final {}; - using namespace fn; - CHECK(std::is_same_v(A{})), A &&>); - CHECK(std::is_same_v(A{})), A const &&>); - - A a = {}; - // NOTE std::move below do *not* leave us with moved-from objects - CHECK(std::is_same_v< // - decltype(apply_const(std::move(a))), A &&>); - CHECK(std::is_same_v< // - decltype(apply_const(std::move(a))), A const &&>); - CHECK(std::is_same_v< // - decltype(apply_const(a)), A &>); - CHECK(std::is_same_v< // - decltype(apply_const(a)), A const &>); - - CHECK(std::is_same_v< // - decltype(apply_const(std::move(std::as_const(a)))), A const &&>); - CHECK(std::is_same_v< // - decltype(apply_const(std::move(std::as_const(a)))), - A const &&>); - CHECK(std::is_same_v< // - decltype(apply_const(std::as_const(a))), A const &>); - CHECK(std::is_same_v< // - decltype(apply_const(std::as_const(a))), A const &>); + using fn::not_tuple; + + struct A final { + int v = 0; + }; + using T = not_tuple; + int val1 = 15; + int const val2 = 92; + T v{3, 14, val1, val2}; + + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](auto &&...) {})); // generic call + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A, auto &&...) {})); // also generic call + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A, int, int, int, int) {})); // pass everything by value + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A const &, int const &, int const &, int const &, int const &) { + })); // pass everything by const reference + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A, int, int, int &, int const &) {})); // bind lvalues + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A &&, int &&, int &&, int &, int const &) { + })); // bind rvalues and lvalues + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A const, int const, int const, int const &, int const &) { + })); // pass values or lvalues promoted to const + + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A &, auto &&...) {})); // cannot bind rvalue to lvalue reference + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A, int &, auto &&...) {})); // cannot bind rvalue to lvalue reference + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A, int, int, int &&, int) {})); // cannot bind lvalue to rvalue reference + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A, int, int, int, int &&) {})); // cannot bind lvalue to rvalue reference + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A, int, int, int, int const &&) { + })); // cannot bind lvalue to const rvalue reference + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](int, auto &&...) {})); // bad type + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](auto, auto, auto, auto) {})); // bad arity + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](auto, auto, auto, auto, auto, auto) {})); // bad arity + + CHECK(T::invoke(v, + [](auto... args) noexcept -> int { return (0 + ... + args); }) + == 3 + 14 + 15 + 92); + CHECK(T::invoke( + v, [](A, auto... args) noexcept -> int { return (0 + ... + args); }, + A{}) + == 3 + 14 + 15 + 92); + + A a; + constexpr auto fn1 = [](A &dest, auto... args) noexcept -> A & { + dest.v = (0 + ... + args); + return dest; + }; + CHECK(T::invoke(v, fn1, a).v == 3 + 14 + 15 + 92); + static_assert(std::is_same_v); + + constexpr auto fn2 + = [](A &&dest, auto &&...) noexcept -> A && { return std::move(dest); }; + static_assert( + std::is_same_v); + + constexpr auto fn3 = [](A &&dest, auto &&...) noexcept -> A { return dest; }; + static_assert(std::is_same_v); } From 0a435aa1925849833c3fdcdd8e1c0ba3c6e223a1 Mon Sep 17 00:00:00 2001 From: Bronek Kozicki Date: Wed, 17 Jan 2024 18:25:25 +0000 Subject: [PATCH 3/4] Drive-by unit test cleanup --- tests/tests/fail.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/tests/fail.cpp b/tests/tests/fail.cpp index 9c7e7b5a..c55c3091 100644 --- a/tests/tests/fail.cpp +++ b/tests/tests/fail.cpp @@ -204,10 +204,8 @@ TEST_CASE("fail", "[fail][optional]") using operand_t = std::optional; auto count = 0; auto fnValue = [&count](auto) { count += 1; }; - constexpr auto fnError = [](auto) { throw 0; }; static_assert(monadic_invocable); - static_assert(monadic_invocable); static_assert([](auto &&fn) constexpr -> bool { return monadic_invocable; }([](auto...) {})); // allow generic call @@ -243,6 +241,8 @@ TEST_CASE("fail", "[fail][optional]") return monadic_invocable; }([](int, int) {})); // wrong arity + constexpr auto wrong = [](auto...) { throw 0; }; + WHEN("operand is lvalue") { WHEN("operand is value") @@ -256,9 +256,9 @@ TEST_CASE("fail", "[fail][optional]") WHEN("operand is error") { operand_t a{std::nullopt}; - using T = decltype(a | fail(fnError)); + using T = decltype(a | fail(wrong)); static_assert(std::is_same_v); - REQUIRE(not(a | fail(fnError)).has_value()); + REQUIRE(not(a | fail(wrong)).has_value()); CHECK(count == 0); } } @@ -274,10 +274,10 @@ TEST_CASE("fail", "[fail][optional]") } WHEN("operand is error") { - using T = decltype(operand_t{std::nullopt} | fail(fnError)); + using T = decltype(operand_t{std::nullopt} | fail(wrong)); static_assert(std::is_same_v); REQUIRE(not(operand_t{std::nullopt} // - | fail(fnError)) + | fail(wrong)) .has_value()); CHECK(count == 0); } From 2dcff6f8bbc509fd4effa28f59c27595357c06aa Mon Sep 17 00:00:00 2001 From: Bronek Kozicki Date: Thu, 18 Jan 2024 12:28:13 +0000 Subject: [PATCH 4/4] Move FWD to detail, rename not_tuple to closure --- include/CMakeLists.txt | 3 +- include/functional/detail/closure.hpp | 38 ++++++++++ include/functional/detail/fwd_macro.hpp | 13 ++++ include/functional/detail/not_tuple.hpp | 36 --------- include/functional/functor.hpp | 15 ++-- include/functional/utility.hpp | 10 +-- tests/tests/CMakeLists.txt | 1 + tests/tests/detail/fwd_macro.cpp | 42 +++++++++++ tests/tests/utility.cpp | 99 +++++++++++++++---------- 9 files changed, 166 insertions(+), 91 deletions(-) create mode 100644 include/functional/detail/closure.hpp create mode 100644 include/functional/detail/fwd_macro.hpp delete mode 100644 include/functional/detail/not_tuple.hpp create mode 100644 tests/tests/detail/fwd_macro.cpp diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index bba29557..0fa80d2b 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -7,8 +7,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Pls keep the filenames sorted set(INCLUDE_SOURCE_FILES + functional/detail/closure.hpp functional/detail/concepts.hpp - functional/detail/not_tuple.hpp + functional/detail/fwd_macro.hpp functional/detail/traits.hpp functional/and_then.hpp functional/fail.hpp diff --git a/include/functional/detail/closure.hpp b/include/functional/detail/closure.hpp new file mode 100644 index 00000000..7b9ec5d8 --- /dev/null +++ b/include/functional/detail/closure.hpp @@ -0,0 +1,38 @@ +// Copyright (c) 2024 Bronek Kozicki +// +// Distributed under the ISC License. See accompanying file LICENSE.md +// or copy at https://opensource.org/licenses/ISC + +#ifndef INCLUDE_FUNCTIONAL_DETAIL_CLOSURE +#define INCLUDE_FUNCTIONAL_DETAIL_CLOSURE + +#include "fwd_macro.hpp" +#include "traits.hpp" + +#include +#include + +namespace fn::detail { + +template struct _element { + static_assert(not std::is_rvalue_reference_v); + T v; +}; + +template struct closure_base; + +template +struct closure_base, Ts...> : _element... { + template + static auto invoke(Self &&self, Fn &&fn, Args &&...args) noexcept + -> decltype(FWD(fn)(FWD(args)..., apply_const_t( + self._element::v)...)) + { + return FWD(fn)(FWD(args)..., + apply_const_t(self._element::v)...); + } +}; + +} // namespace fn::detail + +#endif // INCLUDE_FUNCTIONAL_DETAIL_CLOSURE diff --git a/include/functional/detail/fwd_macro.hpp b/include/functional/detail/fwd_macro.hpp new file mode 100644 index 00000000..8383dacd --- /dev/null +++ b/include/functional/detail/fwd_macro.hpp @@ -0,0 +1,13 @@ +// Copyright (c) 2024 Bronek Kozicki +// +// Distributed under the ISC License. See accompanying file LICENSE.md +// or copy at https://opensource.org/licenses/ISC + +#ifndef INCLUDE_FUNCTIONAL_DETAIL_FWD_MACRO +#define INCLUDE_FUNCTIONAL_DETAIL_FWD_MACRO + +// This FWD macro is a functional equivalent to std::forward(v), +// but it saves compilation time (and typing) when used frequently. +#define FWD(...) static_cast(__VA_ARGS__) + +#endif // INCLUDE_FUNCTIONAL_DETAIL_FWD_MACRO diff --git a/include/functional/detail/not_tuple.hpp b/include/functional/detail/not_tuple.hpp deleted file mode 100644 index bc8698ef..00000000 --- a/include/functional/detail/not_tuple.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef INCLUDE_FUNCTIONAL_DETAIL_NOT_TUPLE -#define INCLUDE_FUNCTIONAL_DETAIL_NOT_TUPLE - -#include "traits.hpp" - -#include -#include - -namespace fn::detail { - -template struct _element { - T v; - - static_assert(not std::is_rvalue_reference_v); -}; - -template struct not_tuple_base; - -template -struct not_tuple_base, Ts...> : _element... { - template - static auto invoke(Self &&self, Fn &&fn, Args &&...args) noexcept - -> decltype(auto) - requires std::invocable< - decltype(fn), decltype(args)..., - decltype(apply_const_t(self._element::v))...> - { - return std::forward(fn)( - std::forward(args)..., - apply_const_t(self._element::v)...); - } -}; - -} // namespace fn::detail - -#endif // INCLUDE_FUNCTIONAL_DETAIL_NOT_TUPLE diff --git a/include/functional/functor.hpp b/include/functional/functor.hpp index deca579a..9ff7bbbd 100644 --- a/include/functional/functor.hpp +++ b/include/functional/functor.hpp @@ -6,8 +6,8 @@ #ifndef INCLUDE_FUNCTIONAL_FUNCTOR #define INCLUDE_FUNCTIONAL_FUNCTOR +#include "functional/detail/closure.hpp" #include "functional/detail/concepts.hpp" -#include "functional/detail/not_tuple.hpp" #include "functional/utility.hpp" #include @@ -23,23 +23,22 @@ concept monadic_invocable template struct functor final { using functor_type = Functor; + using functor_apply = typename functor_type::apply; constexpr static unsigned size = sizeof...(Args); - using data_t = not_tuple...>; + using data_t = closure...>; data_t data; static_assert(sizeof...(Args) > 0); // NOTE Consider relaxing - static_assert(std::is_empty_v - && std::is_empty_v - && std::is_default_constructible_v - && std::is_default_constructible_v); + static_assert(std::is_empty_v && std::is_empty_v + && std::is_default_constructible_v + && std::is_default_constructible_v); friend auto operator|(some_monadic_type auto &&v, auto &&self) noexcept -> decltype(auto) requires std::same_as, functor> && monadic_invocable { - constexpr static typename functor_type::apply fn{}; - return data_t::invoke(FWD(self).data, FWD(fn), FWD(v)); + return data_t::invoke(FWD(self).data, functor_apply{}, FWD(v)); } }; diff --git a/include/functional/utility.hpp b/include/functional/utility.hpp index 220352b0..404544f8 100644 --- a/include/functional/utility.hpp +++ b/include/functional/utility.hpp @@ -6,14 +6,11 @@ #ifndef INCLUDE_FUNCTIONAL_UTILITY #define INCLUDE_FUNCTIONAL_UTILITY +#include "functional/detail/closure.hpp" #include "functional/detail/concepts.hpp" -#include "functional/detail/not_tuple.hpp" +#include "functional/detail/fwd_macro.hpp" #include "functional/detail/traits.hpp" -// This FWD macro is a functional equivalent to std::forward(v), -// but it saves compilation time (and typing) when used frequently. -#define FWD(...) static_cast(__VA_ARGS__) - namespace fn { template @@ -39,8 +36,7 @@ constexpr auto apply_const(auto &&v) noexcept -> decltype(auto) } template -struct not_tuple - : detail::not_tuple_base, Ts...> {}; +struct closure : detail::closure_base, Ts...> {}; } // namespace fn diff --git a/tests/tests/CMakeLists.txt b/tests/tests/CMakeLists.txt index 27c6dd82..ff568c41 100644 --- a/tests/tests/CMakeLists.txt +++ b/tests/tests/CMakeLists.txt @@ -7,6 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Pls keep the filenames sorted set(TESTS_SOURCE_FILES + detail/fwd_macro.cpp and_then.cpp fail.cpp functor.cpp diff --git a/tests/tests/detail/fwd_macro.cpp b/tests/tests/detail/fwd_macro.cpp new file mode 100644 index 00000000..e8076f54 --- /dev/null +++ b/tests/tests/detail/fwd_macro.cpp @@ -0,0 +1,42 @@ +// Copyright (c) 2024 Bronek Kozicki +// +// Distributed under the ISC License. See accompanying file LICENSE.md +// or copy at https://opensource.org/licenses/ISC + +#include "functional/detail/fwd_macro.hpp" + +#include +#include + +extern int value; +extern int const const_value; +static_assert( // + std::is_same_v< // + decltype(FWD(value)), // + decltype(std::forward(value))>); +static_assert( // + std::is_same_v< // + decltype(FWD(const_value)), + decltype(std::forward(const_value))>); + +extern int &lvalue; +extern int const &const_lvalue; +static_assert( // + std::is_same_v< // + decltype(FWD(lvalue)), + decltype(std::forward(lvalue))>); +static_assert( // + std::is_same_v< // + decltype(FWD(const_lvalue)), + decltype(std::forward(const_lvalue))>); + +extern int &&rvalue; +extern int const &&const_rvalue; +static_assert( // + std::is_same_v< // + decltype(FWD(rvalue)), + decltype(std::forward(rvalue))>); +static_assert( // + std::is_same_v< // + decltype(FWD(const_rvalue)), + decltype(std::forward(const_rvalue))>); diff --git a/tests/tests/utility.cpp b/tests/tests/utility.cpp index c74b5e9a..6d591e14 100644 --- a/tests/tests/utility.cpp +++ b/tests/tests/utility.cpp @@ -95,47 +95,14 @@ static_assert(some_monadic_type &&>); static_assert(some_monadic_type const &&>); } // namespace fn -extern int value; -extern int const const_value; -static_assert( // - std::is_same_v< // - decltype(FWD(value)), // - decltype(std::forward(value))>); -static_assert( // - std::is_same_v< // - decltype(FWD(const_value)), - decltype(std::forward(const_value))>); - -extern int &lvalue; -extern int const &const_lvalue; -static_assert( // - std::is_same_v< // - decltype(FWD(lvalue)), - decltype(std::forward(lvalue))>); -static_assert( // - std::is_same_v< // - decltype(FWD(const_lvalue)), - decltype(std::forward(const_lvalue))>); - -extern int &&rvalue; -extern int const &&const_rvalue; -static_assert( // - std::is_same_v< // - decltype(FWD(rvalue)), - decltype(std::forward(rvalue))>); -static_assert( // - std::is_same_v< // - decltype(FWD(const_rvalue)), - decltype(std::forward(const_rvalue))>); - -TEST_CASE("not_tuple", "[not_tuple]") +TEST_CASE("closure", "[closure]") { - using fn::not_tuple; + using fn::closure; struct A final { int v = 0; }; - using T = not_tuple; + using T = closure; int val1 = 15; int const val2 = 92; T v{3, 14, val1, val2}; @@ -158,7 +125,7 @@ TEST_CASE("not_tuple", "[not_tuple]") }([](A, int, int, int &, int const &) {})); // bind lvalues static_assert([&](auto &&fn) constexpr { return requires { T::invoke(v, fn, A{}); }; - }([](A &&, int &&, int &&, int &, int const &) { + }([](A &&, int &&, int const &&, int &, int const &) { })); // bind rvalues and lvalues static_assert([&](auto &&fn) constexpr { return requires { T::invoke(v, fn, A{}); }; @@ -176,11 +143,16 @@ TEST_CASE("not_tuple", "[not_tuple]") }([](A, int, int, int &&, int) {})); // cannot bind lvalue to rvalue reference static_assert(not [&](auto &&fn) constexpr { return requires { T::invoke(v, fn, A{}); }; - }([](A, int, int, int, int &&) {})); // cannot bind lvalue to rvalue reference + }([](A, int, int, int, int &&) { + })); // cannot bind const lvalue to rvalue reference static_assert(not [&](auto &&fn) constexpr { return requires { T::invoke(v, fn, A{}); }; }([](A, int, int, int, int const &&) { - })); // cannot bind lvalue to const rvalue reference + })); // cannot bind const lvalue to const rvalue reference + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn, A{}); }; + }([](A &&, int, int &&, int, int) { + })); // cannot bind const rvalue to non-const rvalue reference static_assert(not [&](auto &&fn) constexpr { return requires { T::invoke(v, fn, A{}); }; }([](int, auto &&...) {})); // bad type @@ -215,3 +187,52 @@ TEST_CASE("not_tuple", "[not_tuple]") constexpr auto fn3 = [](A &&dest, auto &&...) noexcept -> A { return dest; }; static_assert(std::is_same_v); } + +TEST_CASE("closure with immovable data", "[closure][immovable]") +{ + using fn::closure; + + struct ImmovableType { + int value = 0; + + constexpr explicit ImmovableType(int i) noexcept : value(i) {} + + ImmovableType(ImmovableType const &) = delete; + ImmovableType &operator=(ImmovableType const &) = delete; + ImmovableType(ImmovableType &&) = delete; + ImmovableType &operator=(ImmovableType &&) = delete; + + constexpr bool operator==(ImmovableType const &other) const noexcept + { + return value == other.value; + } + }; + + using T = closure; + ImmovableType val1{15}; + ImmovableType const val2{92}; + T v{ImmovableType{3}, ImmovableType{14}, val1, val2}; + + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn); }; + }([](auto &&...) {})); // generic call + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn); }; + }([](ImmovableType const &, ImmovableType const &, ImmovableType const &, + ImmovableType const &) {})); // pass everything by const reference + static_assert([&](auto &&fn) constexpr { + return requires { T::invoke(v, fn); }; + }([](ImmovableType &&, ImmovableType const &&, ImmovableType &, + ImmovableType const &) {})); // bind rvalues and lvalues + + static_assert(not [&](auto &&fn) constexpr { + return requires { T::invoke(v, fn); }; + }([](ImmovableType, auto &&...) {})); // cannot pass immovable by value + + CHECK(T::invoke(v, + [](auto &&...args) noexcept -> int { + return (0 + ... + args.value); + }) + == 3 + 14 + 15 + 92); +}