OpenKalman
ElementAccessor.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) 2020-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_ELEMENTACCESSOR_HPP
17 #define OPENKALMAN_ELEMENTACCESSOR_HPP
18 
19 #include <functional>
20 
21 namespace OpenKalman::internal
22 {
28 #ifdef __cpp_lib_ranges
29  template<indexible Object, index_range_for<Object> Indices> requires
30  writable_by_component<Object, Indices> and values::index<std::ranges::range_value_t<Indices>>
31 #else
32  template<typename Object, typename Indices>
33 #endif
35  {
36  using Scalar = scalar_type_of_t<Object>;
37 
38 
39 #ifdef __cpp_lib_ranges
40  template<typename Arg, std::invocable PreAccess, std::invocable PostSet> requires
41  std::same_as<Scalar, scalar_type_of_t<Arg>>
42 #else
43  template<typename Arg, typename PreAccess, typename PostSet, std::enable_if_t<
44  std::is_invocable_v<PreAccess> and std::is_invocable_v<PostSet> and
45  std::is_same_v<Scalar, typename scalar_type_of<Arg>::type>, int> = 0>
46 #endif
47  ElementAccessor(Arg&& arg, Indices&& indices, PreAccess&& pre_access = []{}, PostSet&& post_set = []{})
48  : object {std::forward<Arg>(arg)},
49  indices {std::forward<Indices>(indices)},
50  before_access {std::forward<decltype(pre_access)>(pre_access)},
51  after_set {std::forward<decltype(post_set)>(post_set)} {}
52 
53 
55  operator Scalar() const
56  {
57  before_access();
58  return get_component(object, indices);
59  }
60 
61 
63  void operator=(Scalar s)
64  {
65  before_access();
66  set_component(object, s, indices);
67  after_set();
68  }
69 
70  private:
71 
72  Object object;
73 
74  Indices indices;
75 
76  const std::function<void()> before_access;
77 
78  const std::function<void()> after_set;
79 
80  };
81 
82 
83  // ----------------- //
84  // Deduction guide //
85  // ----------------- //
86 
87  template<typename Arg, typename Indices, typename PreAccess, typename PostSet>
88  ElementAccessor(Arg&&, Indices&&, PreAccess&&, PostSet&&) -> ElementAccessor<Arg, Indices>;
89 
90 
91 }
92 
93 #endif //OPENKALMAN_ELEMENTACCESSOR_HPP
Definition: ElementAccessor.hpp:34
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
void operator=(Scalar s)
Set an element.
Definition: ElementAccessor.hpp:63
decltype(auto) constexpr get_component(Arg &&arg, const Indices &indices)
Get a component of an object at a particular set of indices.
Definition: get_component.hpp:54
Definition: basics.hpp:48