summaryrefslogtreecommitdiff
path: root/AK/Tuple.h
blob: 9bdec66f158c33c3a216c0f7718581068a43b2db (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/StdLibExtras.h>
#include <AK/TypeList.h>

namespace AK::Detail {

template<typename... Ts>
struct Tuple {
};

template<typename T>
struct Tuple<T> {
    Tuple(T&& value) requires(!IsSame<T&&, const T&>)
        : value(forward<T>(value))
    {
    }

    Tuple(const T& value)
        : value(value)
    {
    }

    template<typename U>
    U& get()
    {
        static_assert(IsSame<T, U>, "Invalid tuple access");
        return value;
    }

    template<typename U>
    const U& get() const
    {
        return const_cast<Tuple<T>&>(*this).get<U>();
    }

    template<typename U, unsigned index>
    U& get_with_index()
    {
        static_assert(IsSame<T, U> && index == 0, "Invalid tuple access");
        return value;
    }

    template<typename U, unsigned index>
    const U& get_with_index() const
    {
        return const_cast<Tuple<T>&>(*this).get_with_index<U, index>();
    }

private:
    T value;
};

template<typename T, typename... TRest>
struct Tuple<T, TRest...> : Tuple<TRest...> {

    template<typename FirstT, typename... RestT>
    Tuple(FirstT&& first, RestT&&... rest)
        : Tuple<TRest...>(forward<RestT>(rest)...)
        , value(forward<FirstT>(first))
    {
    }

    Tuple(T&& first, TRest&&... rest)
        : Tuple<TRest...>(move(rest)...)
        , value(move(first))
    {
    }

    template<typename U>
    U& get()
    {
        if constexpr (IsSame<T, U>)
            return value;
        else
            return Tuple<TRest...>::template get<U>();
    }

    template<typename U>
    const U& get() const
    {
        return const_cast<Tuple<T, TRest...>&>(*this).get<U>();
    }

    template<typename U, unsigned index>
    U& get_with_index()
    {
        if constexpr (IsSame<T, U> && index == 0)
            return value;
        else
            return Tuple<TRest...>::template get_with_index<U, index - 1>();
    }

    template<typename U, unsigned index>
    const U& get_with_index() const
    {
        return const_cast<Tuple<T, TRest...>&>(*this).get_with_index<U, index>();
    }

private:
    T value;
};

}

namespace AK {

template<typename... Ts>
struct Tuple : Detail::Tuple<Ts...> {
    using Types = TypeList<Ts...>;
    using Detail::Tuple<Ts...>::Tuple;
    using Indices = MakeIndexSequence<sizeof...(Ts)>;

    Tuple(Tuple&& other)
        : Tuple(move(other), Indices())
    {
    }

    Tuple(const Tuple& other)
        : Tuple(other, Indices())
    {
    }

    Tuple& operator=(Tuple&& other)
    {
        set(move(other), Indices());
        return *this;
    }

    Tuple& operator=(const Tuple& other)
    {
        set(other, Indices());
        return *this;
    }

    template<typename T>
    auto& get()
    {
        return Detail::Tuple<Ts...>::template get<T>();
    }

    template<unsigned index>
    auto& get()
    {
        return Detail::Tuple<Ts...>::template get_with_index<typename Types::template Type<index>, index>();
    }

    template<typename T>
    auto& get() const
    {
        return Detail::Tuple<Ts...>::template get<T>();
    }

    template<unsigned index>
    auto& get() const
    {
        return Detail::Tuple<Ts...>::template get_with_index<typename Types::template Type<index>, index>();
    }

    template<typename F>
    auto apply_as_args(F&& f)
    {
        return apply_as_args(forward<F>(f), Indices());
    }

    template<typename F>
    auto apply_as_args(F&& f) const
    {
        return apply_as_args(forward<F>(f), Indices());
    }

    static constexpr auto size() { return sizeof...(Ts); }

private:
    template<unsigned... Is>
    Tuple(Tuple&& other, IndexSequence<Is...>)
        : Detail::Tuple<Ts...>(move(other.get<Is>())...)
    {
    }

    template<unsigned... Is>
    Tuple(const Tuple& other, IndexSequence<Is...>)
        : Detail::Tuple<Ts...>(other.get<Is>()...)
    {
    }

    template<unsigned... Is>
    void set(Tuple&& other, IndexSequence<Is...>)
    {
        ((get<Is>() = move(other.get<Is>())), ...);
    }

    template<unsigned... Is>
    void set(const Tuple& other, IndexSequence<Is...>)
    {
        ((get<Is>() = other.get<Is>()), ...);
    }

    template<typename F, unsigned... Is>
    auto apply_as_args(F&& f, IndexSequence<Is...>)
    {
        return forward<F>(f)(get<Is>()...);
    }

    template<typename F, unsigned... Is>
    auto apply_as_args(F&& f, IndexSequence<Is...>) const
    {
        return forward<F>(f)(get<Is>()...);
    }
};

template<class... Args>
Tuple(Args... args) -> Tuple<Args...>;

}

using AK::Tuple;