|
1 | 1 | #pragma once |
2 | 2 | /** |
3 | | - * @file type_tag.hpp |
4 | | - * @brief Defines `type_tag<T>` / `type_c<T>` as both a type tag and |
5 | | - * a Patternia pattern (matches by static type). |
| 3 | + * @file type.hpp |
| 4 | + * @brief Static type-based patterns for Patternia. |
6 | 5 | * |
7 | | - * Part of Pattern Layer (namespace ptn::pattern::value) |
| 6 | + * Provides the static Type Pattern DSL: |
| 7 | + * - type::is<T> : match when subject's type is exactly T |
| 8 | + * - type::in<Ts...> : match when subject's type is in Ts... |
| 9 | + * - type::not_in<Ts...> |
| 10 | + * - type::from<Tpl> : match any specialization of template Tpl<...> |
| 11 | + * |
| 12 | + * Legacy: |
| 13 | + * - type_is<T> alias kept as compatibility wrapper → type::is<T> |
| 14 | + * |
| 15 | + * This file forms the Pattern Layer entry for type patterns. |
8 | 16 | */ |
9 | 17 |
|
10 | 18 | #include <type_traits> |
| 19 | +#include <utility> |
| 20 | + |
11 | 21 | #include "ptn/pattern/pattern_base.hpp" |
12 | 22 |
|
13 | 23 | namespace ptn::pattern::type { |
14 | 24 |
|
15 | | - /** |
16 | | - * @brief Compile-time wrapper around a C++ type, and also a pattern |
17 | | - * that matches when the subject's (decayed) type equals `T`. |
18 | | - */ |
| 25 | + //==================================================================== |
| 26 | + // Internal helper: is specialization of template? |
| 27 | + //==================================================================== |
| 28 | + namespace detail { |
| 29 | + |
| 30 | + template <template <typename...> typename Tpl, typename T> |
| 31 | + struct is_specialization_of : std::false_type {}; |
| 32 | + |
| 33 | + template <template <typename...> typename Tpl, typename... Args> |
| 34 | + struct is_specialization_of<Tpl, Tpl<Args...>> : std::true_type {}; |
| 35 | + |
| 36 | + } // namespace detail |
| 37 | + |
| 38 | + //==================================================================== |
| 39 | + // type::is<T> — static type equality |
| 40 | + //==================================================================== |
| 41 | + |
19 | 42 | template <typename T> |
20 | | - struct type_tag : ptn::pattern::pattern_base<type_tag<T>> { |
| 43 | + struct is_pattern : ptn::pattern::pattern_base<is_pattern<T>> { |
21 | 44 |
|
22 | 45 | template <typename X> |
23 | 46 | constexpr bool match(X &&) const noexcept { |
24 | | - return std::is_same_v<T, std::decay_t<X>>; |
| 47 | + return std::is_same_v<std::decay_t<X>, T>; |
| 48 | + } |
| 49 | + template <typename X> |
| 50 | + constexpr auto bind(X &&x) const noexcept { |
| 51 | + return std::forward<X>(x); |
25 | 52 | } |
| 53 | + }; |
| 54 | + |
| 55 | + template <typename T> |
| 56 | + inline constexpr is_pattern<T> is{}; |
26 | 57 |
|
| 58 | + //==================================================================== |
| 59 | + // type::in<Ts...> — membership |
| 60 | + //==================================================================== |
| 61 | + |
| 62 | + template <typename... Ts> |
| 63 | + struct in_pattern : ptn::pattern::pattern_base<in_pattern<Ts...>> { |
| 64 | + |
| 65 | + template <typename X> |
| 66 | + constexpr bool match(X &&) const noexcept { |
| 67 | + using D = std::decay_t<X>; |
| 68 | + return (std::is_same_v<D, Ts> || ...); |
| 69 | + } |
27 | 70 | template <typename X> |
28 | 71 | constexpr auto bind(X &&x) const noexcept { |
29 | 72 | return std::forward<X>(x); |
30 | 73 | } |
31 | 74 | }; |
32 | 75 |
|
33 | | - /** |
34 | | - * @brief Shorthand constant for `type_tag<T>`. |
35 | | - */ |
| 76 | + template <typename... Ts> |
| 77 | + inline constexpr in_pattern<Ts...> in{}; |
| 78 | + |
| 79 | + //==================================================================== |
| 80 | + // type::not_in<Ts...> |
| 81 | + //==================================================================== |
| 82 | + |
| 83 | + template <typename... Ts> |
| 84 | + struct not_in_pattern : ptn::pattern::pattern_base<not_in_pattern<Ts...>> { |
| 85 | + |
| 86 | + template <typename X> |
| 87 | + constexpr bool match(X &&v) const noexcept { |
| 88 | + return !in_pattern<Ts...>{}.match(std::forward<X>(v)); |
| 89 | + } |
| 90 | + template <typename X> |
| 91 | + constexpr auto bind(X &&x) const noexcept { |
| 92 | + return std::forward<X>(x); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + template <typename... Ts> |
| 97 | + inline constexpr not_in_pattern<Ts...> not_in{}; |
| 98 | + |
| 99 | + //==================================================================== |
| 100 | + // type::from<Tpl> — match template family |
| 101 | + //==================================================================== |
| 102 | + |
| 103 | + template <template <typename...> typename Tpl> |
| 104 | + struct from_pattern : ptn::pattern::pattern_base<from_pattern<Tpl>> { |
| 105 | + |
| 106 | + template <typename X> |
| 107 | + constexpr bool match(X &&) const noexcept { |
| 108 | + using D = std::decay_t<X>; |
| 109 | + return detail::is_specialization_of<Tpl, D>::value; |
| 110 | + } |
| 111 | + template <typename X> |
| 112 | + constexpr auto bind(X &&x) const noexcept { |
| 113 | + return std::forward<X>(x); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + template <template <typename...> typename Tpl> |
| 118 | + inline constexpr from_pattern<Tpl> from{}; |
| 119 | + |
| 120 | + //==================================================================== |
| 121 | + // Legacy compatibility |
| 122 | + //==================================================================== |
| 123 | + |
| 124 | + template <typename T> |
| 125 | + using type_tag = is_pattern<T>; |
| 126 | + |
36 | 127 | template <typename T> |
37 | | - inline constexpr type_tag<T> type_is{}; |
| 128 | + inline constexpr auto type_is = is<T>; |
38 | 129 |
|
39 | 130 | } // namespace ptn::pattern::type |
0 commit comments