1 #ifndef ANDROID_PDX_RPC_SEQUENCE_H_
2 #define ANDROID_PDX_RPC_SEQUENCE_H_
3 
4 #include <cstdint>
5 
6 namespace android {
7 namespace pdx {
8 namespace rpc {
9 
10 // Provides a C++11 implementation of C++14 index_sequence and
11 // make_index_sequence for compatibility with common compilers. This
12 // implementation may be conditionally replaced with compiler-provided versions
13 // when C++14 support is available.
14 
15 // Utility to capture a sequence of unsigned indices.
16 template <std::size_t... I>
17 struct IndexSequence {
18   using type = IndexSequence;
19   using value_type = std::size_t;
sizeIndexSequence20   static constexpr std::size_t size() { return sizeof...(I); }
21 };
22 
23 namespace detail {
24 
25 // Helper class to merge and renumber sequence parts in log N instantiations.
26 template <typename S1, typename S2>
27 struct MergeSequencesAndRenumber;
28 
29 template <std::size_t... I1, std::size_t... I2>
30 struct MergeSequencesAndRenumber<IndexSequence<I1...>, IndexSequence<I2...>>
31     : IndexSequence<I1..., (sizeof...(I1) + I2)...> {};
32 
33 }  // namespace detail
34 
35 // Utility to build an IndexSequence with N indices.
36 template <std::size_t N>
37 struct MakeIndexSequence : detail::MergeSequencesAndRenumber<
38                                typename MakeIndexSequence<N / 2>::type,
39                                typename MakeIndexSequence<N - N / 2>::type> {};
40 
41 // Identity sequences.
42 template <>
43 struct MakeIndexSequence<0> : IndexSequence<> {};
44 template <>
45 struct MakeIndexSequence<1> : IndexSequence<0> {};
46 
47 // Utility to build an IndexSequence with indices for each element of a
48 // parameter pack.
49 template <typename... T>
50 using IndexSequenceFor = MakeIndexSequence<sizeof...(T)>;
51 
52 }  // namespace rpc
53 }  // namespace pdx
54 }  // namespace android
55 
56 #endif  // ANDROID_PDX_RPC_SEQUENCE_H_
57