|
| 1 | +//===--- Pack.h - Helpers for wording with class template packs -*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_BASIC_PACKS_H |
| 14 | +#define SWIFT_BASIC_PACKS_H |
| 15 | + |
| 16 | +namespace swift { |
| 17 | +namespace packs { |
| 18 | + |
| 19 | +/// A pack of types. |
| 20 | +template <class...> |
| 21 | +struct Pack; |
| 22 | + |
| 23 | +/// A trait indicating whether the given type is a specialization |
| 24 | +/// of Pack<...>. |
| 25 | +template <class> |
| 26 | +struct IsPack { |
| 27 | + static constexpr bool value = false; |
| 28 | +}; |
| 29 | + |
| 30 | +template <class... Components> |
| 31 | +struct IsPack<Pack<Components...>> { |
| 32 | + static constexpr bool value = true; |
| 33 | +}; |
| 34 | + |
| 35 | +/// Transform a variadic list of types using the given transform, a class |
| 36 | +/// template which is expected to define a `result` type providing the |
| 37 | +/// result of the transform. |
| 38 | +/// |
| 39 | +/// template <class Arg, class Component> |
| 40 | +/// class Transform { |
| 41 | +/// using result = ...; |
| 42 | +/// }; |
| 43 | +/// |
| 44 | +/// In principle, this would be cleaner as a member template, but that |
| 45 | +/// works out poorly because in practice the member template must be |
| 46 | +/// partially specialized in order to destructure it. |
| 47 | +template <template <class, class> class Transform, |
| 48 | + class TransformArg, class... Values> |
| 49 | +struct PackMapComponents { |
| 50 | + using result = Pack<typename Transform<TransformArg, Values>::result...>; |
| 51 | +}; |
| 52 | + |
| 53 | +/// Transform the components of a pack using the given transform, a class |
| 54 | +/// template which is expected to define a `result` type providing the |
| 55 | +/// result of the transform. |
| 56 | +/// |
| 57 | +/// template <class Arg, class Component> |
| 58 | +/// class Transform { |
| 59 | +/// using result = ...; |
| 60 | +/// }; |
| 61 | +template <template <class, class> class Transform, class TransformArg, |
| 62 | + class P> |
| 63 | +struct PackMap; |
| 64 | + |
| 65 | +template <template <class, class> class Transform, class TransformArg, |
| 66 | + class... Components> |
| 67 | +struct PackMap<Transform, TransformArg, Pack<Components...>> |
| 68 | + : PackMapComponents<Transform, TransformArg, Components...> {}; |
| 69 | + |
| 70 | +/// Concatenate the components of a variadic list of packs. |
| 71 | +template <class... Packs> |
| 72 | +struct PackConcat; |
| 73 | + |
| 74 | +template <> |
| 75 | +struct PackConcat<> { |
| 76 | + using result = Pack<>; |
| 77 | +}; |
| 78 | + |
| 79 | +template <class First> |
| 80 | +struct PackConcat<First> { |
| 81 | + static_assert(IsPack<First>::value, "argument is not a pack"); |
| 82 | + using result = First; |
| 83 | +}; |
| 84 | + |
| 85 | +template <class... FirstComponents, class... SecondComponents> |
| 86 | +struct PackConcat<Pack<FirstComponents...>, Pack<SecondComponents...>> { |
| 87 | + using result = Pack<FirstComponents..., SecondComponents...>; |
| 88 | +}; |
| 89 | + |
| 90 | +template <class Head, class... Tail> |
| 91 | +struct PackConcat<Head, Tail...> |
| 92 | + : PackConcat<Head, typename PackConcat<Tail...>::result> {}; |
| 93 | + |
| 94 | +/// Flatten a pack of packs by concatenating their components. |
| 95 | +template <class> |
| 96 | +struct PackFlatten; |
| 97 | + |
| 98 | +template <class... PackComponents> |
| 99 | +struct PackFlatten<Pack<PackComponents...>> |
| 100 | + : PackConcat<PackComponents...> {}; |
| 101 | + |
| 102 | +/// Apply the given pack-producing transform to each component of a pack, |
| 103 | +/// then concatenate the results. |
| 104 | +/// |
| 105 | +/// For example, if we start with Pack<A, B, C>, and the transform turns: |
| 106 | +/// A => Pack<X, Y> |
| 107 | +/// B => Pack<> |
| 108 | +/// C => Pack<Z> |
| 109 | +/// then the result will be Pack<X, Y, Z>. |
| 110 | +template <template <class, class> class Transform, class TransformArg, class P> |
| 111 | +struct PackFlatMap |
| 112 | + : PackFlatten<typename PackMap<Transform, TransformArg, P>::result> {}; |
| 113 | + |
| 114 | +/// Reverse a variadic list of types. |
| 115 | +template <class... PackComponents> |
| 116 | +struct PackReverseComponents; |
| 117 | + |
| 118 | +template <> |
| 119 | +struct PackReverseComponents<> { |
| 120 | + using result = Pack<>; |
| 121 | +}; |
| 122 | + |
| 123 | +template <class Head> |
| 124 | +struct PackReverseComponents<Head> { |
| 125 | + using result = Pack<Head>; |
| 126 | +}; |
| 127 | + |
| 128 | +template <class Head, class... Tail> |
| 129 | +struct PackReverseComponents<Head, Tail...> |
| 130 | + : PackConcat<typename PackReverseComponents<Tail...>::result, |
| 131 | + Pack<Head>> {}; |
| 132 | + |
| 133 | + |
| 134 | +/// Reverse a pack. |
| 135 | +template <class> |
| 136 | +struct PackReverse; |
| 137 | + |
| 138 | +template <class... PackComponents> |
| 139 | +struct PackReverse<Pack<PackComponents...>> |
| 140 | + : PackReverseComponents<PackComponents...> {}; |
| 141 | + |
| 142 | +/// Determine whether the given pack is transitively ordered according to |
| 143 | +/// the given predicate: |
| 144 | +/// |
| 145 | +/// template <class First, class Second> |
| 146 | +/// struct IsOrdered { |
| 147 | +/// static constexpr bool value; |
| 148 | +/// }; |
| 149 | +template <template <class, class> class IsOrdered, |
| 150 | + class... Components> |
| 151 | +struct PackComponentsAreOrdered; |
| 152 | + |
| 153 | +template <template <class, class> class IsOrdered> |
| 154 | +struct PackComponentsAreOrdered<IsOrdered> { |
| 155 | + static constexpr bool value = true; |
| 156 | +}; |
| 157 | + |
| 158 | +template <template <class, class> class IsOrdered, |
| 159 | + class A> |
| 160 | +struct PackComponentsAreOrdered<IsOrdered, A> { |
| 161 | + static constexpr bool value = true; |
| 162 | +}; |
| 163 | + |
| 164 | +template <template <class, class> class IsOrdered, |
| 165 | + class A, class B> |
| 166 | +struct PackComponentsAreOrdered<IsOrdered, A, B> : IsOrdered<A, B> {}; |
| 167 | + |
| 168 | +template <template <class, class> class IsOrdered, |
| 169 | + class A, class B, class... C> |
| 170 | +struct PackComponentsAreOrdered<IsOrdered, A, B, C...> { |
| 171 | + static constexpr bool value = |
| 172 | + IsOrdered<A, B>::value && |
| 173 | + PackComponentsAreOrdered<IsOrdered, B, C...>::value; |
| 174 | +}; |
| 175 | + |
| 176 | +/// Determine whether the given pack is well-ordered according to the given |
| 177 | +/// predicate: |
| 178 | +/// |
| 179 | +/// template <class First, class Second> |
| 180 | +/// struct IsOrdered { |
| 181 | +/// static constexpr bool value; |
| 182 | +/// }; |
| 183 | +template <template <class, class> class IsOrdered, |
| 184 | + class P> |
| 185 | +struct PackIsOrdered; |
| 186 | + |
| 187 | +template <template <class, class> class IsOrdered, |
| 188 | + class... Components> |
| 189 | +struct PackIsOrdered<IsOrdered, Pack<Components...>> |
| 190 | + : PackComponentsAreOrdered<IsOrdered, Components...> {}; |
| 191 | + |
| 192 | +} // end namespace packs |
| 193 | +} // end namespace swift |
| 194 | + |
| 195 | +#endif |
0 commit comments