tuple.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_DETAIL_TUPLE_HPP_INCLUDED
5 #define LEXY_DETAIL_TUPLE_HPP_INCLUDED
6 
9 
10 namespace lexy::_detail
11 {
12 template <std::size_t Idx, typename T>
14 {
15 #if !defined(__GNUC__) || defined(__clang__)
16  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105795
18 #endif
19  T value;
20 };
21 
22 template <std::size_t Idx, typename... T>
23 struct _nth_type;
24 template <std::size_t Idx, typename H, typename... T>
25 struct _nth_type<Idx, H, T...>
26 {
27  using type = typename _nth_type<Idx - 1, T...>::type;
28 };
29 template <typename H, typename... T>
30 struct _nth_type<0, H, T...>
31 {
32  using type = H;
33 };
34 
35 template <typename T>
37 {
38  using type = T&;
39 };
40 template <typename T>
41 struct _tuple_get_type<T&&>
42 {
43  using type = T&&;
44 };
45 
46 template <typename Indices, typename... T>
47 class _tuple;
48 template <std::size_t... Idx, typename... T>
49 class _tuple<index_sequence<Idx...>, T...> : public _tuple_holder<Idx, T>...
50 {
51 public:
52  constexpr _tuple() = default;
53 
54  template <typename... Args>
55  constexpr _tuple(Args&&... args) : _tuple_holder<Idx, T>{LEXY_FWD(args)}...
56  {}
57 };
58 
59 template <typename... T>
60 struct tuple : _tuple<index_sequence_for<T...>, T...>
61 {
62  constexpr tuple() = default;
63 
64  template <typename... Args>
65  constexpr explicit tuple(Args&&... args)
66  : _tuple<index_sequence_for<T...>, T...>(LEXY_FWD(args)...)
67  {}
68 
69  template <std::size_t N>
70  using element_type = typename _nth_type<N, T...>::type;
71 
72  template <std::size_t N>
73  constexpr decltype(auto) get() noexcept
74  {
75  // NOLINTNEXTLINE: this is fine.
76  auto&& holder = static_cast<_tuple_holder<N, element_type<N>>&>(*this);
77  // NOLINTNEXTLINE
78  return static_cast<typename _tuple_get_type<element_type<N>>::type>(holder.value);
79  }
80  template <std::size_t N>
81  constexpr decltype(auto) get() const noexcept
82  {
83  // NOLINTNEXTLINE: this is fine.
84  auto&& holder = static_cast<const _tuple_holder<N, element_type<N>>&>(*this);
85  // NOLINTNEXTLINE
86  return static_cast<typename _tuple_get_type<const element_type<N>>::type>(holder.value);
87  }
88 
89  static constexpr auto index_sequence()
90  {
91  return index_sequence_for<T...>{};
92  }
93 };
94 template <>
95 struct tuple<>
96 {
97  constexpr tuple() = default;
98 
99  static constexpr auto index_sequence()
100  {
101  return index_sequence_for<>{};
102  }
103 };
104 
105 template <typename... Args>
106 constexpr auto make_tuple(Args&&... args)
107 {
108  return tuple<std::decay_t<Args>...>(LEXY_FWD(args)...);
109 }
110 
111 template <typename... Args>
112 constexpr auto forward_as_tuple(Args&&... args)
113 {
114  return tuple<Args&&...>(LEXY_FWD(args)...);
115 }
116 } // namespace lexy::_detail
117 
118 #endif // LEXY_DETAIL_TUPLE_HPP_INCLUDED
119 
lexy::_detail::forward_as_tuple
constexpr auto forward_as_tuple(Args &&... args)
Definition: tuple.hpp:112
lexy::_detail::index_sequence_for
make_index_sequence< sizeof...(T)> index_sequence_for
Definition: integer_sequence.hpp:60
lexy::_detail::tuple<>::index_sequence
static constexpr auto index_sequence()
Definition: tuple.hpp:99
config.hpp
lexy::_detail::_tuple_get_type< T && >::type
T && type
Definition: tuple.hpp:43
lexy::_detail::tuple::get
constexpr decltype(auto) get() noexcept
Definition: tuple.hpp:73
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy::_detail::_tuple_get_type::type
T & type
Definition: tuple.hpp:38
lexy::_detail::_tuple_holder
Definition: tuple.hpp:13
lexy::_detail::integer_sequence
Definition: integer_sequence.hpp:12
lexy::_detail::_nth_type< Idx, H, T... >::type
typename _nth_type< Idx - 1, T... >::type type
Definition: tuple.hpp:27
lexy::_detail::tuple< rp< R >... >::element_type
typename _nth_type< N, T... >::type element_type
Definition: tuple.hpp:70
lexy::_detail::tuple
Definition: tuple.hpp:60
lexy::_detail::make_tuple
constexpr auto make_tuple(Args &&... args)
Definition: tuple.hpp:106
lexy::_detail::_tuple
Definition: tuple.hpp:47
integer_sequence.hpp
lexy::_detail
Definition: any_ref.hpp:12
lexy::_detail::_tuple_get_type
Definition: tuple.hpp:36
lexy::_detail::_tuple< index_sequence< Idx... >, T... >::_tuple
constexpr _tuple(Args &&... args)
Definition: tuple.hpp:55
lexy::_detail::_nth_type< 0, H, T... >::type
H type
Definition: tuple.hpp:32
lexy::_detail::_tuple_holder::value
LEXY_EMPTY_MEMBER T value
Definition: tuple.hpp:19
lexy::_detail::tuple::index_sequence
static constexpr auto index_sequence()
Definition: tuple.hpp:89
LEXY_EMPTY_MEMBER
#define LEXY_EMPTY_MEMBER
Definition: config.hpp:193
lexy::_detail::tuple::tuple
constexpr tuple()=default
lexy::_detail::tuple::tuple
constexpr tuple(Args &&... args)
Definition: tuple.hpp:65
lexy::_detail::_nth_type
Definition: tuple.hpp:23


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17