1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#pragma once
#include <type_traits>
#include <tuple>
namespace mbgl {
template <class T, class... Ts>
struct TypeIndex;
template <class T, class... Ts>
struct TypeIndex<T, T, Ts...> : std::integral_constant<std::size_t, 0> {};
template <class T, class U, class... Ts>
struct TypeIndex<T, U, Ts...> : std::integral_constant<std::size_t, 1 + TypeIndex<T, Ts...>::value> {};
template <class...> class TypeList {};
template <class...> class IndexedTuple;
// A tuple of Ts, where individual members can be accessed via `t.get<I>()` for I ∈ Is.
//
// See https://github.com/mapbox/cpp/blob/master/C%2B%2B%20Structural%20Metaprogramming.md
// for motivation.
//
template <class... Is, class... Ts>
class IndexedTuple<TypeList<Is...>, TypeList<Ts...>> : public std::tuple<Ts...> {
public:
static_assert(sizeof...(Is) == sizeof...(Ts), "IndexedTuple size mismatch");
using std::tuple<Ts...>::tuple;
template <class I>
static constexpr std::size_t Index = TypeIndex<I, Is...>::value;
template <class I>
auto& get() {
return std::get<Index<I>>(*this);
}
template <class I>
const auto& get() const {
return std::get<Index<I>>(*this);
}
};
} // namespace mbgl
|