-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #164: Add ForEach, which would allow a list of Controllers to b…
…e added
- Loading branch information
1 parent
7df2c33
commit 55b5a48
Showing
14 changed files
with
1,024 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2022 Richard Powell | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
#pragma once | ||
|
||
#include "Widget.h" | ||
|
||
namespace wxUI { | ||
|
||
// how does this work. We want to write: | ||
// wxUI::ForEach({ "These", "are", "some", "buttons"}, [](wxWindow*, auto str) { return wxUI::Button{str}; }); | ||
// or is this better: | ||
// wxUI::ForEach([](wxWindow*, auto str) { return wxUI::Button{str}; }, "These", "are", "some", "buttons" ); | ||
// wxUI::ForEach{ { "These", "are", "some", "buttons" } | std::views::filter([](auto a) { return a.size() < 5; }), [](wxWindow*, auto str) { return wxUI::Button{str}; }}; | ||
// would create an array of Buttons. | ||
namespace details { | ||
// // Concept to check if a type is callable | ||
// template <typename F, typename... Args> | ||
// concept Callable = std::invocable<F, Args...>; | ||
|
||
// Concept to check if a callable object returns something callable | ||
template <typename F, typename... Args> | ||
concept CallableAndReturnsCreateAndAddable = requires(F func, Args... args) { | ||
requires CreateAndAddable<std::invoke_result_t<F, Args...>>; | ||
}; | ||
|
||
template <typename F, typename... Args> | ||
concept CallableAndReturnsWindow = std::is_convertible_v<std::invoke_result_t<F, wxWindow*, Args...>, wxWindow*>; | ||
} | ||
|
||
template <details::CreateAndAddable W, typename Arg> | ||
struct ForEach { | ||
|
||
using LocalCreateForEach = std::function<W(Arg)>; | ||
|
||
template <details::CallableAndReturnsCreateAndAddable<Arg> CreateFunction> | ||
ForEach(std::initializer_list<Arg> args, CreateFunction createFunction) | ||
: args(args) | ||
, createFunction(createFunction) | ||
{ | ||
} | ||
|
||
template <details::CallableAndReturnsCreateAndAddable<Arg> CreateFunction> | ||
ForEach(wxSizerFlags const& flags, std::initializer_list<Arg> args, CreateFunction createFunction) | ||
: flags(flags) | ||
, args(args) | ||
, createFunction(createFunction) | ||
{ | ||
} | ||
|
||
template <details::CallableAndReturnsCreateAndAddable<Arg> CreateFunction> | ||
ForEach(details::Ranges::input_range_of<Arg> auto&& args, CreateFunction createFunction) | ||
: args(details::Ranges::ToVector<Arg>(std::forward<decltype(args)>(args))) | ||
, createFunction(createFunction) | ||
{ | ||
} | ||
|
||
template <details::CallableAndReturnsCreateAndAddable<Arg> CreateFunction> | ||
ForEach(wxSizerFlags const& flags, details::Ranges::input_range_of<Arg> auto&& args, CreateFunction createFunction) | ||
: flags(flags) | ||
, args(details::Ranges::ToVector<Arg>(std::forward<decltype(args)>(args))) | ||
, createFunction(createFunction) | ||
{ | ||
} | ||
|
||
void createAndAdd(wxWindow* parent, wxSizer* parentSizer, wxSizerFlags const& parentFlags) const | ||
{ | ||
for (auto& item : args) { | ||
createFunction(item).createAndAdd(parent, parentSizer, flags ? *flags : parentFlags); | ||
} | ||
} | ||
|
||
private: | ||
std::optional<wxSizerFlags> flags {}; | ||
std::vector<Arg> args; | ||
LocalCreateForEach createFunction; | ||
}; | ||
|
||
template <typename Function, typename Arg> | ||
ForEach(std::initializer_list<Arg> args, Function createFunction) -> ForEach<std::invoke_result_t<Function, Arg>, Arg>; | ||
|
||
template <typename Function, typename Arg> | ||
ForEach(wxSizerFlags const& flags, std::initializer_list<Arg> args, Function createFunction) -> ForEach<std::invoke_result_t<Function, Arg>, Arg>; | ||
|
||
template <typename Function, std::ranges::input_range Range> | ||
ForEach(Range&& args, Function createFunction) -> ForEach<std::invoke_result_t<Function, std::ranges::range_value_t<Range>>, std::ranges::range_value_t<Range>>; | ||
|
||
template <typename Function, std::ranges::input_range Range> | ||
ForEach(wxSizerFlags const& flags, Range&& args, Function createFunction) -> ForEach<std::invoke_result_t<Function, std::ranges::range_value_t<Range>>, std::ranges::range_value_t<Range>>; | ||
|
||
} | ||
|
||
#include "ZapMacros.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.