Debian Codegraph @

src/core/lib/gprpp/sorted_pack.h

raw
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_CORE_LIB_GPRPP_SORTED_PACK_H
16 #define GRPC_CORE_LIB_GPRPP_SORTED_PACK_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <type_traits>
21
22 namespace grpc_core {
23
24 namespace sorted_pack_detail {
25
26 // A list of types
27 template <typename... A>
28 struct Typelist {
29 template <template <typename...> class T>
30 using Instantiate = T<A...>;
31
32 template <typename C>
33 using PushFront = Typelist<C, A...>;
34 };
35
36 // Find the smallest element of Args, and the rest of the elements
37 template <template <typename, typename> class Cmp, typename Args>
38 struct Smallest;
39
40 template <template <typename, typename> class Cmp, typename Arg,
41 typename... Args>
42 struct Smallest<Cmp, Typelist<Arg, Args...>> {
43 using SmallestRest = Smallest<Cmp, Typelist<Args...>>;
44 using PrevSmallest = typename SmallestRest::Result;
45 using PrevRest = typename SmallestRest::Rest;
46 static constexpr bool kCmpResult = Cmp<Arg, PrevSmallest>::kValue;
47 using Result = typename std::conditional<kCmpResult, Arg, PrevSmallest>::type;
48 using Prefix = typename std::conditional<kCmpResult, PrevSmallest, Arg>::type;
49 using Rest = typename PrevRest::template PushFront<Prefix>;
50 };
51
52 template <template <typename, typename> class Cmp, typename Arg>
53 struct Smallest<Cmp, Typelist<Arg>> {
54 using Result = Arg;
55 using Rest = Typelist<>;
56 };
57
58 // Sort a list of types into a typelist
59 template <template <typename, typename> class Cmp, typename Args>
60 struct Sorted;
61
62 template <template <typename, typename> class Cmp, typename... Args>
63 struct Sorted<Cmp, Typelist<Args...>> {
64 using SmallestResult = Smallest<Cmp, Typelist<Args...>>;
65 using SmallestType = typename SmallestResult::Result;
66 using RestOfTypes = typename SmallestResult::Rest;
67 using SortedRestOfTypes = typename Sorted<Cmp, RestOfTypes>::Result;
68 using Result = typename SortedRestOfTypes::template PushFront<SmallestType>;
69 };
70
71 template <template <typename, typename> class Cmp, typename Arg>
72 struct Sorted<Cmp, Typelist<Arg>> {
73 using Result = Typelist<Arg>;
74 };
75
76 template <template <typename, typename> class Cmp>
77 struct Sorted<Cmp, Typelist<>> {
78 using Result = Typelist<>;
79 };
80
81 } // namespace sorted_pack_detail
82
83 // Given a type T<A...>, and a type comparator Cmp<P,Q>, and some set of types
84 // Args...:
85 // Sort Args... using Cmp into SortedArgs..., then instantiate T<SortedArgs...>
86 // as Type.
87 // Cmp<P,Q> should have a single constant `kValue` that is true if P < Q.
88 template <template <typename...> class T,
89 template <typename, typename> class Cmp, typename... Args>
90 struct WithSortedPack {
91 using Type = typename sorted_pack_detail::Sorted<
92 Cmp,
93 sorted_pack_detail::Typelist<Args...>>::Result::template Instantiate<T>;
94 };
95
96 } // namespace grpc_core
97
98 #endif // GRPC_CORE_LIB_GPRPP_SORTED_PACK_H