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
10
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
20namespace 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
48template <typename Type>
49Type StringToSetting(const std::string &s);
50
58template <typename Type>
59std::string SettingToString(const Type &s);
60
64 class SOAPY_SDR_API Range
65{
66public:
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
83private:
84 double _min, _max, _step;
85};
86
92 typedef std::vector<Range> RangeList;
93
97 class SOAPY_SDR_API ArgInfo
98{
99public:
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
131 Range range;
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
168namespace SoapySDR {
169 namespace Detail {
170
171
172//private implementation details for settings converters
173
174template <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
192template <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
198template <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
204template <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
210template <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{
218 return s?SOAPY_SDR_TRUE:SOAPY_SDR_FALSE;
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
231template <typename Type>
232std::string SettingToString(const Type &s)
233{
234 return std::to_string(s);
235}
236
237}}
238
239template <typename Type>
240Type SoapySDR::StringToSetting(const std::string &s)
241{
242 return SoapySDR::Detail::StringToSetting<Type>(s);
243}
244
245template <typename Type>
246std::string SoapySDR::SettingToString(const Type &s)
247{
248 return SoapySDR::Detail::SettingToString(s);
249}
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:121
SoapySDR::ArgInfo::range
Range range
Definition Types.hpp:131
SoapySDR::ArgInfo::options
std::vector< std::string > options
Definition Types.hpp:137
SoapySDR::ArgInfo::description
std::string description
A brief description about the argument (optional).
Definition Types.hpp:118
SoapySDR::ArgInfo::optionNames
std::vector< std::string > optionNames
Definition Types.hpp:143
SoapySDR::ArgInfo::value
std::string value
Definition Types.hpp:112
SoapySDR::ArgInfo::name
std::string name
The displayable name of the argument (optional, use key if empty).
Definition Types.hpp:115
SoapySDR::ArgInfo::key
std::string key
The key used to identify the argument (required).
Definition Types.hpp:105
SoapySDR::ArgInfo::Type
Type
The data type of the argument (required).
Definition Types.hpp:124
SoapySDR::ArgInfo::INT
@ INT
Definition Types.hpp:124
SoapySDR::ArgInfo::BOOL
@ BOOL
Definition Types.hpp:124
SoapySDR::ArgInfo::FLOAT
@ FLOAT
Definition Types.hpp:124
SoapySDR::ArgInfo::STRING
@ STRING
Definition Types.hpp:124
SoapySDR::Range
Definition Types.hpp:65
SoapySDR::Range::maximum
double maximum(void) const
Get the range maximum.
Definition Types.hpp:158
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:153
SoapySDR::Range::step
double step(void) const
Get the range step size.
Definition Types.hpp:163
SoapySDR::Range::Range
Range(void)
Create an empty range (0.0, 0.0).
SoapySDR::Detail
Definition Types.hpp:169
SoapySDR::Detail::StringToSetting
std::enable_if< std::is_same< Type, bool >::value, Type >::type StringToSetting(const std::string &s)
Definition Types.hpp:175
SoapySDR::Detail::SettingToString
std::string SettingToString(const bool &s)
Definition Types.hpp:216
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:92
SoapySDR::ArgInfoList
std::vector< ArgInfo > ArgInfoList
Definition Types.hpp:149
SoapySDR::StringToSetting
Type StringToSetting(const std::string &s)
Definition Types.hpp:240
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:24
SoapySDR::KwargsList
std::vector< Kwargs > KwargsList
Typedef for a list of key-word dictionaries.
Definition Types.hpp:39
SoapySDR::SettingToString
std::string SettingToString(const Type &s)
Definition Types.hpp:246