OpenKalman
cast_to.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 
17 #ifndef OPENKALMAN_VALUES_CAST_TO_HPP
18 #define OPENKALMAN_VALUES_CAST_TO_HPP
19 
23 #include "values/classes/Fixed.hpp"
24 
25 namespace OpenKalman::values
26 {
27 #if __cpp_nontype_template_args < 201911L
28  namespace detail
29  {
30  template<typename Arg, typename T>
31  struct FixedCast
32  {
33  using value_type = T;
34  static constexpr auto value {static_cast<value_type>(values::fixed_number_of_v<Arg>)};
35  using type = FixedCast;
36  constexpr operator value_type() const { return value; }
37  constexpr value_type operator()() const { return value; }
38  };
39  } // namespace detail
40 #endif
41 
42 
49 #ifdef __cpp_concepts
50  template<values::number T, values::value Arg>
51  constexpr values::value decltype(auto)
52 #else
53  template<typename T, typename Arg, std::enable_if_t<values::number<T> and values::value<Arg>, int> = 0>
54  constexpr decltype(auto)
55 #endif
56  cast_to(Arg&& arg)
57  {
58  if constexpr (std::is_same_v<values::number_type_of_t<Arg>, T>)
59  {
60  return std::forward<Arg>(arg);
61  }
62  else if constexpr (values::fixed<Arg>)
63  {
64  constexpr auto x = values::fixed_number_of_v<Arg>;
65 #if __cpp_nontype_template_args >= 201911L
66  return values::Fixed<T, x>{};
67 #else
68  if constexpr (x == static_cast<std::intmax_t>(x))
69  {
71  }
72  else
73  {
75  }
76 #endif
77  }
78  else
79  {
80  return static_cast<T>(values::to_number(std::forward<Arg>(arg)));
81  }
82  }
83 
84 } // namespace OpenKalman::values
85 
86 #endif //OPENKALMAN_VALUES_CAST_TO_HPP
Definition for values::to_number.
constexpr bool value
T is numerical value or is reducible to a numerical value.
Definition: value.hpp:31
constexpr auto to_number(Arg arg)
Convert any values::value to a values::number.
Definition: to_number.hpp:34
Definition for value:number_type_of_t.
Definition for values::abs.
Definition: constant_coefficient.hpp:25
Definition: Fixed.hpp:36
std::decay_t< decltype(values::to_number(std::declval< T >()))> number_type_of_t
Obtain the values::number type associated with avalues::value.
Definition: number_type_of_t.hpp:34
Definition for values::number.
Definition: cast_to.hpp:31