1 // Copyright 2022 gRPC authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef GRPC_SRC_CORE_UTIL_SORTED_PACK_H 16 #define GRPC_SRC_CORE_UTIL_SORTED_PACK_H 17 18 #include <grpc/support/port_platform.h> 19 20 #include <type_traits> 21 22 #include "src/core/util/type_list.h" 23 24 namespace grpc_core { 25 26 namespace sorted_pack_detail { 27 28 // Find the smallest element of Args, and the rest of the elements 29 template <template <typename, typename> class Cmp, typename Args> 30 struct Smallest; 31 32 template <template <typename, typename> class Cmp, typename Arg, 33 typename... Args> 34 struct Smallest<Cmp, Typelist<Arg, Args...>> { 35 using SmallestRest = Smallest<Cmp, Typelist<Args...>>; 36 using PrevSmallest = typename SmallestRest::Result; 37 using PrevRest = typename SmallestRest::Rest; 38 static constexpr bool kCmpResult = Cmp<Arg, PrevSmallest>::kValue; 39 using Result = typename std::conditional<kCmpResult, Arg, PrevSmallest>::type; 40 using Prefix = typename std::conditional<kCmpResult, PrevSmallest, Arg>::type; 41 using Rest = typename PrevRest::template PushFront<Prefix>; 42 }; 43 44 template <template <typename, typename> class Cmp, typename Arg> 45 struct Smallest<Cmp, Typelist<Arg>> { 46 using Result = Arg; 47 using Rest = Typelist<>; 48 }; 49 50 // Sort a list of types into a typelist 51 template <template <typename, typename> class Cmp, typename Args> 52 struct Sorted; 53 54 template <template <typename, typename> class Cmp, typename... Args> 55 struct Sorted<Cmp, Typelist<Args...>> { 56 using SmallestResult = Smallest<Cmp, Typelist<Args...>>; 57 using SmallestType = typename SmallestResult::Result; 58 using RestOfTypes = typename SmallestResult::Rest; 59 using SortedRestOfTypes = typename Sorted<Cmp, RestOfTypes>::Result; 60 using Result = typename SortedRestOfTypes::template PushFront<SmallestType>; 61 }; 62 63 template <template <typename, typename> class Cmp, typename Arg> 64 struct Sorted<Cmp, Typelist<Arg>> { 65 using Result = Typelist<Arg>; 66 }; 67 68 template <template <typename, typename> class Cmp> 69 struct Sorted<Cmp, Typelist<>> { 70 using Result = Typelist<>; 71 }; 72 73 } // namespace sorted_pack_detail 74 75 // Given a type T<A...>, and a type comparator Cmp<P,Q>, and some set of types 76 // Args...: 77 // Sort Args... using Cmp into SortedArgs..., then instantiate T<SortedArgs...> 78 // as Type. 79 // Cmp<P,Q> should have a single constant `kValue` that is true if P < Q. 80 template <template <typename...> class T, 81 template <typename, typename> class Cmp, typename... Args> 82 struct WithSortedPack { 83 using Type = typename sorted_pack_detail::Sorted< 84 Cmp, Typelist<Args...>>::Result::template Instantiate<T>; 85 }; 86 87 } // namespace grpc_core 88 89 #endif // GRPC_SRC_CORE_UTIL_SORTED_PACK_H 90