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 
21 #include "view-concepts.hpp"
22 #include "view_interface.hpp"
23 
25 {
26 #ifdef __cpp_lib_ranges
27  using std::ranges::single_view;
28  namespace views
29  {
30  using std::ranges::views::single;
31  }
32 #else
33 
37 #ifdef __cpp_concepts
38  template<std::move_constructible T> requires std::is_object_v<T>
39 #else
40  template<typename T>
41 #endif
42  struct single_view : view_interface<single_view<T>>
43  {
44  static_assert(stdex::move_constructible<T> and std::is_object_v<T>);
45 
46 
50 #ifdef __cpp_concepts
51  constexpr
52  single_view() requires std::default_initializable<T> = default;
53 #else
54  template<bool Enable = true, std::enable_if_t<Enable and stdex::default_initializable<T>, int> = 0>
55  constexpr
57 #endif
58 
59 
63 #ifdef __cpp_concepts
64  explicit constexpr
65  single_view(const T& t) requires std::copy_constructible<T> : value_ {t} {}
66  requires std::copy_constructible<T>
67 #else
68  template<bool Enable = true, std::enable_if_t<stdex::copy_constructible<T>, int> = 0>
69  explicit constexpr
70  single_view(const T& t) : value_ {t} {}
71 #endif
72 
73 
77  explicit constexpr
78  single_view(T&& t) : value_ {std::move(t)} {}
79 
80 
84  constexpr T*
85  begin() noexcept { return data(); }
86 
88  constexpr const T*
89  begin() const noexcept { return data(); }
90 
91 
95  constexpr T*
96  end() noexcept { return data() + 1_uz; }
97 
99  constexpr const T*
100  end() const noexcept { return data() + 1_uz; }
101 
102 
106  static constexpr auto
107  empty() noexcept { return false; }
108 
109 
113  static constexpr std::size_t
114  size() noexcept { return 1_uz; }
115 
116 
120  constexpr T*
121  data() noexcept { return std::addressof(*value_); }
122 
123 
127  constexpr const T*
128  data() const noexcept { return std::addressof(*value_); }
129 
130  private:
131 
133 
134  };
135 
136 
140  template<typename Arg>
142 
143 
144 #ifdef __cpp_impl_three_way_comparison
145  template<typename T>
146  constexpr std::partial_ordering
147  operator<=>(const single_view<T>& lhs, const T& rhs) noexcept
148  {
149  return get(lhs, std::integral_constant<std::size_t, 0>{}) <=> rhs;
150  }
151 
152  template<typename T>
153  constexpr bool
154  operator==(const single_view<T>& lhs, const T& rhs) noexcept
155  {
156  return stdex::is_eq(operator<=>(lhs, rhs));
157  }
158 #else
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  template<typename T>
169  constexpr bool operator>(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) > rhs; }
170 
171  template<typename T>
172  constexpr bool operator<=(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) <= rhs; }
173 
174  template<typename T>
175  constexpr bool operator>=(const single_view<T>& lhs, const T& rhs) noexcept { return get(lhs, std::integral_constant<std::size_t, 0>{}) >= rhs; }
176 
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  template<typename T>
188  constexpr bool operator>(const T& lhs, const single_view<T>& rhs) noexcept { return lhs > get(rhs, std::integral_constant<std::size_t, 0>{}); }
189 
190  template<typename T>
191  constexpr bool operator<=(const T& lhs, const single_view<T>& rhs) noexcept { return lhs <= get(rhs, std::integral_constant<std::size_t, 0>{}); }
192 
193  template<typename T>
194  constexpr bool operator>=(const T& lhs, const single_view<T>& rhs) noexcept { return lhs >= get(rhs, std::integral_constant<std::size_t, 0>{}); }
195 
196 #endif
197 
198 
199  namespace views
200  {
201  namespace detail
202  {
203  struct single_impl
204  {
205  template<typename T>
206  constexpr auto
207  operator() [[nodiscard]] (T&& t) const { return single_view<std::decay_t<T>> {std::forward<T>(t)}; }
208  };
209  }
210 
211 
217  inline constexpr detail::single_impl single;
218 
219  }
220 
221 #endif
222 }
223 
224 #endif
static constexpr auto empty() noexcept
Definition: single.hpp:107
constexpr T * end() noexcept
Equivalent to data() + 1;.
Definition: single.hpp:96
constexpr detail::single_impl single
Equivalent to std::ranges::views::single.
Definition: single.hpp:217
Definition: view_interface.hpp:32
constexpr const T * end() const noexcept
Definition: single.hpp:100
constexpr T * data() noexcept
A pointer to the contained value.
Definition: single.hpp:121
constexpr T * begin() noexcept
Equivalent to data();.
Definition: single.hpp:85
static constexpr std::size_t size() noexcept
The size of the resulting object (which is always 1)
Definition: single.hpp:114
Definition: common.hpp:200
constexpr single_view()
Default constructor.
Definition: single.hpp:56
constexpr single_view(T &&t)
Construct from an object convertible to type T.
Definition: single.hpp:78
constexpr const T * begin() const noexcept
Definition: single.hpp:89
Equivalent to std::ranges::single_view.
Definition: single.hpp:42
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:128
constexpr single_view(const T &t)
Construct from an object convertible to type T.
Definition: single.hpp:70