OpenKalman
copy_from.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_COPY_FROM_HPP
17 #define OPENKALMAN_COPY_FROM_HPP
18 
24 
25 namespace OpenKalman
26 {
27  namespace detail
28  {
29  template<typename LHS, typename RHS, typename...J>
30  static void copy_tensor_elements(LHS&& lhs, RHS&& rhs, std::index_sequence<>, J...j)
31  {
32  access(lhs, j...) = access(std::forward<RHS>(rhs), j...);
33  }
34 
35 
36  template<typename LHS, typename RHS, std::size_t I, std::size_t...Is, typename...J>
37  static void copy_tensor_elements(LHS&& lhs, RHS&& rhs, std::index_sequence<I, Is...>, J...j)
38  {
39  for (std::size_t i = 0; i < get_index_extent<I>(rhs); i++)
40  copy_tensor_elements(lhs, std::forward<RHS>(rhs), std::index_sequence<Is...> {}, j..., i);
41  }
42  }
43 
44 
53 #ifdef __cpp_concepts
54  template<indexible Dest, indexible Source> requires copyable_from<Dest, Source>
55 #else
56  template<typename Source, typename Dest, std::enable_if_t<copyable_from<Dest, Source>, int> = 0>
57 #endif
58  constexpr decltype(auto)
59  copy_from(Dest&& dest, Source&& source)
60  {
61  if constexpr (interface::copy_from_defined_for<Dest&, Source&&>)
62  {
63  interface::library_interface<Dest>::copy(dest, std::forward<Source>(source));
64  }
65  else if constexpr (interface::copy_from_defined_for<Dest&, decltype(get_mdspan(std::declval<Source&&>()))>)
66  {
67  interface::library_interface<Dest>::copy(dest, get_mdspan(std::forward<Source>(source)));
68  }
69  else
70  {
71  detail::copy_tensor_elements(get_mdspan(dest), get_mdspan(std::forward<Source>(source)), std::make_index_sequence<index_count_v<Dest>>{});
72  }
73  return std::forward<Dest>(dest);
74  }
75 
76 
77 }
78 
79 #endif
constexpr auto get_mdspan(T &t)
Get the coordinates::pattern_collection associated with indexible object T.
Definition: get_mdspan.hpp:35
decltype(auto) constexpr copy_from(Dest &&dest, Source &&source)
Copy elements from one object to another.
Definition: copy_from.hpp:59
Definition of get_index_extent function.
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
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
Definition for access function.
Definition for index_count.
Definition: trait_backports.hpp:64
Concepts for testing whether object_traits or library_interface definitions exist for a particular ob...