Skip to content

Commit c43a3ce

Browse files
committed
rename cib::conditional to cib::constexpr_condition
1 parent cf6ba56 commit c43a3ce

File tree

6 files changed

+74
-60
lines changed

6 files changed

+74
-60
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ target_sources(
4848
include/cib/cib.hpp
4949
include/cib/config.hpp
5050
include/cib/detail/components.hpp
51-
include/cib/detail/conditional.hpp
51+
include/cib/detail/constexpr_conditional.hpp
52+
include/cib/detail/runtime_conditional.hpp
5253
include/cib/detail/config_details.hpp
5354
include/cib/detail/config_item.hpp
5455
include/cib/detail/exports.hpp

docs/flows.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ dependencies.
6161
The `*` operator is used to explicitly add an action to the
6262
flow. Without the `*` operator an action is just a reference.
6363
A compile-time error will be triggered if an action is referenced without ever
64-
being explicitly added to the flow. If an action is added under a compile-time
64+
being explicitly added to the flow. If an action is added under a constexpr
6565
or runtime conditional, and the conditional is false, then it is as if the
6666
action was never added at all.
6767

include/cib/config.hpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include <cib/builder_meta.hpp>
44
#include <cib/detail/components.hpp>
5-
#include <cib/detail/conditional.hpp>
65
#include <cib/detail/config_details.hpp>
76
#include <cib/detail/config_item.hpp>
7+
#include <cib/detail/constexpr_conditional.hpp>
88
#include <cib/detail/exports.hpp>
99
#include <cib/detail/extend.hpp>
1010
#include <cib/detail/runtime_conditional.hpp>
@@ -22,8 +22,8 @@ namespace cib {
2222
* @see cib::components
2323
* @see cib::extend
2424
* @see cib::exports
25-
* @see cib::conditional
26-
* @see cib::runtime_conditional
25+
* @see cib::constexpr_condition
26+
* @see cib::runtime_condition
2727
*/
2828
template <typename... Configs>
2929
[[nodiscard]] CONSTEVAL auto config(Configs const &...configs) {
@@ -61,19 +61,11 @@ template <typename Service, typename... Args>
6161
return detail::extend<Service, Args...>{args...};
6262
}
6363

64-
/**
65-
* Include configs based on predicate.
66-
*
67-
* If predicate evaluates to true, then the configs will be added to the
68-
* configuration. Otherwise the configs contained in this conditional
69-
* will not be added.
70-
*/
71-
template <typename Predicate, typename... Configs>
72-
requires std::is_default_constructible_v<Predicate>
73-
[[nodiscard]] CONSTEVAL auto conditional(Predicate const &,
74-
Configs const &...configs) {
75-
return detail::conditional<Predicate, Configs...>{configs...};
76-
}
64+
template <stdx::ct_string Name>
65+
constexpr auto constexpr_condition = []<typename P>(P) {
66+
static_assert(std::is_default_constructible_v<P>);
67+
return detail::constexpr_condition<Name, P>{};
68+
};
7769

7870
template <stdx::ct_string Name>
7971
constexpr auto runtime_condition = []<typename P>(P) {

include/cib/detail/conditional.hpp

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include <cib/detail/config_details.hpp>
4+
#include <cib/detail/config_item.hpp>
5+
#include <cib/detail/extend.hpp>
6+
7+
#include <stdx/compiler.hpp>
8+
#include <stdx/ct_format.hpp>
9+
#include <stdx/tuple.hpp>
10+
#include <stdx/tuple_algorithms.hpp>
11+
12+
namespace cib::detail {
13+
template <typename Cond, typename... Configs>
14+
struct constexpr_conditional : config_item {
15+
detail::config<Configs...> body;
16+
17+
CONSTEVAL explicit constexpr_conditional(Configs const &...configs)
18+
: body{configs...} {}
19+
20+
[[nodiscard]] constexpr auto extends_tuple() const {
21+
if constexpr (Cond{}) {
22+
return body.extends_tuple();
23+
} else {
24+
return stdx::tuple<>{};
25+
}
26+
}
27+
28+
[[nodiscard]] constexpr auto exports_tuple() const {
29+
if constexpr (Cond{}) {
30+
return body.exports_tuple();
31+
} else {
32+
return stdx::tuple<>{};
33+
}
34+
}
35+
};
36+
37+
template <stdx::ct_string Name, typename... Ps> struct constexpr_condition {
38+
constexpr static auto predicates = stdx::make_tuple(Ps{}...);
39+
40+
constexpr static auto ct_name = Name;
41+
42+
template <typename... Configs>
43+
[[nodiscard]] CONSTEVAL auto operator()(Configs const &...configs) const {
44+
return detail::constexpr_conditional<constexpr_condition<Name, Ps...>,
45+
Configs...>{configs...};
46+
}
47+
48+
explicit constexpr operator bool() const { return (Ps{}() and ...); }
49+
};
50+
51+
} // namespace cib::detail

test/cib/nexus.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ TEST_CASE("configuration with multiple components, services, and features") {
100100
}
101101

102102
template <auto V> struct SimpleConditionalComponent {
103+
constexpr static auto when_v_is_42 =
104+
cib::constexpr_condition<"when_v_is_42">([]() { return V == 42; });
103105

104-
constexpr static auto config = cib::config(cib::conditional(
105-
[]() { return V == 42; },
106+
constexpr static auto config = cib::config(when_v_is_42(
106107
cib::extend<TestCallback<0>>([]() { is_callback_invoked<0> = true; })));
107108
};
108109

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

115-
TEST_CASE("configuration with one conditional component") {
116+
TEST_CASE("configuration with one constexpr conditional component") {
116117
is_callback_invoked<0> = false;
117118

118119
SECTION("invoke with argument match") {
@@ -133,10 +134,13 @@ TEST_CASE("configuration with one conditional component") {
133134
}
134135

135136
template <int EnId, int Id> struct ConditionalComponent {
136-
constexpr static auto config = cib::config(cib::conditional(
137-
[]() { return EnId == Id; }, cib::extend<TestCallback<Id>>([]() {
138-
is_callback_invoked<Id> = true;
139-
})));
137+
constexpr static auto when_id_matches =
138+
cib::constexpr_condition<"when_id_matches">(
139+
[]() { return EnId == Id; });
140+
141+
constexpr static auto config =
142+
cib::config(when_id_matches(cib::extend<TestCallback<Id>>(
143+
[]() { is_callback_invoked<Id> = true; })));
140144
};
141145

142146
template <int EnabledId> struct ConditionalConfig {
@@ -148,7 +152,7 @@ template <int EnabledId> struct ConditionalConfig {
148152
ConditionalComponent<EnabledId, 2>>);
149153
};
150154

151-
TEST_CASE("configuration with conditional features") {
155+
TEST_CASE("configuration with constexpr conditional features") {
152156
is_callback_invoked<0> = false;
153157
is_callback_invoked<1> = false;
154158
is_callback_invoked<2> = false;

0 commit comments

Comments
 (0)