1#ifndef ANDROID_PDX_RPC_SEQUENCE_H_
2#define ANDROID_PDX_RPC_SEQUENCE_H_
3
4#include <cstdint>
5
6namespace android {
7namespace pdx {
8namespace 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.
16template <std::size_t... I>
17struct IndexSequence {
18  using type = IndexSequence;
19  using value_type = std::size_t;
20  static constexpr std::size_t size() { return sizeof...(I); }
21};
22
23namespace detail {
24
25// Helper class to merge and renumber sequence parts in log N instantiations.
26template <typename S1, typename S2>
27struct MergeSequencesAndRenumber;
28
29template <std::size_t... I1, std::size_t... I2>
30struct 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.
36template <std::size_t N>
37struct MakeIndexSequence : detail::MergeSequencesAndRenumber<
38                               typename MakeIndexSequence<N / 2>::type,
39                               typename MakeIndexSequence<N - N / 2>::type> {};
40
41// Identity sequences.
42template <>
43struct MakeIndexSequence<0> : IndexSequence<> {};
44template <>
45struct MakeIndexSequence<1> : IndexSequence<0> {};
46
47// Utility to build an IndexSequence with indices for each element of a
48// parameter pack.
49template <typename... T>
50using IndexSequenceFor = MakeIndexSequence<sizeof...(T)>;
51
52}  // namespace rpc
53}  // namespace pdx
54}  // namespace android
55
56#endif  // ANDROID_PDX_RPC_SEQUENCE_H_
57