From c43a3ce4aba617e80c94338cd17b5e4c75afb6ec Mon Sep 17 00:00:00 2001 From: Luke Valenty Date: Tue, 10 Sep 2024 22:32:12 -0700 Subject: [PATCH] rename cib::conditional to cib::constexpr_condition --- CMakeLists.txt | 3 +- docs/flows.adoc | 2 +- include/cib/config.hpp | 24 +++------ include/cib/detail/conditional.hpp | 34 ------------- include/cib/detail/constexpr_conditional.hpp | 51 ++++++++++++++++++++ test/cib/nexus.cpp | 20 +++++--- 6 files changed, 74 insertions(+), 60 deletions(-) delete mode 100644 include/cib/detail/conditional.hpp create mode 100644 include/cib/detail/constexpr_conditional.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e36ed04..bbb6d036 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,8 @@ target_sources( include/cib/cib.hpp include/cib/config.hpp include/cib/detail/components.hpp - include/cib/detail/conditional.hpp + include/cib/detail/constexpr_conditional.hpp + include/cib/detail/runtime_conditional.hpp include/cib/detail/config_details.hpp include/cib/detail/config_item.hpp include/cib/detail/exports.hpp diff --git a/docs/flows.adoc b/docs/flows.adoc index 0c1ae0e9..90aec246 100644 --- a/docs/flows.adoc +++ b/docs/flows.adoc @@ -61,7 +61,7 @@ dependencies. The `*` operator is used to explicitly add an action to the flow. Without the `*` operator an action is just a reference. A compile-time error will be triggered if an action is referenced without ever -being explicitly added to the flow. If an action is added under a compile-time +being explicitly added to the flow. If an action is added under a constexpr or runtime conditional, and the conditional is false, then it is as if the action was never added at all. diff --git a/include/cib/config.hpp b/include/cib/config.hpp index cd8fd7e9..a6fb1de6 100644 --- a/include/cib/config.hpp +++ b/include/cib/config.hpp @@ -2,9 +2,9 @@ #include #include -#include #include #include +#include #include #include #include @@ -22,8 +22,8 @@ namespace cib { * @see cib::components * @see cib::extend * @see cib::exports - * @see cib::conditional - * @see cib::runtime_conditional + * @see cib::constexpr_condition + * @see cib::runtime_condition */ template [[nodiscard]] CONSTEVAL auto config(Configs const &...configs) { @@ -61,19 +61,11 @@ template return detail::extend{args...}; } -/** - * Include configs based on predicate. - * - * If predicate evaluates to true, then the configs will be added to the - * configuration. Otherwise the configs contained in this conditional - * will not be added. - */ -template - requires std::is_default_constructible_v -[[nodiscard]] CONSTEVAL auto conditional(Predicate const &, - Configs const &...configs) { - return detail::conditional{configs...}; -} +template +constexpr auto constexpr_condition = [](P) { + static_assert(std::is_default_constructible_v

); + return detail::constexpr_condition{}; +}; template constexpr auto runtime_condition = [](P) { diff --git a/include/cib/detail/conditional.hpp b/include/cib/detail/conditional.hpp deleted file mode 100644 index 0dfa14d6..00000000 --- a/include/cib/detail/conditional.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include - -#include -#include - -namespace cib::detail { -template - requires std::is_default_constructible_v -struct conditional : config_item { - detail::config body; - - CONSTEVAL explicit conditional(Configs const &...configs) - : body{configs...} {} - - [[nodiscard]] constexpr auto extends_tuple() const { - if constexpr (Pred{}()) { - return body.extends_tuple(); - } else { - return stdx::tuple<>{}; - } - } - - [[nodiscard]] constexpr auto exports_tuple() const { - if constexpr (Pred{}()) { - return body.exports_tuple(); - } else { - return stdx::tuple<>{}; - } - } -}; -} // namespace cib::detail diff --git a/include/cib/detail/constexpr_conditional.hpp b/include/cib/detail/constexpr_conditional.hpp new file mode 100644 index 00000000..c8115d66 --- /dev/null +++ b/include/cib/detail/constexpr_conditional.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include + +namespace cib::detail { +template +struct constexpr_conditional : config_item { + detail::config body; + + CONSTEVAL explicit constexpr_conditional(Configs const &...configs) + : body{configs...} {} + + [[nodiscard]] constexpr auto extends_tuple() const { + if constexpr (Cond{}) { + return body.extends_tuple(); + } else { + return stdx::tuple<>{}; + } + } + + [[nodiscard]] constexpr auto exports_tuple() const { + if constexpr (Cond{}) { + return body.exports_tuple(); + } else { + return stdx::tuple<>{}; + } + } +}; + +template struct constexpr_condition { + constexpr static auto predicates = stdx::make_tuple(Ps{}...); + + constexpr static auto ct_name = Name; + + template + [[nodiscard]] CONSTEVAL auto operator()(Configs const &...configs) const { + return detail::constexpr_conditional, + Configs...>{configs...}; + } + + explicit constexpr operator bool() const { return (Ps{}() and ...); } +}; + +} // namespace cib::detail diff --git a/test/cib/nexus.cpp b/test/cib/nexus.cpp index 3ef9a4ab..777cf6c3 100644 --- a/test/cib/nexus.cpp +++ b/test/cib/nexus.cpp @@ -100,9 +100,10 @@ TEST_CASE("configuration with multiple components, services, and features") { } template struct SimpleConditionalComponent { + constexpr static auto when_v_is_42 = + cib::constexpr_condition<"when_v_is_42">([]() { return V == 42; }); - constexpr static auto config = cib::config(cib::conditional( - []() { return V == 42; }, + constexpr static auto config = cib::config(when_v_is_42( cib::extend>([]() { is_callback_invoked<0> = true; }))); }; @@ -112,7 +113,7 @@ template struct ConditionalTestProject { cib::components>); }; -TEST_CASE("configuration with one conditional component") { +TEST_CASE("configuration with one constexpr conditional component") { is_callback_invoked<0> = false; SECTION("invoke with argument match") { @@ -133,10 +134,13 @@ TEST_CASE("configuration with one conditional component") { } template struct ConditionalComponent { - constexpr static auto config = cib::config(cib::conditional( - []() { return EnId == Id; }, cib::extend>([]() { - is_callback_invoked = true; - }))); + constexpr static auto when_id_matches = + cib::constexpr_condition<"when_id_matches">( + []() { return EnId == Id; }); + + constexpr static auto config = + cib::config(when_id_matches(cib::extend>( + []() { is_callback_invoked = true; }))); }; template struct ConditionalConfig { @@ -148,7 +152,7 @@ template struct ConditionalConfig { ConditionalComponent>); }; -TEST_CASE("configuration with conditional features") { +TEST_CASE("configuration with constexpr conditional features") { is_callback_invoked<0> = false; is_callback_invoked<1> = false; is_callback_invoked<2> = false;