repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
tuple.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <type_traits>
4#include <utility>
5#include <librepr/util/util.h>
6#include "list.h"
7
8namespace librepr {
9
10namespace detail {
11template <int Idx, typename T>
12struct TupleLeaf {
14 constexpr T& operator[](Constant<Idx>) { return value; }
15};
16
17template <typename... Ts>
18struct TupleImpl;
19
20template <int... Idx, typename... Ts>
21struct TupleImpl<std::integer_sequence<int, Idx...>, Ts...> : TupleLeaf<Idx, Ts>... {
22 using TupleLeaf<Idx, Ts>::operator[]...;
23};
24
25template <int V, typename T>
26constexpr T& get_impl(TupleLeaf<V, T>& node) noexcept {
27 return node.value;
28}
29
30template <int V, typename T>
31constexpr T const& get_impl(TupleLeaf<V, T> const& node) noexcept {
32 return node.value;
33}
34} // namespace detail
35
36template <typename... Ts>
37struct Tuple : detail::TupleImpl<std::make_integer_sequence<int, sizeof...(Ts)>, Ts...> {};
38
39template <std::size_t Idx, typename... Ts>
40constexpr decltype(auto) get(Tuple<Ts...> const& tuple) noexcept {
41 return detail::get_impl<Idx>(tuple);
42}
43
44template <std::size_t Idx, typename... Ts>
45constexpr decltype(auto) get(Tuple<Ts...>& tuple) noexcept {
46 return detail::get_impl<Idx>(tuple);
47}
48} // namespace librepr
49
50template <std::size_t I, typename... Ts>
51struct std::tuple_element<I, librepr::Tuple<Ts...>> {
52 using type = typename librepr::TypeList<Ts...>::template get<I>;
53};
54
55template <typename... Ts>
56struct std::tuple_size<librepr::Tuple<Ts...>> : std::integral_constant<std::size_t, sizeof...(Ts)> {};
constexpr T & get_impl(TupleLeaf< V, T > &node) noexcept
Definition tuple.h:26
Definition ctvi/ctvi.h:9
std::string code_for()
Definition repr:39
constexpr decltype(auto) get(RefTuple< Ts... > const &tuple) noexcept
Definition reftuple.h:59
STL namespace.
Definition util/util.h:15
Definition tuple.h:37
Definition list.h:64
Definition tuple.h:18
Definition tuple.h:12
T value
Definition tuple.h:13
constexpr T & operator[](Constant< Idx >)
Definition tuple.h:14