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
3 changes: 3 additions & 0 deletions include/stdx/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ constexpr bool is_callable_v = is_function_v<T> or is_function_object_v<T>;

template <typename T> struct type_identity {
using type = T;
[[nodiscard]] friend constexpr auto operator==(type_identity const &,
type_identity const &)
-> bool = default;
};
template <typename T> using type_identity_t = typename type_identity<T>::type;
template <typename T>
Expand Down
37 changes: 35 additions & 2 deletions include/stdx/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ template <map_value_helper V> struct map_value_t {
constexpr static inline auto value = V.value;
};

template <typename T> consteval auto unwrap_map_value(T t) -> T { return t; }
template <map_value_helper V> consteval auto unwrap_map_value(map_value_t<V>) {
return V.value;
}

template <typename... Ts> struct type_map : Ts... {};
} // namespace detail

Expand All @@ -52,8 +57,6 @@ using tv_pair = tt_pair<K, detail::map_value_t<V>>;
template <detail::map_value_helper K, detail::map_value_helper V>
using vv_pair = tt_pair<detail::map_value_t<K>, detail::map_value_t<V>>;

template <typename... Ts> using type_map = shrink_t<detail::type_map<Ts...>>;

namespace detail {
template <typename K, typename Default>
constexpr static auto lookup(...) -> Default;
Expand Down Expand Up @@ -121,6 +124,36 @@ constexpr static auto reverse_value_lookup_v =
missing_t>(expand<M>())),
detail::map_value_t<Default>>::value;

template <typename... Ts>
class type_map : public shrink_t<detail::type_map<Ts...>> {
using map_t = shrink_t<detail::type_map<Ts...>>;

template <typename K> consteval static auto try_lookup() {
using V = type_lookup_t<map_t, K>;
if constexpr (requires { V{}; }) {
return detail::unwrap_map_value(V{});
} else {
return type_identity_v<V>;
}
}

public:
template <typename K> consteval auto operator[](K) const {
if constexpr (not same_as<decltype(try_lookup<K>()), missing_t>) {
return try_lookup<K>();
} else if constexpr (requires { typename K::type; }) {
return try_lookup<typename K::type>();
} else {
return missing;
}
}

template <typename K> consteval auto operator()(K) const {
return this->operator[](
detail::map_value_t<detail::map_value_helper{K::value}>{});
}
};

template <typename T, typename U>
[[nodiscard]] constexpr auto forward_like(U &&u) noexcept -> decltype(auto) {
constexpr auto t_is_const = std::is_const_v<std::remove_reference_t<T>>;
Expand Down
Loading