OpenKalman
single.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_COMPATIBILITY_VIEWS_SINGLE_HPP
18 #define OPENKALMAN_COMPATIBILITY_VIEWS_SINGLE_HPP
19 
20 #ifndef __cpp_lib_ranges
21 
22 #include "view-concepts.hpp"
23 #include "view_interface.hpp"
24 
25 namespace OpenKalman::ranges
26 {
31 #ifdef __cpp_concepts
32  template<std::move_constructible T> requires std::is_object_v<T>
33 #else
34  template<typename T>
35 #endif
36  struct single_view : view_interface<single_view<T>>
37  {
41 #ifdef __cpp_concepts
42  constexpr
43  single_view() requires std::default_initializable<T> = default;
44 #else
45  template<typename aT = T, std::enable_if_t<std::is_default_constructible_v<aT>, int> = 0>
46  constexpr
48 #endif
49 
50 
54 #ifdef __cpp_concepts
55  explicit constexpr
56  single_view(const T& t) requires std::copy_constructible<T> : t_ {t} {}
57  requires std::copy_constructible<T>
58 #else
59  template<typename aT = T, std::enable_if_t<std::is_copy_constructible_v<aT>, int> = 0>
60  explicit constexpr
61  single_view(const T& t) : t_ {t} {}
62 #endif
63 
64 
68  explicit constexpr
69  single_view(T&& t) : t_ {std::move(t)} {}
70 
71 
75  constexpr T*
76  begin() noexcept { return data(); }
77 
79  constexpr const T*
80  begin() const noexcept { return data(); }
81 
82 
86  constexpr T*
87  end() noexcept { return data() + 1_uz; }
88 
90  constexpr const T*
91  end() const noexcept { return data() + 1_uz; }
92 
93 
97  static constexpr auto
98  empty() noexcept { return false; }
99 
100 
104  static constexpr std::size_t
105  size() noexcept { return 1_uz; }
106 
107 
111  constexpr T*
112  data() noexcept { return std::addressof(std::get<0>(t_)); }
113 
114 
118  constexpr const T*
119  data() const noexcept { return std::addressof(std::get<0>(t_)); }
120 
121  private:
122 
123  std::tuple<T> t_;
124 
125  };
126 
127 
131  template<typename Arg>
133 
134 
135 #ifdef __cpp_impl_three_way_comparison
136  template<typename T>
137  constexpr std::partial_ordering
138  operator<=>(const single_view<T>& lhs, const T& rhs) noexcept
139  {
140  return get(lhs, std::integral_constant<std::size_t, 0>{}) <=> rhs;
141  }
142 
143  template<typename T>
144  constexpr bool
145  operator==(const single_view<T>& lhs, const T& rhs) noexcept
146  {
147  return std::is_eq(operator<=>(lhs, rhs));
148  }
149 #else
150  template<typename T>
151  constexpr bool operator==(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) == rhs; }
152 
153  template<typename T>
154  constexpr bool operator!=(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) != rhs; }
155 
156  template<typename T>
157  constexpr bool operator<(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) < rhs; }
158 
159  template<typename T>
160  constexpr bool operator>(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) > rhs; }
161 
162  template<typename T>
163  constexpr bool operator<=(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) <= rhs; }
164 
165  template<typename T>
166  constexpr bool operator>=(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) >= rhs; }
167 
168 
169  template<typename T>
170  constexpr bool operator==(const T& lhs, const single_view<T>& rhs) noexcept { return lhs == get(rhs, std::integral_constant<std::size_t, 0>{}); }
171 
172  template<typename T>
173  constexpr bool operator!=(const T& lhs, const single_view<T>& rhs) noexcept { return lhs != get(rhs, std::integral_constant<std::size_t, 0>{}); }
174 
175  template<typename T>
176  constexpr bool operator<(const T& lhs, const single_view<T>& rhs) noexcept { return lhs < get(rhs, std::integral_constant<std::size_t, 0>{}); }
177 
178  template<typename T>
179  constexpr bool operator>(const T& lhs, const single_view<T>& rhs) noexcept { return lhs > get(rhs, std::integral_constant<std::size_t, 0>{}); }
180 
181  template<typename T>
182  constexpr bool operator<=(const T& lhs, const single_view<T>& rhs) noexcept { return lhs <= get(rhs, std::integral_constant<std::size_t, 0>{}); }
183 
184  template<typename T>
185  constexpr bool operator>=(const T& lhs, const single_view<T>& rhs) noexcept { return lhs >= get(rhs, std::integral_constant<std::size_t, 0>{}); }
186 
187 #endif
188 
189 } // namespace OpenKalman
190 
191 
193 {
194  namespace detail
195  {
196  struct single_impl
197  {
198  template<typename R>
199  constexpr auto
200  operator() [[nodiscard]] (R&& r) const { return single_view<R> {std::forward<R>(r)}; }
201  };
202  }
203 
204 
210  inline constexpr detail::single_impl single;
211 
212 }
213 
214 #endif
215 
216 #endif //OPENKALMAN_COMPATIBILITY_VIEWS_SINGLE_HPP
constexpr T * data() noexcept
A pointer to the contained value.
Definition: single.hpp:112
constexpr single_view()
Default constructor.
Definition: single.hpp:47
Equivalent to std::ranges::single_view.
Definition: single.hpp:36
constexpr T * end() noexcept
Equivalent to data() + 1;.
Definition: single.hpp:87
Definition: view_interface.hpp:32
constexpr single_view(const T &t)
Construct from an object convertible to type T.
Definition: single.hpp:61
Definition: range-access.hpp:25
constexpr const T * end() const noexcept
Definition: single.hpp:91
constexpr detail::single_impl single
Equivalent to std::ranges::views::single.
Definition: single.hpp:210
constexpr const T * data() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: single.hpp:119
static constexpr std::size_t size() noexcept
The size of the resulting object (which is always 1)
Definition: single.hpp:105
Definition: all.hpp:35
constexpr T * begin() noexcept
Equivalent to data();.
Definition: single.hpp:76
static constexpr auto empty() noexcept
Definition: single.hpp:98
constexpr const T * begin() const noexcept
Definition: single.hpp:80
constexpr single_view(T &&t)
Construct from an object convertible to type T.
Definition: single.hpp:69