Skip to content
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
23 changes: 15 additions & 8 deletions include/stdx/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

#include <stdx/compiler.hpp>
#include <stdx/ct_string.hpp>
#include <stdx/type_traits.hpp>

#include <boost/mp11/algorithm.hpp>

namespace stdx {
inline namespace v1 {
namespace _env {
template <auto Query, auto Value> struct ct_prop {
[[nodiscard]] CONSTEVAL static auto query(decltype(Query)) noexcept {
return Value;
}
};

namespace _env {
template <typename Q, typename Env>
concept valid_query_for = requires(Env const &e) { e.query(Q{}); };

Expand All @@ -38,6 +39,9 @@ template <typename... Envs> struct env {
}
};

template <typename T>
concept envlike = is_specialization_of_v<T, env>;

namespace _env {
template <typename T> struct autowrap {
// NOLINTNEXTLINE(google-explicit-constructor)
Expand All @@ -64,25 +68,28 @@ template <auto V> struct wrap {
template <typename> struct for_each_pair;
template <std::size_t... Is> struct for_each_pair<std::index_sequence<Is...>> {
template <auto... Args>
using type =
env<ct_prop<boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>,
2 * Is>::value.value,
boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>,
(2 * Is) + 1>::value.value>...>;
using type = env<
_env::ct_prop<boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>,
2 * Is>::value.value,
boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>,
(2 * Is) + 1>::value.value>...>;
};

template <typename Env = env<>>
template <envlike Env = env<>>
constexpr auto make_env = []<autowrap... Args> {
using new_env_t = typename for_each_pair<
std::make_index_sequence<sizeof...(Args) / 2>>::template type<Args...>;
return boost::mp11::mp_append<new_env_t, Env>{};
};
} // namespace _env

template <typename Env, _env::autowrap... Args>
template <envlike Env, _env::autowrap... Args>
using extend_env_t =
decltype(_env::make_env<Env>.template operator()<Args...>());

template <envlike... Envs>
using append_env_t = boost::mp11::mp_reverse<boost::mp11::mp_append<Envs...>>;

template <_env::autowrap... Args>
using make_env_t = extend_env_t<env<>, Args...>;
} // namespace v1
Expand Down
18 changes: 13 additions & 5 deletions test/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ TEST_CASE("lookup query with default", "[env]") {
static_assert(custom(stdx::env<>{}) == 42);
}

TEST_CASE("lookup with single-value prop", "[env]") {
using E = stdx::ct_prop<custom, 17>;
static_assert(custom(E{}) == 17);
}

TEST_CASE("make an environment", "[env]") {
using E = stdx::make_env_t<custom, 17>;
static_assert(custom(E{}) == 17);
Expand All @@ -37,8 +32,21 @@ TEST_CASE("extend an environment", "[env]") {
static_assert(custom(E2{}) == 18);
}

TEST_CASE("append an environment", "[env]") {
using E1 = stdx::make_env_t<custom, 17>;
using E2 = stdx::make_env_t<custom, 18>;
using E3 = stdx::make_env_t<custom, 19>;
using E = stdx::append_env_t<E1, E2, E3>;
static_assert(custom(E{}) == 19);
}

TEST_CASE("environment converts string literals to ct_string", "[env]") {
using namespace stdx::literals;
using E = stdx::make_env_t<custom, "hello">;
static_assert(custom(E{}) == "hello"_cts);
}

TEST_CASE("envlike concept", "[env]") {
static_assert(stdx::envlike<stdx::env<>>);
static_assert(stdx::envlike<stdx::make_env_t<custom, 17>>);
}