Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
complex_decomp.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_COMPLEX_DECOMP_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_COMPLEX_DECOMP_HPP_
7
8// IWYU pragma: private, include "arene/base/math.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
15#include "arene/base/stdlib_choice/declval.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
18
19namespace arene {
20namespace base {
21namespace math {
22
23namespace complex_decomp_detail {
24
25// parasoft-begin-suppress CERT_C-EXP37-a "False positive: rule does not mention naming all parameters"
26// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: function does not have internal linkage"
27
28/// @brief Dummy declaration of the ADL customization point so the call below will be well-formed but not match
29/// @tparam U A type being checked for real overloads
30template <typename U>
31auto real(U) -> U = delete;
32
33/// @brief Dummy declaration of the ADL customization point so the call below will be well-formed but not match
34/// @tparam U A type being checked for imag overloads
35template <typename U>
36auto imag(U) -> U = delete;
37
38// parasoft-end-suppress CERT_C-EXP37-a-3
39// parasoft-end-suppress AUTOSAR-M3_3_2-a
40
41/// @brief Type trait to determine if a type has a const member function called @c real()
42/// @tparam T The type to check
43template <typename T, typename = constraints<>>
44constexpr bool is_member_complex_v{false};
45
46/// @brief Type trait to determine if a type has a const member function called @c real()
47/// @tparam T The type to check
48template <typename T>
49constexpr bool is_member_complex_v<
50 T,
51 constraints<decltype(std::declval<T const&>().real()), decltype(std::declval<T const&>().imag())>>{true};
52
53/// @brief Type trait to determine if a type has an ADL free function <c>real(T const&)</c>
54/// @tparam T The type to check
55template <typename T, typename = constraints<>>
56constexpr bool is_adl_complex_v{false};
57
58/// @brief Type trait to determine if a type has an ADL free function <c>real(T const&)</c>
59/// @tparam T The type to check
60template <typename T>
61constexpr bool is_adl_complex_v<
62 T,
64 decltype(real(std::declval<T const&>())),
65 decltype(imag(std::declval<T const&>()))>>{!is_member_complex_v<T>};
66
67/// @brief Function object class implementing @c arene::base::math::real
68class real_impl {
69 public:
70 /// @brief Extract the real part of a value, assuming the whole value is real if no @c real function exists
71 /// @tparam T Type of the value to extract the real part from
72 /// @param value Value to extract the real part from
73 /// @return The real part of @c value if it appears to be complex, or @c value itself if it does not
74 template <typename T, constraints<std::enable_if_t<is_member_complex_v<T>>> = nullptr>
75 constexpr auto operator()(T const& value) const noexcept(noexcept(std::declval<T const&>().real()))
76 -> decltype(value.real()) {
77 return value.real();
78 }
79
80 /// @brief Extract the real part of a value, assuming the whole value is real if no @c real function exists
81 /// @tparam T Type of the value to extract the real part from
82 /// @param value Value to extract the real part from
83 /// @return The real part of @c value if it appears to be complex, or @c value itself if it does not
84 template <typename T, constraints<std::enable_if_t<is_adl_complex_v<T>>> = nullptr>
85 constexpr auto operator()(T const& value) const noexcept(noexcept(real(std::declval<T const&>())))
86 -> decltype(real(value)) {
87 return real(value);
88 }
89
90 /// @brief Extract the real part of a value, assuming the whole value is real if no @c real function exists
91 /// @tparam T Type of the value to extract the real part from
92 /// @param value Value to extract the real part from
93 /// @return The real part of @c value if it appears to be complex, or @c value itself if it does not
94 template <
95 typename T,
97 constexpr auto operator()(T const& value) const noexcept -> T const& {
98 return value;
99 }
100};
101
102/// @brief Function object class implementing @c arene::base::math::imag
103class imag_impl {
104 public:
105 /// @brief Extract the imaginary part of a value, assuming the whole value is real if no @c imag function exists
106 /// @tparam T Type of the value to extract the imaginary part from
107 /// @param value Value to extract the imaginary part from
108 /// @return The imaginary part of @c value if it appears to be complex, or <c>numbers::zero_v<T></c> if it does not
109 template <typename T, constraints<std::enable_if_t<is_member_complex_v<T>>> = nullptr>
110 constexpr auto operator()(T const& value) const noexcept(noexcept(std::declval<T const&>().imag()))
111 -> decltype(value.imag()) {
112 return value.imag();
113 }
114
115 /// @brief Extract the imaginary part of a value, assuming the whole value is real if no @c imag function exists
116 /// @tparam T Type of the value to extract the imaginary part from
117 /// @param value Value to extract the imaginary part from
118 /// @return The imaginary part of @c value if it appears to be complex, or <c>numbers::zero_v<T></c> if it does not
119 template <typename T, constraints<std::enable_if_t<is_adl_complex_v<T>>> = nullptr>
120 constexpr auto operator()(T const& value) const noexcept(noexcept(imag(std::declval<T const&>())))
121 -> decltype(imag(value)) {
122 return imag(value);
123 }
124
125 /// @brief Extract the imaginary part of a value, assuming the whole value is real if no @c imag function exists
126 /// @tparam T Type of the value to extract the imaginary part from
127 /// @return The imaginary part of @c value if it appears to be complex, or <c>numbers::zero_v<T></c> if it does not
128 template <
129 typename T,
131 constexpr auto operator()(T const&) const noexcept -> T const& {
132 return numbers::zero_v<T>;
133 }
134};
135} // namespace complex_decomp_detail
136
137// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object."
138// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object."
139/// @brief Extract the real part of a value, treating the whole value as real when no @c real function exists
140ARENE_CPP14_INLINE_VARIABLE(complex_decomp_detail::real_impl, real);
141
142/// @brief Extract the imaginary part of a value, returning zero when no @c imag function exists
143ARENE_CPP14_INLINE_VARIABLE(complex_decomp_detail::imag_impl, imag);
144// parasoft-end-suppress AUTOSAR-M7_3_3-a
145// parasoft-end-suppress CERT_CPP-DCL59-a
146
147/// @brief Determine if a type models a complex number or not
148/// @tparam T The type to check
149///
150/// Equal to @c true if either @c T has member functions @c real and @c imag or there are free functions @c real(T) and
151/// @c imag(T) which can be found by argument-dependent lookup. Otherwise equal to <c>false</c>.
152template <typename T>
153constexpr bool
154 is_complex_v{complex_decomp_detail::is_member_complex_v<T> || complex_decomp_detail::is_adl_complex_v<T>};
155
156} // namespace math
157} // namespace base
158} // namespace arene
159
160#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_COMPLEX_DECOMP_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition abs_cpo.hpp:26
constexpr bool is_complex_v
Determine if a type models a complex number or not.
Definition complex_decomp.hpp:154
constexpr const & zero_v
constexpr reference to the additive identity value zero<T>::value .
Definition arithmetic_identities.hpp:58
Definition accumulate.hpp:37
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...