OpenKalman
pattern_adapter.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) 2024-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 
16 #ifndef OPENKALMAN_PATTERN_ADAPTER_HPP
17 #define OPENKALMAN_PATTERN_ADAPTER_HPP
18 
19 #include "patterns/patterns.hpp"
22 #include "linear-algebra/adapters/internal/AdapterBase.hpp"
23 #include "linear-algebra/adapters/interfaces/pass_through_interface.hpp"
24 
25 namespace OpenKalman
26 {
32 #ifdef __cpp_concepts
33  template<indexible Nested, pattern_collection_for<Nested> PatternCollection>
34  requires std::same_as<PatternCollection, std::decay_t<PatternCollection>>
35 #else
36  template<typename Nested, typename PatternCollection>
37 #endif
38  struct pattern_adapter : internal::AdapterBase<pattern_adapter<Nested, PatternCollection>, Nested>
39  {
40  private:
41 
42 #ifndef __cpp_concepts
43  static_assert(pattern_collection_for<PatternCollection, Nested>);
44 #endif
45 
47 
48  public:
49 
53 #ifdef __cpp_concepts
54  template<indexible Arg, patterns::pattern_collection P> requires
55  std::constructible_from<Nested, Arg&&> and
56  std::constructible_from<PatternCollection, P&&>
57 #else
58  template<typename Arg, typename P, std::enable_if_t<
59  stdex::constructible_from<Nested, Arg&&> and
60  stdex::constructible_from<PatternCollection, P&&>, int> = 0>
61 #endif
62  constexpr
63  pattern_adapter(Arg&& arg, P&& p) : Base {std::forward<Arg>(arg)}, patt_ {std::forward<P>(p)}
64  {
65  if constexpr(not pattern_collection_for<PatternCollection, Nested, applicability::guaranteed>)
66  {
67  // Note: this could be expensive in some circumstances:
69  }
70  }
71 
72 
76  constexpr pattern_adapter() = default;
77 
78 
82 #ifdef __cpp_explicit_this_parameter
83  template<typename Self>
84  constexpr decltype(auto) pattern_collection(this Self&& self)
85  {
86  return std::forward<Self>(self).patt_;
87  }
88 #else
89  constexpr PatternCollection& pattern_collection() & { return patt_; }
90 
92  constexpr const PatternCollection& pattern_collection() const & { return patt_; }
93 
95  constexpr PatternCollection&& pattern_collection() && { return std::move(*this).patt_; }
96 
98  constexpr const PatternCollection&& pattern_collection() const && { return std::move(*this).patt_; }
99 #endif
100 
101  private:
102 
103  PatternCollection patt_;
104 
105  };
106 
107 
111 #ifdef __cpp_concepts
112  template<indexible Arg, pattern_collection_for<Arg> P>
113 #else
114  template<typename Arg, typename P, std::enable_if_t<indexible<Arg> and pattern_collection_for<P, Arg>, int> = 0>
115 #endif
117 
118 
120  // interface //
122 
123  namespace interface
124  {
128  template<typename Nested, typename PatternCollection>
129  struct object_traits<pattern_adapter<Nested, PatternCollection>>
130  : pass_through_object_traits<pattern_adapter<Nested, PatternCollection>, Nested>
131  {
132  static const bool is_specialized = true;
133 
134  static constexpr auto
135  get_mdspan = [](auto&& t)
136  { return OpenKalman::get_mdspan(std::forward<decltype(t)>(t).nested_object()); };
137 
138  static constexpr auto
139  get_pattern_collection = [](auto&& t) -> decltype(auto)
140  { return std::forward<decltype(t)>(t).pattern_collection(); };
141  };
142 
143 
147  template<typename Nested, typename PatternCollection>
148  struct library_interface<pattern_adapter<Nested, PatternCollection>>
149  : pass_through_library_interface<pattern_adapter<Nested, PatternCollection>, Nested>
150  {};
151 
152  }
153 
154 
155 }
156 
157 
158 #endif
Pass-through object traits.
Definition: pass_through_interface.hpp:39
constexpr pattern_adapter(Arg &&arg, P &&p)
Construct from an indexible object and a pattern_collection.
Definition: pattern_adapter.hpp:63
Library interface traits for pattern_adapter.
Definition: pass_through_interface.hpp:115
decltype(auto) constexpr get_pattern_collection(T &&t)
Get the patterns::pattern_collection associated with indexible object T.
Definition: get_pattern_collection.hpp:36
Definition: AdapterBase.hpp:38
constexpr const PatternCollection & pattern_collection() const &
Definition: pattern_adapter.hpp:92
The root namespace for OpenKalman.
Definition: basics.hpp:34
An interface to various routines from the linear algebra library associated with indexible object T...
Definition: library_interface.hpp:42
Definition: object_traits.hpp:38
constexpr const PatternCollection && pattern_collection() const &&
Definition: pattern_adapter.hpp:98
An adapter that attaches a pattern_collection to an indexible object.
Definition: pattern_adapter.hpp:38
Definition for pattern_collection_for.
constexpr auto compare_pattern_collections(const A &a, const B &b)
Compare each element of two pattern_collection objects lexicographically.
Definition: compare_pattern_collections.hpp:137
constexpr Nested & nested_object() &
Get the nested object.
Definition: AdapterBase.hpp:77
Definition for indexible.
constexpr pattern_adapter()=default
Default constructor.
constexpr PatternCollection & pattern_collection() &
Get the associated pattern_collection.
Definition: pattern_adapter.hpp:89
constexpr PatternCollection && pattern_collection() &&
Definition: pattern_adapter.hpp:95
constexpr auto to_extents(P &&p)
Convert a pattern_collection to std::extents.
Definition: to_extents.hpp:78
decltype(auto) constexpr get_mdspan(T &&t)
Get the mdspan associated with indexible object T.
Definition: get_mdspan.hpp:35