SoapySDR  0.8.1-g8c6cb7c5
Vendor and platform neutral SDR interface library
Types.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #include <SoapySDR/Config.hpp>
14 #include <SoapySDR/Types.h>
15 #include <cstring>
16 #include <type_traits>
17 #include <vector>
18 #include <stdexcept>
19 #include <string>
20 #include <map>
21 
22 namespace SoapySDR
23 {
24 
26 typedef std::map<std::string, std::string> Kwargs;
27 
32 SOAPY_SDR_API Kwargs KwargsFromString(const std::string &markup);
33 
38 SOAPY_SDR_API std::string KwargsToString(const Kwargs &args);
39 
41 typedef std::vector<Kwargs> KwargsList;
42 
50 template <typename Type>
51 Type StringToSetting(const std::string &s);
52 
60 template <typename Type>
61 std::string SettingToString(const Type &s);
62 
67 {
68 public:
69 
71  Range(void);
72 
74  Range(const double minimum, const double maximum, const double step=0.0);
75 
77  double minimum(void) const;
78 
80  double maximum(void) const;
81 
83  double step(void) const;
84 
85 private:
86  double _min, _max, _step;
87 };
88 
94 typedef std::vector<Range> RangeList;
95 
100 {
101 public:
102 
104  ArgInfo(void);
105 
107  std::string key;
108 
114  std::string value;
115 
117  std::string name;
118 
120  std::string description;
121 
123  std::string units;
124 
126  enum Type {BOOL, INT, FLOAT, STRING} type;
127 
134 
139  std::vector<std::string> options;
140 
145  std::vector<std::string> optionNames;
146 };
147 
151 typedef std::vector<ArgInfo> ArgInfoList;
152 
153 }
154 
155 inline double SoapySDR::Range::minimum(void) const
156 {
157  return _min;
158 }
159 
160 inline double SoapySDR::Range::maximum(void) const
161 {
162  return _max;
163 }
164 
165 inline double SoapySDR::Range::step(void) const
166 {
167  return _step;
168 }
169 
170 namespace SoapySDR {
171 namespace Detail {
172 
173 
174 //private implementation details for settings converters
175 
176 template <typename Type>
177 typename std::enable_if<std::is_same<Type, bool>::value, Type>::type StringToSetting(const std::string &s)
178 {
179  if (s.empty() or s == SOAPY_SDR_FALSE) {
180  return false;
181  }
182  if (s == SOAPY_SDR_TRUE) {
183  return true;
184  }
185  try {
186  // C++: Float conversion to bool is unambiguous
187  char *str_end = nullptr;
188  double d = std::strtod(s.c_str(), &str_end);
189 
190  // Either the input wasn't numeric, so str_end should point to the front of the string,
191  // or the whole string was consumed and the resulting number is non-zero.
192  return (s == str_end) or ((d != 0.0) and (std::strlen(str_end) == 0));
193  } catch (std::invalid_argument&) {
194  }
195  // other values are true
196  return true;
197 }
198 
199 template <typename Type>
200 typename std::enable_if<not std::is_same<Type, bool>::value and std::is_integral<Type>::value and std::is_signed<Type>::value, Type>::type StringToSetting(const std::string &s)
201 {
202  return Type(std::stoll(s));
203 }
204 
205 template <typename Type>
206 typename std::enable_if<not std::is_same<Type, bool>::value and std::is_integral<Type>::value and std::is_unsigned<Type>::value, Type>::type StringToSetting(const std::string &s)
207 {
208  return Type(std::stoull(s));
209 }
210 
211 template <typename Type>
212 typename std::enable_if<std::is_floating_point<Type>::value, Type>::type StringToSetting(const std::string &s)
213 {
214  return Type(std::stod(s));
215 }
216 
217 template <typename Type>
218 typename std::enable_if<std::is_same<typename std::decay<Type>::type, std::string>::value, Type>::type StringToSetting(const std::string &s)
219 {
220  return s;
221 }
222 
223 inline std::string SettingToString(const bool &s)
224 {
226 }
227 
228 inline std::string SettingToString(const char *s)
229 {
230  return s;
231 }
232 
233 inline std::string SettingToString(const std::string &s)
234 {
235  return s;
236 }
237 
238 template <typename Type>
239 std::string SettingToString(const Type &s)
240 {
241  return std::to_string(s);
242 }
243 
244 }}
245 
246 template <typename Type>
247 Type SoapySDR::StringToSetting(const std::string &s)
248 {
249  return SoapySDR::Detail::StringToSetting<Type>(s);
250 }
251 
252 template <typename Type>
253 std::string SoapySDR::SettingToString(const Type &s)
254 {
256 }
#define SOAPY_SDR_API
Definition: Config.h:41
#define SOAPY_SDR_FALSE
String definition for boolean false used in settings.
Definition: Types.h:23
#define SOAPY_SDR_TRUE
String definition for boolean true used in settings.
Definition: Types.h:20
Definition: Types.hpp:100
ArgInfo(void)
Default constructor.
std::string units
The units of the argument: dB, Hz, etc (optional)
Definition: Types.hpp:123
Range range
Definition: Types.hpp:133
std::vector< std::string > options
Definition: Types.hpp:139
std::string description
A brief description about the argument (optional)
Definition: Types.hpp:120
std::vector< std::string > optionNames
Definition: Types.hpp:145
std::string value
Definition: Types.hpp:114
std::string name
The displayable name of the argument (optional, use key if empty)
Definition: Types.hpp:117
std::string key
The key used to identify the argument (required)
Definition: Types.hpp:107
Type
The data type of the argument (required)
Definition: Types.hpp:126
@ BOOL
Definition: Types.hpp:126
Definition: Types.hpp:67
double maximum(void) const
Get the range maximum.
Definition: Types.hpp:160
Range(const double minimum, const double maximum, const double step=0.0)
Create a min/max range.
double minimum(void) const
Get the range minimum.
Definition: Types.hpp:155
double step(void) const
Get the range step size.
Definition: Types.hpp:165
Range(void)
Create an empty range (0.0, 0.0)
std::enable_if< std::is_same< Type, bool >::value, Type >::type StringToSetting(const std::string &s)
Definition: Types.hpp:177
std::string SettingToString(const bool &s)
Definition: Types.hpp:223
Definition: ConverterPrimitives.hpp:15
SOAPY_SDR_API Kwargs KwargsFromString(const std::string &markup)
std::vector< Range > RangeList
Definition: Types.hpp:94
std::vector< ArgInfo > ArgInfoList
Definition: Types.hpp:151
Type StringToSetting(const std::string &s)
Definition: Types.hpp:247
SOAPY_SDR_API std::string KwargsToString(const Kwargs &args)
std::map< std::string, std::string > Kwargs
Typedef for a dictionary of key-value string arguments.
Definition: Types.hpp:26
std::vector< Kwargs > KwargsList
Typedef for a list of key-word dictionaries.
Definition: Types.hpp:41
std::string SettingToString(const Type &s)
Definition: Types.hpp:253