Pothos  0.5.0-ga4964040
The Pothos dataflow programming software suite
QFormat.hpp
Go to the documentation of this file.
1 
11 #include <Pothos/Config.hpp>
12 #include <type_traits>
13 #include <complex>
14 #include <cmath>
15 
16 namespace Pothos {
17 namespace Util {
18 
20 template <typename T>
22 {
23  typedef T Type;
24 };
25 
27 template <typename T>
28 struct Fundamental<std::complex<T>>
29 {
30  typedef T Type;
31 };
32 
46 template <typename T, typename U>
47 T fromQ(const U &in, const int n);
48 
53 template <typename T, typename U>
54 T fromQ(const U &in);
55 
69 template <typename T, typename U>
70 T floatToQ(const U &in, const int n);
71 
76 template <typename T, typename U>
77 T floatToQ(const U &in);
78 
79 namespace Detail {
80 
81 template <typename T, typename U>
82 T fromQImpl(const U &in, const int, std::false_type)
83 {
84  return T(in);
85 }
86 
87 template <typename T, typename U>
88 T fromQImpl(const U &in, const int n, std::true_type)
89 {
90  return T(in >> n);
91 }
92 
93 template <typename T, typename U>
94 T fromQImpl(const std::complex<U> &in, const int, std::false_type)
95 {
96  return T(in);
97 }
98 
99 template <typename T, typename U>
100 T fromQImpl(const std::complex<U> &in, const int n, std::true_type)
101 {
102  auto real = fromQ<typename T::value_type, U>(in.real(), n);
103  auto imag = fromQ<typename T::value_type, U>(in.imag(), n);
104  return T(real, imag);
105 }
106 
107 template <typename T, typename U>
108 T floatToQImpl(const U &in, const int, std::false_type)
109 {
110  return T(in);
111 }
112 
113 template <typename T, typename U>
114 T floatToQImpl(const U &in, const int n, std::true_type)
115 {
116  return T(std::ldexp(in, n));
117 }
118 
119 template <typename T, typename U>
120 T floatToQImpl(const std::complex<U> &in, const int, std::false_type)
121 {
122  return T(in);
123 }
124 
125 template <typename T, typename U>
126 T floatToQImpl(const std::complex<U> &in, const int n, std::true_type)
127 {
128  auto real = floatToQ<typename T::value_type, U>(in.real(), n);
129  auto imag = floatToQ<typename T::value_type, U>(in.imag(), n);
130  return T(real, imag);
131 }
132 
133 static_assert(std::is_same<typename Fundamental<double>::Type, double>::value, "Fundamental of number");
134 
135 static_assert(std::is_same<typename Fundamental<std::complex<double>>::Type, double>::value, "Fundamental of complex");
136 
137 } //namespace Detail
138 } //namespace Util
139 } //namespace Pothos
140 
141 template <typename T, typename U>
142 T Pothos::Util::fromQ(const U &in, const int n)
143 {
144  return Pothos::Util::Detail::fromQImpl<T>(in, n, std::is_integral<typename Fundamental<U>::Type>());
145 }
146 
147 template <typename T, typename U>
148 T Pothos::Util::fromQ(const U &in)
149 {
150  const int n = sizeof(typename Fundamental<U>::Type)*4;
151  return Pothos::Util::Detail::fromQImpl<T>(in, n, std::is_integral<typename Fundamental<U>::Type>());
152 }
153 
154 template <typename T, typename U>
155 T Pothos::Util::floatToQ(const U &in, const int n)
156 {
157  return Pothos::Util::Detail::floatToQImpl<T>(in, n, std::is_integral<typename Fundamental<T>::Type>());
158 }
159 
160 template <typename T, typename U>
162 {
163  const int n = sizeof(typename Fundamental<T>::Type)*4;
164  return Pothos::Util::Detail::floatToQImpl<T>(in, n, std::is_integral<typename Fundamental<T>::Type>());
165 }
T floatToQ(const U &in, const int n)
Definition: QFormat.hpp:155
T Type
Definition: QFormat.hpp:23
Determine the fundamental data type of T for primitive types.
Definition: QFormat.hpp:21
Definition: ArchiveEntry.hpp:20
T fromQ(const U &in, const int n)
Definition: QFormat.hpp:142