raw_processing.h
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 Pilz GmbH & Co. KG
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <https://www.gnu.org/licenses/>.
15 #ifndef PSEN_SCAN_V2_STANDALONE_RAW_PROCESSING_H
16 #define PSEN_SCAN_V2_STANDALONE_RAW_PROCESSING_H
17 
18 #include <algorithm>
19 #include <cmath>
20 #include <functional>
21 #include <istream>
22 #include <sstream>
23 #include <vector>
24 
25 #include <fmt/format.h>
26 
28 {
29 namespace data_conversion_layer
30 {
35 namespace raw_processing
36 {
40 class StringStreamFailure : public std::runtime_error
41 {
42 public:
43  StringStreamFailure(const std::string& msg = "Error in raw data processing");
44 };
45 
46 template <typename T>
47 inline void write(std::ostringstream& os, const T& data)
48 {
49  os.write(reinterpret_cast<const char*>(&data), sizeof(T));
50 }
51 
52 template <typename T>
53 inline void read(std::istream& is, T& data)
54 {
55  is.read(reinterpret_cast<char*>(&data), sizeof(T));
56  if (!is)
57  {
59  fmt::format("Failure reading {} characters from input stream, could only read {}.", sizeof(T), is.gcount()));
60  }
61 }
62 
63 template <typename T>
64 inline T read(std::istream& is)
65 {
66  T retval;
67  raw_processing::read<T>(is, retval);
68  return retval;
69 }
70 
71 template <class ReturnType, class RawType>
72 using ConversionFunc = std::function<ReturnType(const RawType&)>;
73 
74 template <typename RawType, typename ReturnType>
75 inline ReturnType read(std::istream& is, const ConversionFunc<ReturnType, RawType>& conversion_fcn)
76 {
77  return conversion_fcn(raw_processing::read<RawType>(is));
78 }
79 
80 template <typename RawType, typename ReturnType>
81 inline ReturnType read(std::istream& is)
82 {
83  return ReturnType(raw_processing::read<RawType>(is));
84 }
85 
86 template <typename RawType, typename ReturnType>
87 inline void readArray(std::istream& is,
88  std::vector<ReturnType>& data,
89  const size_t& number_of_samples,
90  const ConversionFunc<ReturnType, RawType>& conversion_fcn)
91 {
92  data.reserve(number_of_samples);
93 
94  std::generate_n(std::back_inserter(data), number_of_samples, [&is, &conversion_fcn]() {
95  return raw_processing::read<RawType, ReturnType>(is, conversion_fcn);
96  });
97 }
98 
99 template <typename RawType, typename ArrayElemType>
100 inline void writeArray(std::ostringstream& os,
101  const std::vector<ArrayElemType>& array,
103 {
104  for (const auto& elem : array)
105  {
106  raw_processing::write(os, conversion_fcn(elem));
107  }
108 }
109 
110 template <typename T>
111 inline T toArray(std::ostringstream& os)
112 {
113  const std::string data_str(os.str());
114 
115  T raw_data;
116  raw_data.reserve(data_str.length());
117 
118  std::copy(data_str.begin(), data_str.end(), std::back_inserter(raw_data));
119  return raw_data;
120 }
121 
122 inline StringStreamFailure::StringStreamFailure(const std::string& msg) : std::runtime_error(msg)
123 {
124 }
125 
126 } // namespace raw_processing
127 } // namespace data_conversion_layer
128 } // namespace psen_scan_v2_standalone
129 
130 #endif // PSEN_SCAN_V2_STANDALONE_RAW_PROCESSING_H
psen_scan_v2_standalone::data_conversion_layer::raw_processing::readArray
void readArray(std::istream &is, std::vector< ReturnType > &data, const size_t &number_of_samples, const ConversionFunc< ReturnType, RawType > &conversion_fcn)
Definition: raw_processing.h:87
psen_scan_v2_standalone::data_conversion_layer::raw_processing::StringStreamFailure::StringStreamFailure
StringStreamFailure(const std::string &msg="Error in raw data processing")
Definition: raw_processing.h:122
psen_scan_v2_standalone::data_conversion_layer::raw_processing::writeArray
void writeArray(std::ostringstream &os, const std::vector< ArrayElemType > &array, ConversionFunc< RawType, ArrayElemType > conversion_fcn)
Definition: raw_processing.h:100
psen_scan_v2_standalone::data_conversion_layer::raw_processing::StringStreamFailure
Exception thrown if the incoming data from the scanner cannot be processed.
Definition: raw_processing.h:40
psen_scan_v2_standalone::data_conversion_layer::raw_processing::write
void write(std::ostringstream &os, const T &data)
Definition: raw_processing.h:47
psen_scan_v2_standalone::data_conversion_layer::raw_processing::ConversionFunc
std::function< ReturnType(const RawType &)> ConversionFunc
Definition: raw_processing.h:72
std
psen_scan_v2_standalone
Root namespace in which the software components to communicate with the scanner (firmware-version: 2)...
Definition: udp_client.h:41
psen_scan_v2_standalone::data_conversion_layer::raw_processing::toArray
T toArray(std::ostringstream &os)
Definition: raw_processing.h:111
psen_scan_v2_standalone::data_conversion_layer::raw_processing::read
void read(std::istream &is, T &data)
Definition: raw_processing.h:53


psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Jun 22 2024 02:46:11