SoapySDR  0.8.1-2
Vendor and platform neutral SDR interface library
Types.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 #include <SoapySDR/Config.hpp>
13 #include <SoapySDR/Types.h>
14 #include <type_traits>
15 #include <vector>
16 #include <stdexcept>
17 #include <string>
18 #include <map>
19 
20 namespace SoapySDR
21 {
22 
24 typedef std::map<std::string, std::string> Kwargs;
25 
30 SOAPY_SDR_API Kwargs KwargsFromString(const std::string &markup);
31 
36 SOAPY_SDR_API std::string KwargsToString(const Kwargs &args);
37 
39 typedef std::vector<Kwargs> KwargsList;
40 
48 template <typename Type>
49 Type StringToSetting(const std::string &s);
50 
58 template <typename Type>
59 std::string SettingToString(const Type &s);
60 
65 {
66 public:
67 
69  Range(void);
70 
72  Range(const double minimum, const double maximum, const double step=0.0);
73 
75  double minimum(void) const;
76 
78  double maximum(void) const;
79 
81  double step(void) const;
82 
83 private:
84  double _min, _max, _step;
85 };
86 
92 typedef std::vector<Range> RangeList;
93 
98 {
99 public:
100 
102  ArgInfo(void);
103 
105  std::string key;
106 
112  std::string value;
113 
115  std::string name;
116 
118  std::string description;
119 
121  std::string units;
122 
124  enum Type {BOOL, INT, FLOAT, STRING} type;
125 
132 
137  std::vector<std::string> options;
138 
143  std::vector<std::string> optionNames;
144 };
145 
149 typedef std::vector<ArgInfo> ArgInfoList;
150 
151 }
152 
153 inline double SoapySDR::Range::minimum(void) const
154 {
155  return _min;
156 }
157 
158 inline double SoapySDR::Range::maximum(void) const
159 {
160  return _max;
161 }
162 
163 inline double SoapySDR::Range::step(void) const
164 {
165  return _step;
166 }
167 
168 namespace SoapySDR {
169 namespace Detail {
170 
171 
172 //private implementation details for settings converters
173 
174 template <typename Type>
175 typename std::enable_if<std::is_same<Type, bool>::value, Type>::type StringToSetting(const std::string &s)
176 {
177  if (s.empty() or s == SOAPY_SDR_FALSE) {
178  return false;
179  }
180  if (s == SOAPY_SDR_TRUE) {
181  return true;
182  }
183  try {
184  // C++: Float conversion to bool is unambiguous
185  return std::stod(s);
186  } catch (std::invalid_argument&) {
187  }
188  // other values are true
189  return true;
190 }
191 
192 template <typename Type>
193 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)
194 {
195  return Type(std::stoll(s));
196 }
197 
198 template <typename Type>
199 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)
200 {
201  return Type(std::stoull(s));
202 }
203 
204 template <typename Type>
205 typename std::enable_if<std::is_floating_point<Type>::value, Type>::type StringToSetting(const std::string &s)
206 {
207  return Type(std::stod(s));
208 }
209 
210 template <typename Type>
211 typename std::enable_if<std::is_same<typename std::decay<Type>::type, std::string>::value, Type>::type StringToSetting(const std::string &s)
212 {
213  return s;
214 }
215 
216 inline std::string SettingToString(const bool &s)
217 {
219 }
220 
221 inline std::string SettingToString(const char *s)
222 {
223  return s;
224 }
225 
226 inline std::string SettingToString(const std::string &s)
227 {
228  return s;
229 }
230 
231 template <typename Type>
232 std::string SettingToString(const Type &s)
233 {
234  return std::to_string(s);
235 }
236 
237 }}
238 
239 template <typename Type>
240 Type SoapySDR::StringToSetting(const std::string &s)
241 {
242  return SoapySDR::Detail::StringToSetting<Type>(s);
243 }
244 
245 template <typename Type>
246 std::string SoapySDR::SettingToString(const Type &s)
247 {
249 }
#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:98
ArgInfo(void)
Default constructor.
std::string units
The units of the argument: dB, Hz, etc (optional)
Definition: Types.hpp:121
Range range
Definition: Types.hpp:131
std::vector< std::string > options
Definition: Types.hpp:137
std::string description
A brief description about the argument (optional)
Definition: Types.hpp:118
std::vector< std::string > optionNames
Definition: Types.hpp:143
std::string value
Definition: Types.hpp:112
std::string name
The displayable name of the argument (optional, use key if empty)
Definition: Types.hpp:115
std::string key
The key used to identify the argument (required)
Definition: Types.hpp:105
Type
The data type of the argument (required)
Definition: Types.hpp:124
@ BOOL
Definition: Types.hpp:124
Definition: Types.hpp:65
double maximum(void) const
Get the range maximum.
Definition: Types.hpp:158
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:153
double step(void) const
Get the range step size.
Definition: Types.hpp:163
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:175
std::string SettingToString(const bool &s)
Definition: Types.hpp:216
Definition: ConverterPrimitives.hpp:15
SOAPY_SDR_API Kwargs KwargsFromString(const std::string &markup)
std::vector< Range > RangeList
Definition: Types.hpp:92
std::vector< ArgInfo > ArgInfoList
Definition: Types.hpp:149
Type StringToSetting(const std::string &s)
Definition: Types.hpp:240
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:24
std::vector< Kwargs > KwargsList
Typedef for a list of key-word dictionaries.
Definition: Types.hpp:39
std::string SettingToString(const Type &s)
Definition: Types.hpp:246