2021-05-09 06:54:22 +03:00
|
|
|
/*
|
|
|
|
* 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> {
|
2022-10-17 01:06:11 +03:00
|
|
|
Tuple(T&& value)
|
|
|
|
requires(!IsSame < T &&, T const& >)
|
2021-05-09 06:54:22 +03:00
|
|
|
: value(forward<T>(value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-17 01:06:11 +03:00
|
|
|
Tuple(T const& value)
|
2021-05-09 06:54:22 +03:00
|
|
|
: value(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U& get()
|
|
|
|
{
|
|
|
|
static_assert(IsSame<T, U>, "Invalid tuple access");
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
U const& get() const
|
2021-05-09 06:54:22 +03:00
|
|
|
{
|
|
|
|
return const_cast<Tuple<T>&>(*this).get<U>();
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<typename U, size_t index>
|
2021-05-09 06:54:22 +03:00
|
|
|
U& get_with_index()
|
|
|
|
{
|
|
|
|
static_assert(IsSame<T, U> && index == 0, "Invalid tuple access");
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<typename U, size_t index>
|
2022-10-17 01:06:11 +03:00
|
|
|
U const& get_with_index() const
|
2021-05-09 06:54:22 +03:00
|
|
|
{
|
|
|
|
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...> {
|
2021-07-31 14:32:00 +03:00
|
|
|
|
|
|
|
template<typename FirstT, typename... RestT>
|
|
|
|
Tuple(FirstT&& first, RestT&&... rest)
|
|
|
|
: Tuple<TRest...>(forward<RestT>(rest)...)
|
|
|
|
, value(forward<FirstT>(first))
|
2021-05-09 06:54:22 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-31 14:32:00 +03:00
|
|
|
Tuple(T&& first, TRest&&... rest)
|
|
|
|
: Tuple<TRest...>(move(rest)...)
|
|
|
|
, value(move(first))
|
2021-05-09 06:54:22 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U& get()
|
|
|
|
{
|
|
|
|
if constexpr (IsSame<T, U>)
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
return Tuple<TRest...>::template get<U>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2022-10-17 01:06:11 +03:00
|
|
|
U const& get() const
|
2021-05-09 06:54:22 +03:00
|
|
|
{
|
|
|
|
return const_cast<Tuple<T, TRest...>&>(*this).get<U>();
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<typename U, size_t index>
|
2021-05-09 06:54:22 +03:00
|
|
|
U& get_with_index()
|
|
|
|
{
|
|
|
|
if constexpr (IsSame<T, U> && index == 0)
|
|
|
|
return value;
|
|
|
|
else
|
|
|
|
return Tuple<TRest...>::template get_with_index<U, index - 1>();
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<typename U, size_t index>
|
2022-10-17 01:06:11 +03:00
|
|
|
U const& get_with_index() const
|
2021-05-09 06:54:22 +03:00
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Tuple(Tuple const& other)
|
2021-05-09 06:54:22 +03:00
|
|
|
: Tuple(other, Indices())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Tuple& operator=(Tuple&& other)
|
|
|
|
{
|
|
|
|
set(move(other), Indices());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Tuple& operator=(Tuple const& other)
|
2021-05-09 06:54:22 +03:00
|
|
|
{
|
|
|
|
set(other, Indices());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
auto& get()
|
|
|
|
{
|
|
|
|
return Detail::Tuple<Ts...>::template get<T>();
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<size_t index>
|
2021-05-09 06:54:22 +03:00
|
|
|
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>();
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<size_t index>
|
2021-05-09 06:54:22 +03:00
|
|
|
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:
|
2024-02-09 02:52:03 +03:00
|
|
|
template<size_t... Is>
|
2021-05-09 06:54:22 +03:00
|
|
|
Tuple(Tuple&& other, IndexSequence<Is...>)
|
|
|
|
: Detail::Tuple<Ts...>(move(other.get<Is>())...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<size_t... Is>
|
2022-04-01 20:58:27 +03:00
|
|
|
Tuple(Tuple const& other, IndexSequence<Is...>)
|
2021-05-09 06:54:22 +03:00
|
|
|
: Detail::Tuple<Ts...>(other.get<Is>()...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<size_t... Is>
|
2021-05-09 06:54:22 +03:00
|
|
|
void set(Tuple&& other, IndexSequence<Is...>)
|
|
|
|
{
|
|
|
|
((get<Is>() = move(other.get<Is>())), ...);
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<size_t... Is>
|
2022-04-01 20:58:27 +03:00
|
|
|
void set(Tuple const& other, IndexSequence<Is...>)
|
2021-05-09 06:54:22 +03:00
|
|
|
{
|
|
|
|
((get<Is>() = other.get<Is>()), ...);
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<typename F, size_t... Is>
|
2021-05-09 06:54:22 +03:00
|
|
|
auto apply_as_args(F&& f, IndexSequence<Is...>)
|
|
|
|
{
|
|
|
|
return forward<F>(f)(get<Is>()...);
|
|
|
|
}
|
|
|
|
|
2024-02-09 02:52:03 +03:00
|
|
|
template<typename F, size_t... Is>
|
2021-05-09 06:54:22 +03:00
|
|
|
auto apply_as_args(F&& f, IndexSequence<Is...>) const
|
|
|
|
{
|
|
|
|
return forward<F>(f)(get<Is>()...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-18 17:14:03 +03:00
|
|
|
template<class... Args>
|
|
|
|
Tuple(Args... args) -> Tuple<Args...>;
|
|
|
|
|
2021-05-09 06:54:22 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-05-09 06:54:22 +03:00
|
|
|
using AK::Tuple;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|