Skip to content

Commit

Permalink
pre-v1.0
Browse files Browse the repository at this point in the history
dakka committed Mar 26, 2024
1 parent af15e31 commit c1af4d7
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -174,8 +174,8 @@ _output_
14 component::fragment
```
### `for_each`
Call supplied invocable for each enum. First parameter of invocable must be an enum value (passed by `for_each`). Optionally provide
any additional parameters. Works with lambdas, member functions, functions etc. When using a member function, the _first_ parameter
Call supplied invocable for each enum. Similar to `std::for_each` except first parameter of your invocable must be an enum value (passed by `for_each`).
Optionally provide any additional parameters. Works with lambdas, member functions, functions etc. When using a member function, the _first_ parameter
passed by your call must be the `this` pointer of the object. If you wish to pass a `reference` parameter, you must wrap it in
`std::ref`.

@@ -184,7 +184,6 @@ Returns `std::bind(std::forward<Fn>(func), std::placeholders::_1, std::forward<A
conjure_enum::for_each<component>([](component val, int other)
{
std::cout << static_cast<int>(val) << ' ' << other << '\n';
return 0;
}, 10);
```
_output_
@@ -207,7 +206,6 @@ auto myfunc { conjure_enum::for_each<component>([](component val, int other, int
{
std::cout << static_cast<int>(val) << ' ' << other << '\n';
tot += static_cast<int>(val);
return 0;
}, 10, std::ref(total)) };
myfunc(component::fragment);
std::cout << total << '\n';
9 changes: 9 additions & 0 deletions examples/example.cpp
Original file line number Diff line number Diff line change
@@ -100,5 +100,14 @@ int main()
else
std::cout << "invalid int to enum\n";

int total{};
auto myfunc { conjure_enum::for_each<component>([](component val, int other, int& tot)
{
std::cout << static_cast<int>(val) << ' ' << other << '\n';
tot += static_cast<int>(val);
}, 10, std::ref(total)) };
myfunc(component::fragment);
std::cout << total << '\n';

return 0;
}
10 changes: 10 additions & 0 deletions include/fix8/conjure_enum.hpp
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@
#include <tuple>
#include <concepts>
#include <optional>
#include <functional>
#include <ostream>
#include <cstddef>
#include <array>
@@ -237,6 +238,15 @@ class conjure_enum

template<typename T, T e>
static constexpr auto enum_name_v { enum_name<T, e>() };

template<typename T, typename Fn, typename... Args>
requires std::invocable<Fn&&, T, Args...>
[[maybe_unused]] static constexpr auto for_each(Fn&& func, Args&&... args) noexcept
{
for (const auto ev : enum_values<T>)
std::invoke(std::forward<Fn>(func), ev, std::forward<Args>(args)...);
return std::bind(std::forward<Fn>(func), std::placeholders::_1, std::forward<Args>(args)...);
}
};

//-----------------------------------------------------------------------------------------

0 comments on commit c1af4d7

Please sign in to comment.