OpenKalman
fixed_value.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_FIXED_VALUE_HPP
18 #define OPENKALMAN_VALUES_FIXED_VALUE_HPP
19 
22 #include "values/math/real.hpp"
23 #include "values/math/imag.hpp"
24 
25 namespace OpenKalman::values
26 {
33 #ifdef __cpp_concepts
34  template<typename C, auto...constant> requires
35  (sizeof...(constant) == 1 and requires { static_cast<C>((constant,...)); }) or
36  (not complex<C> or std::bool_constant<(interface::number_traits<std::decay_t<C>>::make_complex(constant...), sizeof...(constant) == 2)>::value) or
37  (complex<C> or std::bool_constant<(C{constant...}, true)>::value)
38 #else
39  template<typename C, auto...constant>
40 #endif
41  struct fixed_value
42  {
43  static constexpr auto value = []
44  {
45  if constexpr (sizeof...(constant) == 1) return static_cast<C>((constant,...));
46  else if constexpr (complex<C>) return interface::number_traits<std::decay_t<C>>::make_complex(constant...);
47  else return C {constant...};
48  }();
49 
50 
51  using value_type = std::decay_t<decltype(value)>;
52 
53 
54  using type = fixed_value;
55 
56 
57  constexpr operator value_type() const { return value; }
58 
59 
60  constexpr value_type operator()() const { return value; }
61 
62 
63  constexpr fixed_value() = default;
64 
65 
66 #ifdef __cpp_concepts
67  template<fixed T> requires (fixed_value_of_v<T> == value)
68 #else
70 #endif
71  explicit constexpr fixed_value(const T&) {};
72 
73 
74 #ifdef __cpp_concepts
75  template<fixed T> requires (fixed_value_of_v<T> == value)
76 #else
77  template<typename T, std::enable_if_t<fixed_value_of<T>::value == value, int> = 0>
78 #endif
79  constexpr fixed_value& operator=(const T&) { return *this; }
80 
81  };
82 
83 
88 #ifdef __cpp_concepts
89  template<fixed T> requires (not complex<T>)
90 #else
91  template<typename T, std::enable_if_t<fixed<T> and not complex<T>, int> = 0>
92 #endif
93  explicit fixed_value(const T&) -> fixed_value<typename fixed_value_of<T>::value_type, fixed_value_of_v<T>>;
94 
95 
100 #ifdef __cpp_concepts
101  template<fixed T> requires complex<T>
102 #else
103  template<typename T, std::enable_if_t<fixed<T> and complex<T>, int> = 0>
104 #endif
105  explicit fixed_value(const T&) -> fixed_value<typename fixed_value_of<T>::value_type, values::real(fixed_value_of_v<T>), values::imag(fixed_value_of_v<T>)>;
106 
107 
108 }
109 
110 
111 #endif
Definition: fixed_value.hpp:41
constexpr bool value
T is a fixed or dynamic value that is reducible to a number.
Definition: value.hpp:45
constexpr auto imag(const Arg &arg)
A constexpr function to obtain the imaginary part of a (complex) number.
Definition: imag.hpp:40
Definition for values::imag.
constexpr auto real(const Arg &arg)
A constexpr function to obtain the real part of a (complex) number.
Definition: real.hpp:40
Definition for values::abs.
Definition: fixed-constants.hpp:23
Definition for values::real.
Definition: number_traits.hpp:36
Traits for arithmetic and complex scalar types.
Definition for values::complex.