dsplib 1.1.0
C++ DSP library for MATLAB-like coding
Loading...
Searching...
No Matches
traits.h
1#pragma once
2
3#include <dsplib/types.h>
4#include <type_traits>
5
6namespace dsplib {
7
8template<typename T>
9class base_array;
10
11template<bool Cond_, typename Iftrue_, typename Iffalse_>
12using conditional_t = typename std::conditional_t<Cond_, Iftrue_, Iffalse_>;
13
14//reduce type for `a+b`, `a-b`, `a*b`, `a/b` operators
15template<typename T1, typename T2>
16constexpr auto reduce_operator_type() noexcept {
17 using T1_ = std::remove_reference_t<T1>;
18 using T2_ = std::remove_reference_t<T2>;
19 if constexpr (is_scalar_v<T1_> && is_scalar_v<T2_>) {
20 if constexpr (is_complex_v<T1_> || is_complex_v<T2_>) {
21 return cmplx_t{};
22 } else {
23 return real_t{};
24 }
25 } else if constexpr (!is_scalar_v<T1_> && !is_scalar_v<T2_>) {
26 using R1 = std::remove_reference_t<decltype(std::declval<T1_>()[0])>;
27 using R2 = std::remove_reference_t<decltype(std::declval<T2_>()[0])>;
28 return reduce_operator_type<R1, R2>();
29 } else if constexpr (!is_scalar_v<T1_>) {
30 using R1 = std::remove_reference_t<decltype(std::declval<T1_>()[0])>;
31 return reduce_operator_type<R1, T2_>();
32 } else {
33 using R2 = std::remove_reference_t<decltype(std::declval<T2_>()[0])>;
34 return reduce_operator_type<T1_, R2>();
35 }
36}
37
38template<typename T1, typename T2>
39using ResultType = decltype(reduce_operator_type<T1, T2>());
40
41//rules for implicit array conversion
42//TODO: use static_assert and verbose error message
43template<typename T_src, typename T_dst>
44constexpr bool is_array_convertible() noexcept {
45 if constexpr (!std::is_convertible_v<T_src, T_dst>) {
46 return false;
47 }
48
49 //only arithmetic scalar
50 if constexpr (!is_scalar_v<T_src> || !is_scalar_v<T_dst>) {
51 return false;
52 }
53
54 //cmplx -> real
55 if constexpr (is_complex_v<T_src> && !is_complex_v<T_dst>) {
56 return false;
57 }
58
59 //real -> cmplx
60 if constexpr (!is_complex_v<T_src> && is_complex_v<T_dst>) {
61 return false;
62 }
63
64 //float -> int
65 if constexpr (std::is_floating_point_v<T_src> && std::is_integral_v<T_dst>) {
66 return false;
67 }
68
69 return true;
70}
71
72template<typename T>
73constexpr bool support_type_for_array() {
74 using U = std::remove_cv_t<T>;
75 if constexpr (std::is_same_v<U, real_t>) {
76 return true;
77 }
78 if constexpr (std::is_same_v<U, cmplx_t>) {
79 return true;
80 }
81 if constexpr (std::is_same_v<U, int>) {
82 return true;
83 }
84 return false;
85}
86
87} // namespace dsplib