rohdeschwarz  0.2.0
TCPIP socket library for Rohde & Schwarz instrument control
to_vector.hpp
Go to the documentation of this file.
1 
8 #ifndef ROHDESCHWARZ_TO_VECTOR_HPP
9 #define ROHDESCHWARZ_TO_VECTOR_HPP
10 
11 
12 // std lib
13 #include <complex>
14 #include <cstddef>
15 #include <vector>
16 
17 
18 namespace rohdeschwarz
19 {
20 
21 
26  template <class out_type, class in_type = unsigned char>
27  std::vector<out_type> to_vector(in_type* data, std::size_t data_size)
28  {
29  // out pointer type
30  using out_type_p = out_type*;
31 
32  // calculate output size
33  // note: integer math is automatically floored
34  const std::size_t size = sizeof(in_type) * data_size / sizeof(out_type);
35 
36  // return vector copy
37  const out_type_p begin = out_type_p(data);
38  const out_type_p end = begin + size;
39  return std::vector<out_type>(begin, end);
40  }
41 
42 
50  template <class in_type = unsigned char>
51  std::vector<std::complex<double>> to_vector_complex_double(in_type* data, std::size_t data_size)
52  {
53  // parse to double
54  std::vector<double> values = to_vector<double>(data, data_size);
55 
56  // reserve output
57  std::vector<std::complex<double>> output;
58  output.reserve(values.size() / 2);
59 
60  // populate
61  for (int i = 0; i + 1 < values.size(); i += 2)
62  {
63  const double real = values[i];
64  const double imag = values[i + 1];
65  output.emplace_back(real, imag);
66  }
67  return output;
68  }
69 
70 
71 } // rohdeschwarz
72 #endif // ROHDESCHWARZ_TO_VECTOR_HPP
std::vector< std::complex< double > > to_vector_complex_double(in_type *data, std::size_t data_size)
Converts a vector of a primitive type to a vector of complex<double>
Definition: to_vector.hpp:51
std::vector< out_type > to_vector(in_type *data, std::size_t data_size)
Converts a vector of a primitive type to a vector of a different primitive type.
Definition: to_vector.hpp:27