|
| 1 | +module; |
| 2 | +#include <tuple> |
| 3 | +#include <type_traits> |
| 4 | + |
| 5 | +export module mcpplibs.primitives.primitive.traits; |
| 6 | + |
| 7 | +// Import the policy taxonomy so we can inspect policy::traits<>. |
| 8 | +export import mcpplibs.primitive.traits.policy; |
| 9 | + |
| 10 | +// Public default policy aliases — these must be visible to internal details. |
| 11 | +export namespace mcpplibs::primitives { |
| 12 | +using default_value_policy = ::mcpplibs::primitive::policy::unchecked_value; |
| 13 | +using default_type_policy = ::mcpplibs::primitive::policy::transparent_type; |
| 14 | +using default_error_policy = ::mcpplibs::primitive::policy::throw_error; |
| 15 | +using default_concurrency_policy = ::mcpplibs::primitive::policy::single_thread; |
| 16 | +} |
| 17 | + |
| 18 | +// Internal implementation details — not exported. |
| 19 | +namespace mcpplibs::primitives::traits::details { |
| 20 | +using policy_category = ::mcpplibs::primitive::policy::category; |
| 21 | + |
| 22 | +template <policy_category C, |
| 23 | + ::mcpplibs::primitive::policy::policy_type... Policies> |
| 24 | +struct count_matching_impl; |
| 25 | + |
| 26 | +template <policy_category C> struct count_matching_impl<C> { |
| 27 | + static constexpr std::size_t value = 0; |
| 28 | +}; |
| 29 | + |
| 30 | +template <policy_category C, ::mcpplibs::primitive::policy::policy_type First, |
| 31 | + ::mcpplibs::primitive::policy::policy_type... Rest> |
| 32 | +struct count_matching_impl<C, First, Rest...> { |
| 33 | + static constexpr std::size_t rest = count_matching_impl<C, Rest...>::value; |
| 34 | + static constexpr bool is_match = |
| 35 | + ::mcpplibs::primitive::policy::traits<First>::enabled && |
| 36 | + (::mcpplibs::primitive::policy::traits<First>::kind == C); |
| 37 | + static constexpr std::size_t value = rest + (is_match ? 1u : 0u); |
| 38 | +}; |
| 39 | + |
| 40 | +template <policy_category C, |
| 41 | + ::mcpplibs::primitive::policy::policy_type... Policies> |
| 42 | +constexpr std::size_t count_matching_v = |
| 43 | + count_matching_impl<C, Policies...>::value; |
| 44 | + |
| 45 | +template <policy_category C, |
| 46 | + ::mcpplibs::primitive::policy::policy_type... Policies> |
| 47 | +struct find_first_impl; |
| 48 | + |
| 49 | +template <policy_category C> struct find_first_impl<C> { |
| 50 | + using type = void; |
| 51 | +}; |
| 52 | + |
| 53 | +template <policy_category C, ::mcpplibs::primitive::policy::policy_type First, |
| 54 | + ::mcpplibs::primitive::policy::policy_type... Rest> |
| 55 | +struct find_first_impl<C, First, Rest...> { |
| 56 | + static constexpr bool is_match = |
| 57 | + ::mcpplibs::primitive::policy::traits<First>::enabled && |
| 58 | + (::mcpplibs::primitive::policy::traits<First>::kind == C); |
| 59 | + using type = std::conditional_t<is_match, First, |
| 60 | + typename find_first_impl<C, Rest...>::type>; |
| 61 | +}; |
| 62 | + |
| 63 | +template <policy_category C, primitive::policy::policy_type... Policies> |
| 64 | +using find_first_t = find_first_impl<C, Policies...>::type; |
| 65 | + |
| 66 | +template <policy_category C, primitive::policy::policy_type... Policies> |
| 67 | +struct resolve_policy_impl { |
| 68 | + static_assert(count_matching_v<C, Policies...> <= 1, |
| 69 | + "Multiple policies provided for the same category"); |
| 70 | + |
| 71 | + using found = find_first_t<C, Policies...>; |
| 72 | + using type = std::conditional_t< |
| 73 | + std::is_same_v<found, void>, |
| 74 | + std::conditional_t< |
| 75 | + C == policy_category::value, primitives::default_value_policy, |
| 76 | + std::conditional_t< |
| 77 | + C == policy_category::type, primitives::default_type_policy, |
| 78 | + std::conditional_t<C == policy_category::error, |
| 79 | + primitives::default_error_policy, |
| 80 | + primitives::default_concurrency_policy>>>, |
| 81 | + found>; |
| 82 | +}; |
| 83 | + |
| 84 | +template <policy_category C, |
| 85 | + ::mcpplibs::primitive::policy::policy_type... Policies> |
| 86 | +using resolve_policy_t = typename resolve_policy_impl<C, Policies...>::type; |
| 87 | + |
| 88 | +} // namespace mcpplibs::primitives::traits::details |
| 89 | + |
| 90 | +// Public API exported from this module. |
| 91 | +export namespace mcpplibs::primitives::traits { |
| 92 | + using policy_category = ::mcpplibs::primitive::policy::category; |
| 93 | + |
| 94 | + template <policy_category C, |
| 95 | + ::mcpplibs::primitive::policy::policy_type... Policies> |
| 96 | + using resolve_policy_t = |
| 97 | + ::mcpplibs::primitives::traits::details::resolve_policy_t<C, Policies...>; |
| 98 | + |
| 99 | + template <typename P> struct primitive_traits; // customization point for users |
| 100 | + |
| 101 | + // Forward-declare exported `primitive` (defined in impl module). |
| 102 | + template <typename T, ::mcpplibs::primitive::policy::policy_type... Policies> |
| 103 | + struct primitive; |
| 104 | + |
| 105 | + template <typename T, ::mcpplibs::primitive::policy::policy_type... Policies> |
| 106 | + struct primitive_traits<primitive<T, Policies...>> { |
| 107 | + using value_type = T; |
| 108 | + using policies = std::tuple<Policies...>; |
| 109 | + using value_policy = resolve_policy_t<policy_category::value, Policies...>; |
| 110 | + using type_policy = resolve_policy_t<policy_category::type, Policies...>; |
| 111 | + using error_policy = resolve_policy_t<policy_category::error, Policies...>; |
| 112 | + using concurrency_policy = |
| 113 | + resolve_policy_t<policy_category::concurrency, Policies...>; |
| 114 | + }; |
| 115 | + |
| 116 | +} // namespace traits::traits |
| 117 | + |
0 commit comments