OpenKalman
ref_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_REF_VIEW_HPP
18 #define OPENKALMAN_COMPATIBILITY_VIEWS_REF_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 ref_view : view_interface<ref_view<R>>
33  {
34  private:
35 
36  static_assert(std::is_object_v<R>);
37  static void FUN(R&);
38  static void FUN(R&&) = delete;
39 
40  public:
41 
45  template<typename T, std::enable_if_t<std::is_convertible_v<T, R&> and (not std::is_same_v<remove_cvref_t<T>, ref_view>), int> = 0,
46  typename = std::void_t<decltype(FUN(std::declval<T>()))>>
47  constexpr
48  ref_view(T&& t) : r_ {std::addressof(static_cast<R&>(std::forward<T>(t)))} {}
49 
50 
54  constexpr R&
55  base() const { return *r_; }
56 
57 
61  constexpr ranges::iterator_t<R> begin() const { return ranges::begin(*r_); }
62 
63 
67  constexpr ranges::sentinel_t<R> end() const { return ranges::end(*r_); }
68 
69 
73  constexpr bool empty() const { return ranges::empty(*r_); }
74 
75 
79  template<bool Enable = true, std::enable_if_t<Enable and sized_range<R>, int> = 0>
80  constexpr auto size() const { return ranges::size(*r_); }
81 
82  private:
83 
84  R* r_;
85 
86  };
87 
88 
92  template<typename R>
93  ref_view(R&) -> ref_view<R>;
94 
95 
96  template<typename R>
97  constexpr bool enable_borrowed_range<ref_view<R>> = true;
98 
99 }
100 
101 
102 #endif
103 #endif //OPENKALMAN_COMPATIBILITY_VIEWS_REF_VIEW_HPP
Definition: view_interface.hpp:32
Definition: range-access.hpp:25
constexpr ranges::iterator_t< R > begin() const
Definition: ref_view.hpp:61
constexpr ref_view(T &&t)
Construct from a range.
Definition: ref_view.hpp:48
constexpr bool empty() const
Indicates whether the view is empty.
Definition: ref_view.hpp:73
Definition: ref_view.hpp:32
constexpr ranges::sentinel_t< R > end() const
Definition: ref_view.hpp:67
constexpr bool size
T is either an index representing a size, or void which represents that there is no size...
Definition: size.hpp:32
constexpr R & base() const
Definition: ref_view.hpp:55
constexpr auto size() const
The size of the object.
Definition: ref_view.hpp:80
Definitions implementing features of the c++ ranges library for compatibility.