1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
//
// Copyright 2014 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#ifndef INCLUDED_LIBUHD_USRP_COMMON_CONSTRAINED_DEV_ARGS_HPP
#define INCLUDED_LIBUHD_USRP_COMMON_CONSTRAINED_DEV_ARGS_HPP
#include <uhd/types/device_addr.hpp>
#include <uhd/exception.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/assign/list_of.hpp>
#include <vector>
#include <string>
#include <sstream>
#include <unordered_map>
namespace uhd {
namespace usrp {
/*!
* constrained_device_args_t provides a base and utilities to
* map key=value pairs passed in through the device creation
* args interface (device_addr_t).
*
* Inherit from this class to create typed device specific
* arguments and use the base class methods to handle parsing
* the device_addr or any key=value string to populate the args
*
* This file contains a library of different types of args the
* the user can pass in. The library can be extended to support
* non-intrinsic types by the client.
*
*/
class constrained_device_args_t {
public: //Types
/*!
* Base argument type. All other arguments inherit from this.
*/
class generic_arg {
public:
generic_arg(const std::string& key): _key(key) {}
inline const std::string& key() const { return _key; }
inline virtual std::string to_string() const = 0;
private:
std::string _key;
};
/*!
* String argument type. Can be case sensitive or insensitive
*/
template<bool case_sensitive>
class str_arg : public generic_arg {
public:
str_arg(const std::string& name, const std::string& default_value) :
generic_arg(name) { set(default_value); }
inline void set(const std::string& value) {
_value = case_sensitive ? value : boost::algorithm::to_lower_copy(value);
}
inline const std::string& get() const {
return _value;
}
inline void parse(const std::string& str_rep) {
set(str_rep);
}
inline virtual std::string to_string() const {
return key() + "=" + get();
}
inline bool operator==(const std::string& rhs) const {
return get() == boost::algorithm::to_lower_copy(rhs);
}
private:
std::string _value;
};
typedef str_arg<false> str_ci_arg;
typedef str_arg<true> str_cs_arg;
/*!
* Numeric argument type. The template type data_t allows the
* client to constrain the type of the number.
*/
template<typename data_t>
class num_arg : public generic_arg {
public:
num_arg(const std::string& name, const data_t default_value) :
generic_arg(name) { set(default_value); }
inline void set(const data_t value) {
_value = value;
}
inline const data_t get() const {
return _value;
}
inline void parse(const std::string& str_rep) {
try {
_value = boost::lexical_cast<data_t>(str_rep);
} catch (std::exception& ex) {
throw uhd::value_error(str(boost::format(
"Error parsing numeric parameter %s: %s.") %
key() % ex.what()
));
}
}
inline virtual std::string to_string() const {
return key() + "=" + std::to_string(get());
}
private:
data_t _value;
};
/*!
* Enumeration argument type. The template type enum_t allows the
* client to use their own enum and specify a string mapping for
* the values of the enum
*/
template<typename enum_t>
class enum_arg : public generic_arg {
public:
enum_arg(
const std::string& name,
const enum_t default_value,
const std::unordered_map<std::string, enum_t>& values) :
generic_arg(name), _str_values(_enum_map_to_lowercase<enum_t>(values))
{
set(default_value);
}
inline void set(const enum_t value) {
_value = value;
}
inline const enum_t get() const {
return _value;
}
inline void parse(
const std::string& str_rep,
const bool assert_invalid = true
) {
const std::string str_rep_lowercase =
boost::algorithm::to_lower_copy(str_rep);
if (_str_values.count(str_rep_lowercase) == 0) {
if (assert_invalid) {
std::string valid_values_str = "";
for (const auto &value : _str_values) {
valid_values_str +=
(valid_values_str.empty()?"":", ")
+ value.first;
}
throw uhd::value_error(str(boost::format(
"Invalid device arg value: %s=%s (Valid: {%s})") %
key() % str_rep % valid_values_str
));
} else {
return;
}
}
set(_str_values.at(str_rep_lowercase));
}
inline virtual std::string to_string() const {
std::string repr;
for (const auto& value : _str_values) {
if (value.second == _value) {
repr = value.first;
break;
}
}
UHD_ASSERT_THROW(!repr.empty());
return key() + "=" + repr;
}
private:
enum_t _value;
const std::unordered_map<std::string, enum_t> _str_values;
};
/*!
* Boolean argument type.
*/
class bool_arg : public generic_arg {
public:
bool_arg(const std::string& name, const bool default_value) :
generic_arg(name) { set(default_value); }
inline void set(const bool value) {
_value = value;
}
inline bool get() const {
return _value;
}
inline void parse(const std::string& str_rep) {
try {
_value = (std::stoi(str_rep) != 0);
} catch (std::exception& ex) {
if (str_rep.empty()) {
//If str_rep is empty then the device_addr was set
//without a value which means that the user "set" the flag
_value = true;
} else if (boost::algorithm::to_lower_copy(str_rep) == "true" ||
boost::algorithm::to_lower_copy(str_rep) == "yes" ||
boost::algorithm::to_lower_copy(str_rep) == "y") {
_value = true;
} else if (boost::algorithm::to_lower_copy(str_rep) == "false" ||
boost::algorithm::to_lower_copy(str_rep) == "no" ||
boost::algorithm::to_lower_copy(str_rep) == "n") {
_value = false;
} else {
throw uhd::value_error(str(boost::format(
"Error parsing boolean parameter %s: %s.") %
key() % ex.what()
));
}
}
}
inline virtual std::string to_string() const {
return key() + "=" + (get() ? "true" : "false");
}
private:
bool _value;
};
public: //Methods
constrained_device_args_t() {}
virtual ~constrained_device_args_t() {}
void parse(const std::string& str_args) {
device_addr_t dev_args(str_args);
_parse(dev_args);
}
void parse(const device_addr_t& dev_args) {
_parse(dev_args);
}
inline virtual std::string to_string() const = 0;
template <typename arg_type>
void parse_arg_default(
const device_addr_t& dev_args,
arg_type& constrained_arg
) {
if (dev_args.has_key(constrained_arg.key())) {
constrained_arg.parse(dev_args[constrained_arg.key()]);
}
}
protected: //Methods
//Override _parse to provide an implementation to parse all
//client specific device args
virtual void _parse(const device_addr_t& dev_args) = 0;
/*!
* Utility: Ensure that the value of the device arg is between min and max
*/
template<typename num_data_t>
static inline void _enforce_range(const num_arg<num_data_t>& arg, const num_data_t& min, const num_data_t& max) {
if (arg.get() > max || arg.get() < min) {
throw uhd::value_error(str(boost::format(
"Invalid device arg value: %s (Minimum: %s, Maximum: %s)") %
arg.to_string() %
std::to_string(min) % std::to_string(max)));
}
}
/*!
* Utility: Ensure that the value of the device arg is is contained in valid_values
*/
template<typename arg_t, typename data_t>
static inline void _enforce_discrete(const arg_t& arg, const std::vector<data_t>& valid_values) {
bool match = false;
for(const data_t& val: valid_values) {
if (val == arg.get()) {
match = true;
break;
}
}
if (!match) {
std::string valid_values_str;
for (size_t i = 0; i < valid_values.size(); i++) {
std::stringstream valid_values_ss;
valid_values_ss << ((i==0)?"":", ") << valid_values[i];
throw uhd::value_error(str(boost::format(
"Invalid device arg value: %s (Valid: {%s})") %
arg.to_string() % valid_values_ss.str()
));
}
}
}
//! Helper for enum_arg: Create a new map where keys are converted to
// lowercase.
template<typename enum_t>
static std::unordered_map<std::string, enum_t> _enum_map_to_lowercase(
const std::unordered_map<std::string, enum_t>& in_map
) {
std::unordered_map<std::string, enum_t> new_map;
for (const auto& str_to_enum : in_map) {
new_map.insert(
std::pair<std::string, enum_t>(
boost::algorithm::to_lower_copy(str_to_enum.first),
str_to_enum.second
)
);
}
return new_map;
}
};
}} //namespaces
#endif /* INCLUDED_LIBUHD_USRP_COMMON_CONSTRAINED_DEV_ARGS_HPP */
|