Skip to content
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

rename cib::conditional to cib::constexpr_condition #612

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/flows.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
24 changes: 8 additions & 16 deletions include/cib/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <cib/builder_meta.hpp>
#include <cib/detail/components.hpp>
#include <cib/detail/conditional.hpp>
#include <cib/detail/config_details.hpp>
#include <cib/detail/config_item.hpp>
#include <cib/detail/constexpr_conditional.hpp>
#include <cib/detail/exports.hpp>
#include <cib/detail/extend.hpp>
#include <cib/detail/runtime_conditional.hpp>
Expand All @@ -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 <typename... Configs>
[[nodiscard]] CONSTEVAL auto config(Configs const &...configs) {
Expand Down Expand Up @@ -61,19 +61,11 @@ template <typename Service, typename... Args>
return detail::extend<Service, Args...>{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 <typename Predicate, typename... Configs>
requires std::is_default_constructible_v<Predicate>
[[nodiscard]] CONSTEVAL auto conditional(Predicate const &,
Configs const &...configs) {
return detail::conditional<Predicate, Configs...>{configs...};
}
template <stdx::ct_string Name>
constexpr auto constexpr_condition = []<typename P>(P) {
static_assert(std::is_default_constructible_v<P>);
return detail::constexpr_condition<Name, P>{};
};

template <stdx::ct_string Name>
constexpr auto runtime_condition = []<typename P>(P) {
Expand Down
34 changes: 0 additions & 34 deletions include/cib/detail/conditional.hpp

This file was deleted.

51 changes: 51 additions & 0 deletions include/cib/detail/constexpr_conditional.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <cib/detail/config_details.hpp>
#include <cib/detail/config_item.hpp>
#include <cib/detail/extend.hpp>

#include <stdx/compiler.hpp>
#include <stdx/ct_format.hpp>
#include <stdx/tuple.hpp>
#include <stdx/tuple_algorithms.hpp>

namespace cib::detail {
template <typename Cond, typename... Configs>
struct constexpr_conditional : config_item {
detail::config<Configs...> 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 <stdx::ct_string Name, typename... Ps> struct constexpr_condition {
constexpr static auto predicates = stdx::make_tuple(Ps{}...);

constexpr static auto ct_name = Name;

template <typename... Configs>
[[nodiscard]] CONSTEVAL auto operator()(Configs const &...configs) const {
return detail::constexpr_conditional<constexpr_condition<Name, Ps...>,
Configs...>{configs...};
}

explicit constexpr operator bool() const { return (Ps{}() and ...); }
};

} // namespace cib::detail
20 changes: 12 additions & 8 deletions test/cib/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ TEST_CASE("configuration with multiple components, services, and features") {
}

template <auto V> 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<TestCallback<0>>([]() { is_callback_invoked<0> = true; })));
};

Expand All @@ -112,7 +113,7 @@ template <int ConditionalValue> struct ConditionalTestProject {
cib::components<SimpleConditionalComponent<ConditionalValue>>);
};

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") {
Expand All @@ -133,10 +134,13 @@ TEST_CASE("configuration with one conditional component") {
}

template <int EnId, int Id> struct ConditionalComponent {
constexpr static auto config = cib::config(cib::conditional(
[]() { return EnId == Id; }, cib::extend<TestCallback<Id>>([]() {
is_callback_invoked<Id> = 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<TestCallback<Id>>(
[]() { is_callback_invoked<Id> = true; })));
};

template <int EnabledId> struct ConditionalConfig {
Expand All @@ -148,7 +152,7 @@ template <int EnabledId> struct ConditionalConfig {
ConditionalComponent<EnabledId, 2>>);
};

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;
Expand Down