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
14 changes: 11 additions & 3 deletions include/stdx/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace stdx {
inline namespace v1 {
namespace _env {
template <auto Query, auto Value> struct ct_prop {
[[nodiscard]] CONSTEVAL static auto query(decltype(Query)) noexcept {
using query_t = std::remove_cvref_t<decltype(Query)>;

[[nodiscard]] CONSTEVAL static auto query(query_t) noexcept {
return Value;
}
};
Expand Down Expand Up @@ -61,6 +63,9 @@ template <std::size_t N> struct autowrap<str_lit_t<N>> {
template <typename T> autowrap(T) -> autowrap<T>;
template <std::size_t N> autowrap(str_lit_t<N>) -> autowrap<str_lit_t<N>>;

template <typename T, typename U>
using queries_equal = std::is_same<typename T::query_t, typename U::query_t>;

template <typename> struct for_each_pair;
template <std::size_t... Is> struct for_each_pair<std::index_sequence<Is...>> {
template <auto... Args>
Expand All @@ -72,7 +77,8 @@ 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>{};
return boost::mp11::mp_unique_if<boost::mp11::mp_append<new_env_t, Env>,
queries_equal>{};
};
} // namespace _env

Expand All @@ -81,7 +87,9 @@ 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...>>;
using append_env_t = boost::mp11::mp_unique_if<
boost::mp11::mp_reverse<boost::mp11::mp_append<Envs...>>,
_env::queries_equal>;

template <_env::autowrap... Args>
using make_env_t = extend_env_t<env<>, Args...>;
Expand Down
23 changes: 23 additions & 0 deletions test/env.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdx/ct_string.hpp>
#include <stdx/env.hpp>

#include <boost/mp11/algorithm.hpp>
#include <catch2/catch_test_macros.hpp>

namespace {
Expand Down Expand Up @@ -50,3 +51,25 @@ TEST_CASE("envlike concept", "[env]") {
static_assert(stdx::envlike<stdx::env<>>);
static_assert(stdx::envlike<stdx::make_env_t<custom, 17>>);
}

namespace {
template <typename Q> struct match_query {
template <typename T> using fn = std::is_same<Q, typename T::query_t>;
};
} // namespace

TEST_CASE("extending environment doesn't create duplicate keys", "[env]") {
using E1 = stdx::make_env_t<custom, 17>;
using E2 = stdx::extend_env_t<E1, custom, 18>;
static_assert(
boost::mp11::mp_count_if_q<E2, match_query<custom_t>>::value == 1);
}

TEST_CASE("appending environment doesn't create duplicate keys", "[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(boost::mp11::mp_count_if_q<E, match_query<custom_t>>::value ==
1);
}