-
Notifications
You must be signed in to change notification settings - Fork 567
feat(utility): add bind_front for enum_switch partial application (#200) #471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4fe52dd
9f3c760
683b337
11f83d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
|
|
||
| #ifndef MAGIC_ENUM_USE_STD_MODULE | ||
| # include <tuple> | ||
| # include <utility> | ||
| #endif | ||
|
|
||
| namespace magic_enum { | ||
|
|
@@ -144,6 +145,32 @@ template <typename E, detail::enum_subtype S = detail::subtype_v<E>> | |
| return MAGIC_ENUM_ASSERT(false), value; | ||
| } | ||
|
|
||
| template <typename F, typename... BoundArgs> | ||
| class bind_front_t { | ||
| public: | ||
| template <typename Fn, typename... Args> | ||
| constexpr explicit bind_front_t(Fn&& fn, Args&&... args) | ||
| : fn_(std::forward<Fn>(fn)), bound_(std::forward<Args>(args)...) {} | ||
|
|
||
| template <typename... CallArgs> | ||
| constexpr decltype(auto) operator()(CallArgs&&... call_args) const { | ||
| return std::apply( | ||
| fn_, | ||
| std::tuple_cat(bound_, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid |
||
| std::forward_as_tuple(std::forward<CallArgs>(call_args)...))); | ||
| } | ||
|
|
||
| private: | ||
| F fn_; | ||
| std::tuple<BoundArgs...> bound_; | ||
| }; | ||
|
|
||
| template <typename F, typename... Args> | ||
| [[nodiscard]] constexpr auto bind_front(F&& f, Args&&... args) { | ||
| return bind_front_t<std::decay_t<F>, std::decay_t<Args>...>{ | ||
| std::forward<F>(f), std::forward<Args>(args)...}; | ||
| } | ||
|
|
||
| } // namespace magic_enum | ||
|
|
||
| #endif // NEARGYE_MAGIC_ENUM_UTILITY_HPP | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1315,6 +1315,19 @@ TEST_CASE("enum_for_each") { | |
| } | ||
| } | ||
|
|
||
| TEST_CASE("bind_front") { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test does not verify fix for issue #200. |
||
| auto add = [](int a, int b, int c) { return a + b + c; }; | ||
| auto add_ab = magic_enum::bind_front(add, 1, 2); | ||
| REQUIRE(add_ab(3) == 6); | ||
|
|
||
| enum class Shape { Circle, Square }; | ||
| auto describe = [](Shape s, std::string_view prefix) { | ||
| return std::string(prefix) + magic_enum::enum_name(s).data(); | ||
| }; | ||
| auto describe_circle = magic_enum::bind_front(describe, Shape::Circle); | ||
| REQUIRE(describe_circle("shape: ") == "shape: Circle"); | ||
| } | ||
|
|
||
| #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920 | ||
| # define MAGIC_ENUM_SUPPORTED_CONSTEXPR_FOR 1 | ||
| #endif | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was never required and should be removed; internal documentation remains in the comments.