OpenKalman
set_component.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) 2022-2024 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_SET_COMPONENT_HPP
17 #define OPENKALMAN_SET_COMPONENT_HPP
18 
19 
20 namespace OpenKalman
21 {
22  // \todo Add functions that return stl ranges.
23 
24  namespace detail
25  {
26  template<typename Arg, typename Indices>
27  constexpr Arg&&
28  set_component_impl(Arg&& arg, const scalar_type_of_t<Arg>& s, const Indices& indices)
29  {
30  using Trait = interface::library_interface<std::decay_t<Arg>>;
31  Trait::set_component(arg, s, internal::truncate_indices(indices, count_indices(arg)));
32  return std::forward<Arg>(arg);
33  }
34  } // namespace detail
35 
36 
44 #ifdef __cpp_lib_concepts
45  template<indexible Arg, index_range_for<Arg> Indices> requires writable_by_component<Arg, Indices>
46 #else
47  template<typename Arg, typename Indices, std::enable_if_t<
48  indexible<Arg> and index_range_for<Indices, Arg> and writable_by_component<Arg, Indices>, int> = 0>
49 #endif
50  inline Arg&&
51  set_component(Arg&& arg, const scalar_type_of_t<Arg>& s, const Indices& indices)
52  {
53  return detail::set_component_impl(std::forward<Arg>(arg), s, indices);
54  }
55 
56 
60 #ifdef __cpp_lib_concepts
61  template<indexible Arg, values::index Ix> requires writable_by_component<Arg, const std::initializer_list<Ix>&>
62 #else
63  template<typename Arg, typename Ix, std::enable_if_t<
64  indexible<Arg> and values::index<Ix> and writable_by_component<Arg, const std::initializer_list<Ix>&>, int> = 0>
65 #endif
66  inline Arg&&
67  set_component(Arg&& arg, const scalar_type_of_t<Arg>& s, const std::initializer_list<Ix>& indices)
68  {
69  return detail::set_component_impl(std::forward<Arg>(arg), s, indices);
70  }
71 
72 
79 #ifdef __cpp_lib_concepts
80  template<typename Arg, values::index...I> requires writable_by_component<Arg, std::array<std::size_t, sizeof...(I)>> and
81  (index_count_v<Arg> == dynamic_size or sizeof...(I) >= index_count_v<Arg>) and
83 #else
84  template<typename Arg, typename...I, std::enable_if_t<
85  (values::index<I> and ...) and writable_by_component<Arg, std::array<std::size_t, sizeof...(I)>> and
88 #endif
89  inline Arg&&
90  set_component(Arg&& arg, const scalar_type_of_t<Arg>& s, I&&...i)
91  {
92  if constexpr (sizeof...(I) == 0)
93  return detail::set_component_impl(std::forward<Arg>(arg), s, std::array<std::size_t, 0> {});
94  else
95  return detail::set_component_impl(std::forward<Arg>(arg), s,
96  std::array {static_cast<std::common_type_t<I...>>(std::forward<I>(i))...});
97  }
98 
99 
100 } // namespace OpenKalman
101 
102 #endif //OPENKALMAN_SET_COMPONENT_HPP
constexpr auto count_indices(const T &t)
Get the number of indices available to address the components of an indexible object.
Definition: count_indices.hpp:33
Arg && set_component(Arg &&arg, const scalar_type_of_t< Arg > &s, I &&...i)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: set_component.hpp:90
Arg && set_component(Arg &&arg, const scalar_type_of_t< Arg > &s, const Indices &indices)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: set_component.hpp:51
typename scalar_type_of< T >::type scalar_type_of_t
helper template for scalar_type_of.
Definition: scalar_type_of.hpp:54
The root namespace for OpenKalman.
Definition: basics.hpp:34
constexpr bool index
T is an index value.
Definition: index.hpp:56
The minimum number of indices need to access all the components of an object.
Definition: index_count.hpp:33
constexpr std::size_t dynamic_size
A constant indicating that a size or index is dynamic.
Definition: global-definitions.hpp:33
constexpr bool writable_by_component
Specifies that a type has components that can be set with Indices (an std::ranges::input_range) of ty...
Definition: writable_by_component.hpp:36