Skip to content

Commit

Permalink
remove cib config args, use template args instead
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevalenty committed Aug 27, 2024
1 parent 924cab6 commit 0cdb25e
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 68 deletions.
19 changes: 1 addition & 18 deletions include/cib/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
#include <stdx/compiler.hpp>

namespace cib {
/**
* List of arguments to configure compile-time initialization of components.
*
* @see cib::conditional
*/
template <auto... Args> constexpr static detail::args<Args...> args{};

/**
* Container for project and component configuration declarations.
*
Expand All @@ -32,13 +25,7 @@ template <auto... Args> constexpr static detail::args<Args...> args{};
*/
template <typename... Configs>
[[nodiscard]] CONSTEVAL auto config(Configs const &...configs) {
return detail::config{args<>, configs...};
}

template <auto... Args, typename... Configs>
[[nodiscard]] CONSTEVAL auto config(detail::args<Args...> config_args,
Configs const &...configs) {
return detail::config{config_args, configs...};
return detail::config{configs...};
}

/**
Expand All @@ -64,10 +51,6 @@ constexpr static detail::exports<Services...> exports{};
* @tparam Service
* Type name of the service to extend.
*
* @tparam ServiceTemplateArgs
* Template arguments to be passed to the service's
* builder add function.
*
* @param args
* Value arguments to be passed to the service's builder add function.
*/
Expand Down
10 changes: 4 additions & 6 deletions include/cib/detail/components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
namespace cib::detail {
template <typename... Components>
struct components : public detail::config_item {
template <typename... Args>
[[nodiscard]] constexpr auto extends_tuple(Args const &...args) const {
return stdx::tuple_cat(Components::config.extends_tuple(args...)...);
[[nodiscard]] constexpr auto extends_tuple() const {
return stdx::tuple_cat(Components::config.extends_tuple()...);
}

template <typename... Args>
[[nodiscard]] constexpr auto exports_tuple(Args const &...args) const {
return stdx::tuple_cat(Components::config.exports_tuple(args...)...);
[[nodiscard]] constexpr auto exports_tuple() const {
return stdx::tuple_cat(Components::config.exports_tuple()...);
}
};
} // namespace cib::detail
18 changes: 8 additions & 10 deletions include/cib/detail/conditional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@ namespace cib::detail {
template <typename Pred, typename... Configs>
requires std::is_default_constructible_v<Pred>
struct conditional : config_item {
detail::config<detail::args<>, Configs...> body;
detail::config<Configs...> body;

CONSTEVAL explicit conditional(Configs const &...configs)
: body{{}, configs...} {}
: body{configs...} {}

template <typename... Args>
[[nodiscard]] constexpr auto extends_tuple(Args const &...) const {
if constexpr (Pred{}(Args{}...)) {
return body.extends_tuple(Args{}...);
[[nodiscard]] constexpr auto extends_tuple() const {
if constexpr (Pred{}()) {
return body.extends_tuple();
} else {
return stdx::tuple<>{};
}
}

template <typename... Args>
[[nodiscard]] constexpr auto exports_tuple(Args const &...) const {
if constexpr (Pred{}(Args{}...)) {
return body.exports_tuple(Args{}...);
[[nodiscard]] constexpr auto exports_tuple() const {
if constexpr (Pred{}()) {
return body.exports_tuple();
} else {
return stdx::tuple<>{};
}
Expand Down
22 changes: 7 additions & 15 deletions include/cib/detail/config_details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,22 @@ template <auto Value>
constexpr static auto as_constant_v =
std::integral_constant<std::remove_cvref_t<decltype(Value)>, Value>{};

template <auto... Args> struct args {};

template <typename...> struct config;

template <auto... ConfigArgs, typename... ConfigTs>
struct config<args<ConfigArgs...>, ConfigTs...> : public detail::config_item {
template <typename... ConfigTs>
struct config : public detail::config_item {
stdx::tuple<ConfigTs...> configs_tuple;

CONSTEVAL explicit config(args<ConfigArgs...>, ConfigTs const &...configs)
CONSTEVAL explicit config(ConfigTs const &...configs)
: configs_tuple{configs...} {}

template <typename... Args>
[[nodiscard]] constexpr auto extends_tuple(Args const &...args) const {
[[nodiscard]] constexpr auto extends_tuple() const {
return configs_tuple.apply([&](auto const &...configs_pack) {
return stdx::tuple_cat(configs_pack.extends_tuple(
args..., as_constant_v<ConfigArgs>...)...);
return stdx::tuple_cat(configs_pack.extends_tuple()...);
});
}

template <typename... Args>
[[nodiscard]] constexpr auto exports_tuple(Args const &...args) const {
[[nodiscard]] constexpr auto exports_tuple() const {
return configs_tuple.apply([&](auto const &...configs_pack) {
return stdx::tuple_cat(configs_pack.exports_tuple(
args..., as_constant_v<ConfigArgs>...)...);
return stdx::tuple_cat(configs_pack.exports_tuple()...);
});
}
};
Expand Down
6 changes: 2 additions & 4 deletions include/cib/detail/config_item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace cib::detail {
struct config_item {
template <typename... Args>
[[nodiscard]] constexpr auto
extends_tuple(Args const &...) const -> stdx::tuple<> {
extends_tuple() const -> stdx::tuple<> {
return {};
}

template <typename... InitArgs>
[[nodiscard]] constexpr auto
exports_tuple(InitArgs const &...) const -> stdx::tuple<> {
exports_tuple() const -> stdx::tuple<> {
return {};
}
};
Expand Down
6 changes: 2 additions & 4 deletions include/cib/detail/exports.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ template <typename ServiceT, typename BuilderT> struct service_entry {
};

template <typename... Services> struct exports : public detail::config_item {
template <typename... InitArgs>
[[nodiscard]] constexpr auto extends_tuple(InitArgs const &...) const
[[nodiscard]] constexpr auto extends_tuple() const
-> stdx::tuple<extend<Services>...> {
return {extend<Services>{}...};
}

template <typename... InitArgs>
[[nodiscard]] constexpr auto
exports_tuple(InitArgs const &...) const -> stdx::tuple<Services...> {
exports_tuple() const -> stdx::tuple<Services...> {
return {};
}
};
Expand Down
3 changes: 1 addition & 2 deletions include/cib/detail/extend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ struct extend : public config_item {

CONSTEVAL explicit extend(Args const &...args) : args_tuple{args...} {}

template <typename... InitArgs>
[[nodiscard]] constexpr auto
extends_tuple(InitArgs const &...) const -> stdx::tuple<extend> {
extends_tuple() const -> stdx::tuple<extend> {
return {*this};
}
};
Expand Down
18 changes: 9 additions & 9 deletions test/cib/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,18 @@ TEST_CASE("configuration with multiple components, services, and features") {
}
}

template<auto V>
struct SimpleConditionalComponent {

constexpr static auto config = cib::config(cib::conditional(
[]<typename Arg>(Arg) { return Arg::value == 42; },
[]() { return V == 42; },
cib::extend<TestCallback<0>>([]() { is_callback_invoked<0> = true; })));
};

template <int ConditionalValue> struct ConditionalTestProject {
constexpr static auto config =
cib::config(cib::args<ConditionalValue>, cib::exports<TestCallback<0>>,
cib::components<SimpleConditionalComponent>);
cib::config(cib::exports<TestCallback<0>>,
cib::components<SimpleConditionalComponent<ConditionalValue>>);
};

TEST_CASE("configuration with one conditional component") {
Expand All @@ -131,21 +133,19 @@ TEST_CASE("configuration with one conditional component") {
}
}

template <int Id> struct ConditionalComponent {
template <int EnId, int Id> struct ConditionalComponent {
constexpr static auto config = cib::config(
cib::conditional([]<typename Arg>(Arg) { return Arg::value == Id; },
cib::conditional([]() { return EnId == Id; },
cib::extend<TestCallback<Id>>(
[]() { is_callback_invoked<Id> = true; })));
};

template <int EnabledId> struct ConditionalConfig {
constexpr static auto config = cib::config(
cib::args<EnabledId>,

cib::exports<TestCallback<0>, TestCallback<1>, TestCallback<2>>,

cib::components<ConditionalComponent<0>, ConditionalComponent<1>,
ConditionalComponent<2>>);
cib::components<ConditionalComponent<EnabledId, 0>, ConditionalComponent<EnabledId, 1>,
ConditionalComponent<EnabledId, 2>>);
};

TEST_CASE("configuration with conditional features") {
Expand Down

0 comments on commit 0cdb25e

Please sign in to comment.