|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if __cplusplus >= 202002L |
| 4 | + |
| 5 | +#include <stdx/compiler.hpp> |
| 6 | +#include <stdx/ct_string.hpp> |
| 7 | + |
| 8 | +#include <boost/mp11/algorithm.hpp> |
| 9 | + |
| 10 | +namespace stdx { |
| 11 | +inline namespace v1 { |
| 12 | +template <auto Query, auto Value> struct ct_prop { |
| 13 | + [[nodiscard]] CONSTEVAL static auto query(decltype(Query)) noexcept { |
| 14 | + return Value; |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | +namespace _env { |
| 19 | +template <typename Q, typename Env> |
| 20 | +concept valid_query_for = requires(Env const &e) { e.query(Q{}); }; |
| 21 | + |
| 22 | +template <typename Q, typename... Envs> |
| 23 | +concept valid_query_over = (... or valid_query_for<Q, Envs>); |
| 24 | + |
| 25 | +template <typename Q> struct has_query { |
| 26 | + template <typename Env> |
| 27 | + using fn = std::bool_constant<valid_query_for<Q, Env>>; |
| 28 | +}; |
| 29 | +} // namespace _env |
| 30 | + |
| 31 | +template <typename... Envs> struct env { |
| 32 | + template <_env::valid_query_over<Envs...> Q> |
| 33 | + CONSTEVAL static auto query(Q) noexcept { |
| 34 | + using I = boost::mp11::mp_find_if_q<boost::mp11::mp_list<Envs...>, |
| 35 | + _env::has_query<Q>>; |
| 36 | + using E = boost::mp11::mp_at<boost::mp11::mp_list<Envs...>, I>; |
| 37 | + return Q{}(E{}); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +namespace _env { |
| 42 | +template <typename T> struct autowrap { |
| 43 | + // NOLINTNEXTLINE(google-explicit-constructor) |
| 44 | + CONSTEVAL autowrap(T t) : value(t) {} |
| 45 | + T value; |
| 46 | +}; |
| 47 | + |
| 48 | +// NOLINTNEXTLINE(modernize-avoid-c-arrays) |
| 49 | +template <std::size_t N> using str_lit_t = char const (&)[N]; |
| 50 | + |
| 51 | +template <std::size_t N> struct autowrap<str_lit_t<N>> { |
| 52 | + // NOLINTNEXTLINE(google-explicit-constructor) |
| 53 | + CONSTEVAL autowrap(str_lit_t<N> str) : value(str) {} |
| 54 | + stdx::ct_string<N> value; |
| 55 | +}; |
| 56 | + |
| 57 | +template <typename T> autowrap(T) -> autowrap<T>; |
| 58 | +template <std::size_t N> autowrap(str_lit_t<N>) -> autowrap<str_lit_t<N>>; |
| 59 | + |
| 60 | +template <auto V> struct wrap { |
| 61 | + constexpr static auto value = V; |
| 62 | +}; |
| 63 | + |
| 64 | +template <typename> struct for_each_pair; |
| 65 | +template <std::size_t... Is> struct for_each_pair<std::index_sequence<Is...>> { |
| 66 | + template <auto... Args> |
| 67 | + using type = |
| 68 | + env<ct_prop<boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>, |
| 69 | + 2 * Is>::value.value, |
| 70 | + boost::mp11::mp_at_c<boost::mp11::mp_list<wrap<Args>...>, |
| 71 | + (2 * Is) + 1>::value.value>...>; |
| 72 | +}; |
| 73 | + |
| 74 | +template <typename Env = env<>> |
| 75 | +constexpr auto make_env = []<autowrap... Args> { |
| 76 | + using new_env_t = typename for_each_pair< |
| 77 | + std::make_index_sequence<sizeof...(Args) / 2>>::template type<Args...>; |
| 78 | + return boost::mp11::mp_append<new_env_t, Env>{}; |
| 79 | +}; |
| 80 | +} // namespace _env |
| 81 | + |
| 82 | +template <typename Env, _env::autowrap... Args> |
| 83 | +using extend_env_t = |
| 84 | + decltype(_env::make_env<Env>.template operator()<Args...>()); |
| 85 | + |
| 86 | +template <_env::autowrap... Args> |
| 87 | +using make_env_t = extend_env_t<env<>, Args...>; |
| 88 | +} // namespace v1 |
| 89 | +} // namespace stdx |
| 90 | + |
| 91 | +#endif |
0 commit comments