OpenKalman
AdapterBase.hpp
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2019-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_ADAPTERBASE_HPP
18 #define OPENKALMAN_ADAPTERBASE_HPP
19 
20 #include "linear-algebra/traits/internal/library_base.hpp"
22 
23 namespace OpenKalman::internal
24 {
32 #ifdef __cpp_concepts
33  template<typename Derived, indexible Nested, indexible LibraryObject = Nested> requires
34  (not std::is_rvalue_reference_v<Nested>)
35 #else
36  template<typename Derived, typename Nested, typename LibraryObject = Nested>
37 #endif
38  struct AdapterBase : library_base_t<Derived, LibraryObject>
39  {
40 
41 #ifndef __cpp_concepts
42  static_assert(indexible<Nested>);
43  static_assert(indexible<LibraryObject>);
44  static_assert(not std::is_rvalue_reference_v<Nested>);
45 #endif
46 
50  constexpr
51  AdapterBase() = default;
52 
53 
57 #ifdef __cpp_concepts
58  template<typename Arg> requires
59  (not std::is_base_of_v<Derived, std::decay_t<Arg>>) and
60  std::constructible_from<Nested, Arg&&>
61 #else
62  template<typename Arg, std::enable_if_t<
63  (not std::is_base_of_v<Derived, std::decay_t<Arg>>) and
64  stdex::constructible_from<Nested, Arg&&>, int> = 0>
65 #endif
66  constexpr explicit
67  AdapterBase(Arg&& arg) : nested_ {std::forward<Arg>(arg)} {}
68 
69 
73 #ifdef __cpp_explicit_this_parameter
74  template<typename Self>
75  constexpr decltype(auto) nested_object(this Self&& self) { return std::forward<Self>(self).nested_; }
76 #else
77  constexpr Nested& nested_object() & { return nested_; }
78 
80  constexpr const Nested& nested_object() const & { return nested_; }
81 
83  constexpr Nested&& nested_object() && { return std::move(*this).nested_; }
84 
86  constexpr const Nested&& nested_object() const && { return std::move(*this).nested_; }
87 #endif
88 
89 
93 #ifdef __cpp_explicit_this_parameter
94  template<typename Self, typename...I> requires
95  requires(Self&& s, I&&...i) { access(std::forward<Self>(s), std::forward<I>(i)...); }
96  constexpr values::value decltype(auto)
97  operator[](this Self&& self, I&&...i)
98  {
99  return access(std::forward<Self>(self), std::forward<I>(i)...);
100  }
101 #else
102  template<typename...I, std::enable_if_t<
103  values::value<decltype(access(std::declval<Derived&>(), std::declval<I>()...))>, int> = 0>
104  constexpr decltype(auto) operator[](I&&...i) &
105  {
106  return access(static_cast<Derived&>(*this), std::forward<I>(i)...);
107  }
108 
110  template<typename...I, std::enable_if_t<
111  values::value<decltype(access(std::declval<const Derived&>(), std::declval<I>()...))>, int> = 0>
112  constexpr decltype(auto) operator[](I&&...i) const &
113  {
114  return access(static_cast<const Derived&>(*this), std::forward<I>(i)...);
115  }
116 
118  template<typename...I, std::enable_if_t<
119  values::value<decltype(access(std::declval<Derived&&>(), std::declval<I>()...))>, int> = 0>
120  constexpr decltype(auto) operator[](I&&...i) &&
121  {
122  return access(static_cast<Derived&&>(*this), std::forward<I>(i)...);
123  }
124 
126  template<typename...I, std::enable_if_t<
127  values::value<decltype(access(std::declval<const Derived&&>(), std::declval<I>()...))>, int> = 0>
128  constexpr decltype(auto) operator[](I&&...i) const &&
129  {
130  return access(static_cast<const Derived&&>(*this), std::forward<I>(i)...);
131  }
132 #endif
133 
134  private:
135 
136  Nested nested_;
137 
138  };
139 
140 }
141 
142 #endif
constexpr bool value
T is a fixed or dynamic value that is reducible to a number.
Definition: value.hpp:45
Definition: AdapterBase.hpp:38
decltype(auto) constexpr access(Arg &&arg, const Indices &indices)
Access a component of an indexible object at a given set of indices.
Definition: access.hpp:74
decltype(auto) constexpr operator[](I &&...i) &
Access a component using a collection or pack of indices.
Definition: AdapterBase.hpp:104
Definition for access function.
constexpr AdapterBase()=default
Default constructor.
constexpr Nested & nested_object() &
Get the nested object.
Definition: AdapterBase.hpp:77
constexpr const Nested && nested_object() const &&
Definition: AdapterBase.hpp:86
decltype(auto) constexpr operator[](I &&...i) &&
Definition: AdapterBase.hpp:120
constexpr AdapterBase(Arg &&arg)
Construct from the nested type.
Definition: AdapterBase.hpp:67
constexpr Nested && nested_object() &&
Definition: AdapterBase.hpp:83
decltype(auto) constexpr operator[](I &&...i) const &&
Definition: AdapterBase.hpp:128
Definition: basics.hpp:48
constexpr const Nested & nested_object() const &
Definition: AdapterBase.hpp:80
decltype(auto) constexpr operator[](I &&...i) const &
Definition: AdapterBase.hpp:112