SoapySDR: Types.hpp Source File

| SoapySDR 0.8.1-ARCH Vendor and platform neutral SDR interface library

Loading...
Searching...
No Matches
Types.hpp
Go to the documentation of this file.
1
11
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
22namespace 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
50template <typename Type>
51Type StringToSetting(const std::string &s);
52
60template <typename Type>
61std::string SettingToString(const Type &s);
62
66 class SOAPY_SDR_API Range
67{
68public:
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
85private:
86 double _min, _max, _step;
87};
88
94 typedef std::vector<Range> RangeList;
95
99 class SOAPY_SDR_API ArgInfo
100{
101public:
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
133 Range range;
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
170namespace SoapySDR {
171 namespace Detail {
172
173
174//private implementation details for settings converters
175
176template <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() || 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) || ((d != 0.0) && (std::strlen(str_end) == 0));
193 } catch (std::invalid_argument&) {
194 }
195 // other values are true
196 return true;
197}
198
199template <typename Type>
200 typename std::enable_if<!std::is_same<Type, bool>::value && std::is_integral<Type>::value && std::is_signed<Type>::value, Type>::type StringToSetting(const std::string &s)
201{
202 return Type(std::stoll(s));
203}
204
205template <typename Type>
206 typename std::enable_if<!std::is_same<Type, bool>::value && std::is_integral<Type>::value && std::is_unsigned<Type>::value, Type>::type StringToSetting(const std::string &s)
207{
208 return Type(std::stoull(s));
209}
210
211template <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
217template <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{
225 return s?SOAPY_SDR_TRUE:SOAPY_SDR_FALSE;
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
238template <typename Type>
239std::string SettingToString(const Type &s)
240{
241 return std::to_string(s);
242}
243
244}}
245
246template <typename Type>
247Type SoapySDR::StringToSetting(const std::string &s)
248{
249 return SoapySDR::Detail::StringToSetting<Type>(s);
250}
251
252template <typename Type>
253std::string SoapySDR::SettingToString(const Type &s)
254{
255 return SoapySDR::Detail::SettingToString(s);
256}
SOAPY_SDR_API
#define SOAPY_SDR_API
Definition Config.h:41
Config.hpp
Types.h
SOAPY_SDR_FALSE
#define SOAPY_SDR_FALSE
String definition for boolean false used in settings.
Definition Types.h:23
SOAPY_SDR_TRUE
#define SOAPY_SDR_TRUE
String definition for boolean true used in settings.
Definition Types.h:20
SoapySDR::ArgInfo::ArgInfo
ArgInfo(void)
Default constructor.
SoapySDR::ArgInfo::units
std::string units
The units of the argument: dB, Hz, etc (optional).
Definition Types.hpp:123
SoapySDR::ArgInfo::range
Range range
Definition Types.hpp:133
SoapySDR::ArgInfo::options
std::vector< std::string > options
Definition Types.hpp:139
SoapySDR::ArgInfo::description
std::string description
A brief description about the argument (optional).
Definition Types.hpp:120
SoapySDR::ArgInfo::optionNames
std::vector< std::string > optionNames
Definition Types.hpp:145
SoapySDR::ArgInfo::value
std::string value
Definition Types.hpp:114
SoapySDR::ArgInfo::name
std::string name
The displayable name of the argument (optional, use key if empty).
Definition Types.hpp:117
SoapySDR::ArgInfo::key
std::string key
The key used to identify the argument (required).
Definition Types.hpp:107
SoapySDR::ArgInfo::Type
Type
The data type of the argument (required).
Definition Types.hpp:126
SoapySDR::ArgInfo::INT
@ INT
Definition Types.hpp:126
SoapySDR::ArgInfo::BOOL
@ BOOL
Definition Types.hpp:126
SoapySDR::ArgInfo::FLOAT
@ FLOAT
Definition Types.hpp:126
SoapySDR::ArgInfo::STRING
@ STRING
Definition Types.hpp:126
SoapySDR::Range
Definition Types.hpp:67
SoapySDR::Range::maximum
double maximum(void) const
Get the range maximum.
Definition Types.hpp:160
SoapySDR::Range::Range
Range(const double minimum, const double maximum, const double step=0.0)
Create a min/max range.
SoapySDR::Range::minimum
double minimum(void) const
Get the range minimum.
Definition Types.hpp:155
SoapySDR::Range::step
double step(void) const
Get the range step size.
Definition Types.hpp:165
SoapySDR::Range::Range
Range(void)
Create an empty range (0.0, 0.0).
SoapySDR::Detail
Definition Types.hpp:171
SoapySDR::Detail::StringToSetting
std::enable_if< std::is_same< Type, bool >::value, Type >::type StringToSetting(const std::string &s)
Definition Types.hpp:177
SoapySDR::Detail::SettingToString
std::string SettingToString(const bool &s)
Definition Types.hpp:223
SoapySDR
Definition ConverterPrimitives.hpp:15
SoapySDR::KwargsFromString
SOAPY_SDR_API Kwargs KwargsFromString(const std::string &markup)
SoapySDR::RangeList
std::vector< Range > RangeList
Definition Types.hpp:94
SoapySDR::ArgInfoList
std::vector< ArgInfo > ArgInfoList
Definition Types.hpp:151
SoapySDR::StringToSetting
Type StringToSetting(const std::string &s)
Definition Types.hpp:247
SoapySDR::KwargsToString
SOAPY_SDR_API std::string KwargsToString(const Kwargs &args)
SoapySDR::Kwargs
std::map< std::string, std::string > Kwargs
Typedef for a dictionary of key-value string arguments.
Definition Types.hpp:26
SoapySDR::KwargsList
std::vector< Kwargs > KwargsList
Typedef for a list of key-word dictionaries.
Definition Types.hpp:41
SoapySDR::SettingToString
std::string SettingToString(const Type &s)
Definition Types.hpp:253