OpenKalman
operation.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-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_VALUES_OPERATION_HPP
18 #define OPENKALMAN_VALUES_OPERATION_HPP
19 
20 #include "basics/basics.hpp"
24 
25 namespace OpenKalman::values
26 {
32 #ifdef __cpp_concepts
33  template<typename Operation, fixed...Args> requires
34  std::bool_constant<(stdex::invoke(Operation{}, fixed_value_of_v<Args>...), true)>::value
35 #else
36  template<typename Operation, typename...Args>
37 #endif
39  {
40  constexpr consteval_operation() = default;
41  explicit constexpr consteval_operation(const Operation&, const Args&...) {};
42  static constexpr auto value = stdex::invoke(Operation{}, fixed_value_of_v<Args>...);
43  using value_type = std::decay_t<decltype(value)>;
44  using type = consteval_operation;
45  constexpr operator value_type() const { return value; }
46  constexpr value_type operator()() const { return value; }
47  };
48 
49 
53  template<typename Operation, typename...Args>
54  explicit consteval_operation(const Operation&, const Args&...) -> consteval_operation<Operation, Args...>;
55 
56 
57  namespace detail
58  {
59 #ifdef __cpp_concepts
60  template<typename Op, typename...Args>
61 #else
62  template<typename Op, typename = void, typename...Args>
63 #endif
64  struct operation_consteval_invocable_impl : std::false_type{};
65 
66 
67 #ifdef __cpp_concepts
68  template<typename Op, typename...Args> requires requires { consteval_operation {std::declval<Op>(), std::declval<Args>()...}; }
69  struct operation_consteval_invocable_impl<Op, Args...>
70 #else
71  template<typename Op, typename...Args>
72  struct operation_consteval_invocable_impl<Op, std::enable_if_t<std::bool_constant<(stdex::invoke(Op{}, fixed_value_of<Args>::value...), true)>::value>, Args...>
73 #endif
74  : std::true_type{};
75 
76 
77  template<typename Op, typename...Args>
78 #ifdef __cpp_concepts
79  concept operation_consteval_invocable = operation_consteval_invocable_impl<Op, Args...>::value;
80 #else
81  inline constexpr bool operation_consteval_invocable = operation_consteval_invocable_impl<Op, void, Args...>::value;
82 #endif
83  }
84 
85 
92 #ifdef __cpp_concepts
93  template<typename Operation, typename...Args> requires std::invocable<Operation&&, value_type_of_t<Args&&>...>
94 #else
95  template<typename Operation, typename...Args>
96 #endif
97  constexpr auto
98  operation(Operation&& op, Args&&...args)
99  {
100  if constexpr ((... and fixed<Args>) and detail::operation_consteval_invocable<Operation, Args...>)
101  {
102  return consteval_operation {op, args...};
103  }
104  else
105  {
106  return stdex::invoke(std::forward<Operation>(op), to_value_type(std::forward<Args>(args))...);
107  }
108  }
109 
110 
114  template<typename Operation, typename...Args>
115  using operation_t = decltype(operation(std::declval<Operation&&>(), std::declval<Args&&>()...));
116 
117 }
118 
119 #endif
Definition for values::to_value_type.
decltype(auto) constexpr to_value_type(Arg &&arg)
Convert, if necessary, a fixed or dynamic value to its underlying base type.
Definition: to_value_type.hpp:28
constexpr bool value
T is a fixed or dynamic value that is reducible to a number.
Definition: value.hpp:45
Definition for value:value_type_of and value:value_type_of_t.
Definition for values::abs.
Definition: fixed-constants.hpp:23
decltype(operation(std::declval< Operation && >(), std::declval< Args && >()...)) operation_t
The resulting type from an values::operation.
Definition: operation.hpp:115
Definition for values::fixed_value_of.
consteval_operation(const Operation &, const Args &...) -> consteval_operation< Operation, Args... >
Deduction guide.
constexpr bool fixed
T is a value that is determinable at compile time.
Definition: fixed.hpp:66
Basic definitions for OpenKalman as a whole.
An operation involving some number of values.
Definition: operation.hpp:38
constexpr auto operation(Operation &&op, Args &&...args)
A potentially constant-evaluated operation involving some number of values.
Definition: operation.hpp:98