OpenKalman
tuple_reverse.hpp
Go to the documentation of this file.
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2025 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
17 #ifndef OPENKALMAN_TUPLE_REVERSE_HPP
18 #define OPENKALMAN_TUPLE_REVERSE_HPP
19 
20 #include <type_traits>
21 #include <tuple>
26 
28 {
33 #ifdef __cpp_concepts
34  template<uniformly_gettable T>
35 #else
36  template<typename T>
37 #endif
39  {
40  private:
41 
42  static constexpr auto base_size = size_of_v<T>;
43 
44  public:
45 
46 #ifdef __cpp_concepts
47  constexpr tuple_reverse_view() requires std::default_initializable<T> = default;
48 #else
49  template<bool Enable = true, std::enable_if_t<Enable and stdex::default_initializable<T>, int> = 0>
50  constexpr tuple_reverse_view() {};
51 #endif
52 
53 
54 #ifdef __cpp_concepts
55  template<typename Arg> requires std::constructible_from<T, Arg&&>
56 #else
57  template<typename Arg, std::enable_if_t<stdex::constructible_from<T, Arg&&>, int> = 0>
58 #endif
59  explicit constexpr tuple_reverse_view(Arg&& arg) : t {std::forward<Arg>(arg)} {}
60 
61 
65 #ifdef __cpp_explicit_this_parameter
66  template<std::size_t i>
67  constexpr decltype(auto)
68  get(this auto&& self) noexcept
69  {
70  static_assert(i < size_of_v<T>, "Index out of range.");
71  return collections::get<base_size - i - 1_uz>(std::forward<decltype(self)>(self).t);
72  }
73 #else
74  template<std::size_t i>
75  constexpr decltype(auto)
76  get() &
77  {
78  static_assert(i < size_of_v<T>, "Index out of range.");
79  return collections::get<base_size - i - 1_uz>(t);
80  }
81 
82  template<std::size_t i>
83  constexpr decltype(auto)
84  get() const &
85  {
86  static_assert(i < size_of_v<T>, "Index out of range.");
87  return collections::get<base_size - i - 1_uz>(t);
88  }
89 
90  template<std::size_t i>
91  constexpr decltype(auto)
92  get() && noexcept
93  {
94  static_assert(i < size_of_v<T>, "Index out of range.");
95  return collections::get<base_size - i - 1_uz>(std::move(*this).t);
96  }
97 
98  template<std::size_t i>
99  constexpr decltype(auto)
100  get() const && noexcept
101  {
102  static_assert(i < size_of_v<T>, "Index out of range.");
103  return collections::get<base_size - i - 1_uz>(std::move(*this).t);
104  }
105 #endif
106 
107  private:
108 
109  T t;
110  };
111 
112 
116  template<typename Arg>
118 
119 }
120 
121 
122 namespace std
123 {
124  template<typename T>
126 
127 
128  template<std::size_t i, typename T>
129  struct tuple_element<i, OpenKalman::collections::tuple_reverse_view<T>>
130  {
131  static_assert(i < OpenKalman::collections::size_of_v<T>);
133  };
134 }
135 
136 
137 namespace OpenKalman::collections
138 {
142 #ifdef __cpp_concepts
143  template<uniformly_gettable Arg>
144  constexpr tuple_like auto
145 #else
146  template<typename Arg, std::enable_if_t<uniformly_gettable<Arg>, int> = 0>
147  constexpr auto
148 #endif
149  tuple_reverse(Arg&& arg)
150  {
151  return tuple_reverse_view {std::forward<Arg>(arg)};
152  }
153 
154 
158 #ifdef __cpp_concepts
159  template<uniformly_gettable T> requires std::default_initializable<T>
160  constexpr tuple_like auto
161 #else
162  template<typename T, std::enable_if_t<uniformly_gettable<T> and stdex::default_initializable<T>, int> = 0>
163  constexpr auto
164 #endif
166  {
167  return tuple_reverse_view<T> {};
168  }
169 
170 }
171 
172 #endif
Definition for collections::tuple_like.
Namespace for collections.
Definition: collections.hpp:27
tuple_reverse_view(Arg &&) -> tuple_reverse_view< Arg >
Deduction guide.
constexpr detail_get::get_impl< i > get
A generalization of std::get, where the index is known at compile time.
Definition: get.hpp:50
Definition for collections::size_of.
The size of a sized object (including a collection).
Definition: size_of.hpp:33
A view of a tuple that reverses the order of a base tuple.
Definition: tuple_reverse.hpp:38
The root namespace for OpenKalman.
Definition: basics.hpp:34
constexpr auto tuple_reverse(Arg &&arg)
Reverses the order of a uniformly_gettable object.
Definition: tuple_reverse.hpp:149
decltype(auto) constexpr get() &
Get element i of a tuple_reverse_view.
Definition: tuple_reverse.hpp:76
typename collection_element< i, T >::type collection_element_t
Helper template for collection_element.
Definition: collection_element.hpp:116
Definition for collections::uniformly_gettable.
Definition for collections::collection_element.
constexpr bool tuple_like
T is a non-empty tuple, pair, array, or other type that acts like a tuple.
Definition: tuple_like.hpp:66