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
|
//
// Copyright 2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#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/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/assign/list_of.hpp>
#include <vector>
#include <string>
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() + "=" + boost::lexical_cast<std::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
*
* NOTE: The constraint on enum_t is that the values must start with
* 0 and be sequential
*/
template<typename enum_t>
class enum_arg : public generic_arg {
public:
enum_arg(
const std::string& name,
const enum_t default_value,
const std::vector<std::string>& values) :
generic_arg(name), _str_values(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, bool assert_invalid = true) {
std::string valid_values_str;
for (size_t i = 0; i < _str_values.size(); i++) {
if (boost::algorithm::to_lower_copy(str_rep) ==
boost::algorithm::to_lower_copy(_str_values[i]))
{
valid_values_str += ((i==0)?"":", ") + _str_values[i];
set(static_cast<enum_t>(static_cast<int>(i)));
return;
}
}
//If we reach here then, the string enum value was invalid
if (assert_invalid) {
throw uhd::value_error(str(boost::format(
"Invalid device arg value: %s=%s (Valid: {%s})") %
key() % str_rep % valid_values_str
));
}
}
inline virtual std::string to_string() const {
size_t index = static_cast<size_t>(static_cast<int>(_value));
UHD_ASSERT_THROW(index < _str_values.size());
return key() + "=" + _str_values[index];
}
private:
enum_t _value;
std::vector<std::string> _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 = (boost::lexical_cast<int>(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;
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() %
boost::lexical_cast<std::string>(min) % boost::lexical_cast<std::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;
BOOST_FOREACH(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++) {
valid_values_str += ((i==0)?"":", ") + boost::lexical_cast<std::string>(valid_values[i]);
throw uhd::value_error(str(boost::format(
"Invalid device arg value: %s (Valid: {%s})") %
arg.to_string() % valid_values_str
));
}
}
}
};
}} //namespaces
#endif /* INCLUDED_LIBUHD_USRP_COMMON_CONSTRAINED_DEV_ARGS_HPP */
|