OpenKalman
owning_view.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_OWNING_VIEW_HPP
18 #define OPENKALMAN_COMPATIBILITY_VIEWS_OWNING_VIEW_HPP
19 
20 #ifndef __cpp_lib_ranges
21 
23 #include "view_interface.hpp"
24 
25 namespace OpenKalman::ranges
26 {
31  template<typename R>
32  struct owning_view : view_interface<owning_view<R>>
33  {
34  static_assert(std::is_object_v<std::remove_reference_t<R>> and std::is_move_constructible_v<std::remove_reference_t<R>> and
35  std::is_assignable_v<std::remove_reference_t<R>&, std::remove_reference_t<R>> and
37 
38 
42  template<typename aR = R, std::enable_if_t<std::is_default_constructible_v<aR>, int> = 0>
43  constexpr
45 
46 
50  constexpr
51  owning_view(owning_view&& other) = default;
52 
53 
57  explicit constexpr
58  owning_view(R&& t) : my_r {std::forward<R>(t)} {}
59 
60 
64  constexpr owning_view&
65  operator=(owning_view&& other) = default;
66 
67 
71  constexpr decltype(auto)
72  base() & { return my_r; }
73 
75  constexpr decltype(auto)
76  base() const & { return my_r; }
77 
79  constexpr decltype(auto)
80  base() && noexcept { return std::move(*this).my_r; }
81 
83  constexpr decltype(auto)
84  base() const && noexcept { return std::move(*this).my_r; }
85 
86 
90  constexpr auto begin() { return ranges::begin(my_r); }
91 
93  template<bool Enable = true, std::enable_if_t<Enable and range<const R>, int> = 0>
94  constexpr auto begin() const { return ranges::begin(my_r); }
95 
96 
100  constexpr auto end() { return ranges::end(my_r); }
101 
103  template<bool Enable = true, std::enable_if_t<Enable and range<const R>, int> = 0>
104  constexpr auto end() const { return ranges::end(my_r); }
105 
106 
110  template<typename Enable = void, typename = std::void_t<Enable, decltype(ranges::empty(std::declval<R>()))>>
111  constexpr auto empty() { return ranges::empty(my_r); }
112 
114  template<typename Enable = void, typename = std::void_t<Enable, decltype(ranges::empty(std::declval<const R>()))>>
115  constexpr auto empty() const { return ranges::empty(my_r); }
116 
117 
121  template<bool Enable = true, std::enable_if_t<Enable and sized_range<R>, int> = 0>
122  constexpr auto size() noexcept { return ranges::size(my_r); }
123 
125  template<bool Enable = true, std::enable_if_t<Enable and sized_range<const R>, int> = 0>
126  constexpr auto size() const noexcept { return ranges::size(my_r); }
127 
128  private:
129 
130  R my_r;
131 
132  };
133 
134 
135  template<typename R>
136  constexpr bool enable_borrowed_range<owning_view<R>> = enable_borrowed_range<R>;
137 
138 }
139 
140 
141 
142 #endif
143 
144 #endif //OPENKALMAN_COMPATIBILITY_VIEWS_OWNING_VIEW_HPP
constexpr auto begin() const
Definition: owning_view.hpp:94
Definition: view_interface.hpp:32
constexpr owning_view()
Default constructor.
Definition: owning_view.hpp:44
constexpr auto empty() const
Definition: owning_view.hpp:115
constexpr owning_view(R &&t)
Construct from a collection.
Definition: owning_view.hpp:58
constexpr auto begin()
Definition: owning_view.hpp:90
Definition: range-access.hpp:25
constexpr bool size
T is either an index representing a size, or void which represents that there is no size...
Definition: size.hpp:32
Definition: owning_view.hpp:32
decltype(auto) constexpr base() &
Get the base object.
Definition: owning_view.hpp:72
constexpr owning_view & operator=(owning_view &&other)=default
Move assignment operator.
constexpr auto empty()
Indicates whether the view is empty.
Definition: owning_view.hpp:111
Definitions implementing features of the c++ ranges library for compatibility.
constexpr auto end() const
Definition: owning_view.hpp:104
constexpr auto size() const noexcept
Definition: owning_view.hpp:126
constexpr auto end()
Definition: owning_view.hpp:100
Whether the argument is a specialization of std::initializer_list.
Definition: global-definitions.hpp:165
constexpr auto size() noexcept
The size of the object.
Definition: owning_view.hpp:122