summaryrefslogtreecommitdiff
path: root/include/mbgl/util/indexed_tuple.hpp
blob: fc22d1ee3572e39558918f2d539e8461155b88fe (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include <mbgl/util/type_list.hpp>

#include <tuple>
#include <type_traits>

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 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");

    template <class I>
    auto& get() {
        return std::get<TypeIndex<I, Is...>::value>(*this);
    }

    template <class I>
    const auto& get() const {
        return std::get<TypeIndex<I, Is...>::value>(*this);
    }

    template <class... Us>
    IndexedTuple(Us&&... other) : std::tuple<Ts...>(std::forward<Us>(other)...) {};

    template <class... Js, class... Us>
    IndexedTuple<TypeList<Is..., Js...>, TypeList<Ts..., Us...>>
    concat(const IndexedTuple<TypeList<Js...>, TypeList<Us...>>& other) const {
        return IndexedTuple<TypeList<Is..., Js...>, TypeList<Ts..., Us...>> {
            get<Is>()...,
            other.template get<Js>()...
        };
    }

    // Help out MSVC++
    bool operator==(const IndexedTuple<TypeList<Is...>, TypeList<Ts...>>& other) const {
        return static_cast<const std::tuple<Ts...>&>(*this) == static_cast<const std::tuple<Ts...>&>(other);
    }

    bool operator!=(const IndexedTuple<TypeList<Is...>, TypeList<Ts...>>& other) const {
        return !(*this == other);
    }
};

template <class, class T>
using ExpandToType = T;

} // namespace mbgl