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 
21 #include "view_interface.hpp"
22 
24 {
25 #ifdef __cpp_lib_ranges
26  using std::ranges::ref_view;
27 #else
28 
32  template<typename R>
33  struct ref_view : view_interface<ref_view<R>>
34  {
35  private:
36 
37  static_assert(std::is_object_v<R>);
38  static void FUN(R&);
39  static void FUN(R&&) = delete;
40 
41  public:
42 
46  template<typename T, std::enable_if_t<stdex::convertible_to<T, R&> and (not std::is_same_v<stdex::remove_cvref_t<T>, ref_view>), int> = 0,
47  typename = std::void_t<decltype(FUN(std::declval<T>()))>>
48  constexpr
49  ref_view(T&& t) : r_ {std::addressof(static_cast<R&>(std::forward<T>(t)))} {}
50 
51 
55  constexpr R&
56  base() const { return *r_; }
57 
58 
62  constexpr stdex::ranges::iterator_t<R> begin() const { return stdex::ranges::begin(*r_); }
63 
64 
68  constexpr stdex::ranges::sentinel_t<R> end() const { return stdex::ranges::end(*r_); }
69 
70 
74  constexpr bool empty() const { return stdex::ranges::empty(*r_); }
75 
76 
80  template<bool Enable = true, std::enable_if_t<Enable and sized_range<R>, int> = 0>
81  constexpr auto size() const { return stdex::ranges::size(*r_); }
82 
83  private:
84 
85  R* r_;
86 
87  };
88 
89 
93  template<typename R>
94  ref_view(R&) -> ref_view<R>;
95 
96 
97  template<typename R>
98  constexpr bool enable_borrowed_range<ref_view<R>> = true;
99 
100 #endif
101 }
102 
103 #endif
constexpr bool empty() const
Indicates whether the view is empty.
Definition: ref_view.hpp:74
constexpr stdex::ranges::iterator_t< R > begin() const
Definition: ref_view.hpp:62
constexpr R & base() const
Definition: ref_view.hpp:56
Definition: view_interface.hpp:32
Definitions relating to the availability of c++ language features.
Definition: common.hpp:200
constexpr bool size
T is either an index representing a size, or unbounded_size_t, which indicates that the size is unbou...
Definition: size.hpp:65
Definition: ref_view.hpp:33
constexpr ref_view(T &&t)
Construct from a range.
Definition: ref_view.hpp:49
constexpr auto size() const
The size of the object.
Definition: ref_view.hpp:81
constexpr stdex::ranges::sentinel_t< R > end() const
Definition: ref_view.hpp:68