File: Types.hpp

package info (click to toggle)
soapysdr 0.8.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 948 kB
  • sloc: cpp: 5,378; ansic: 471; python: 311; sh: 21; makefile: 18
file content (249 lines) | stat: -rw-r--r-- 5,946 bytes parent folder | download | duplicates (3)
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
///
/// \file SoapySDR/Types.hpp
///
/// Misc data type definitions used in the API.
///
/// \copyright
/// Copyright (c) 2014-2021 Josh Blum
/// SPDX-License-Identifier: BSL-1.0
///

#pragma once
#include <SoapySDR/Config.hpp>
#include <SoapySDR/Types.h>
#include <type_traits>
#include <vector>
#include <stdexcept>
#include <string>
#include <map>

namespace SoapySDR
{

//! Typedef for a dictionary of key-value string arguments
typedef std::map<std::string, std::string> Kwargs;

/*!
 * Convert a markup string to a key-value map.
 * The markup format is: "key0=value0, key1=value1"
 */
SOAPY_SDR_API Kwargs KwargsFromString(const std::string &markup);

/*!
 * Convert a key-value map to a markup string.
 * The markup format is: "key0=value0, key1=value1"
 */
SOAPY_SDR_API std::string KwargsToString(const Kwargs &args);

//! Typedef for a list of key-word dictionaries
typedef std::vector<Kwargs> KwargsList;

/*!
 * Convert a string to the specified type.
 * Supports bools, integers, floats, and strings
 * \tparam Type the specified output type
 * \param s the setting value as a string
 * \return the value converted to Type
 */
template <typename Type>
Type StringToSetting(const std::string &s);

/*!
 * Convert an arbitrary type to a string.
 * Supports bools, integers, floats, and strings
 * \tparam Type the type of the input argument
 * \param s the setting value as the specified type
 * \return the value converted to a string
 */
template <typename Type>
std::string SettingToString(const Type &s);

/*!
 * A simple min/max numeric range pair
 */
class SOAPY_SDR_API Range
{
public:

    //! Create an empty range (0.0, 0.0)
    Range(void);

    //! Create a min/max range
    Range(const double minimum, const double maximum, const double step=0.0);

    //! Get the range minimum
    double minimum(void) const;

    //! Get the range maximum
    double maximum(void) const;

    //! Get the range step size
    double step(void) const;

private:
    double _min, _max, _step;
};

/*!
 * Typedef for a list of min/max range pairs.
 * Overall minimum: rl.front().minimum();
 * Overall maximum: rl.back().maximum();
 */
typedef std::vector<Range> RangeList;

/*!
 * Argument info describes a key/value argument.
 */
class SOAPY_SDR_API ArgInfo
{
public:

    //! Default constructor
    ArgInfo(void);

    //! The key used to identify the argument (required)
    std::string key;

    /*!
     * The default value of the argument when not specified (required)
     * Numbers should use standard floating point and integer formats.
     * Boolean values should be represented as "true" and  "false".
     */
    std::string value;

    //! The displayable name of the argument (optional, use key if empty)
    std::string name;

    //! A brief description about the argument (optional)
    std::string description;

    //! The units of the argument: dB, Hz, etc (optional)
    std::string units;

    //! The data type of the argument (required)
    enum Type {BOOL, INT, FLOAT, STRING} type;

    /*!
     * The range of possible numeric values (optional)
     * When specified, the argument should be restricted to this range.
     * The range is only applicable to numeric argument types.
     */
    Range range;

    /*!
     * A discrete list of possible values (optional)
     * When specified, the argument should be restricted to this options set.
     */
    std::vector<std::string> options;

    /*!
     * A discrete list of displayable names for the enumerated options (optional)
     * When not specified, the option value itself can be used as a display name.
     */
    std::vector<std::string> optionNames;
};

/*!
 * Typedef for a list of Argument infos.
 */
typedef std::vector<ArgInfo> ArgInfoList;

}

inline double SoapySDR::Range::minimum(void) const
{
    return _min;
}

inline double SoapySDR::Range::maximum(void) const
{
    return _max;
}

inline double SoapySDR::Range::step(void) const
{
    return _step;
}

namespace SoapySDR {
namespace Detail {


//private implementation details for settings converters

template <typename Type>
typename std::enable_if<std::is_same<Type, bool>::value, Type>::type StringToSetting(const std::string &s)
{
  if (s.empty() or s == SOAPY_SDR_FALSE) {
    return false;
  }
  if (s == SOAPY_SDR_TRUE) {
    return true;
  }
  try {
    // C++: Float conversion to bool is unambiguous
    return std::stod(s);
  } catch (std::invalid_argument&) {
  }
  // other values are true
  return true;
}

template <typename Type>
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)
{
    return Type(std::stoll(s));
}

template <typename Type>
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)
{
    return Type(std::stoull(s));
}

template <typename Type>
typename std::enable_if<std::is_floating_point<Type>::value, Type>::type StringToSetting(const std::string &s)
{
    return Type(std::stod(s));
}

template <typename Type>
typename std::enable_if<std::is_same<typename std::decay<Type>::type, std::string>::value, Type>::type StringToSetting(const std::string &s)
{
    return s;
}

inline std::string SettingToString(const bool &s)
{
    return s?SOAPY_SDR_TRUE:SOAPY_SDR_FALSE;
}

inline std::string SettingToString(const char *s)
{
    return s;
}

inline std::string SettingToString(const std::string &s)
{
    return s;
}

template <typename Type>
std::string SettingToString(const Type &s)
{
    return std::to_string(s);
}

}}

template <typename Type>
Type SoapySDR::StringToSetting(const std::string &s)
{
    return SoapySDR::Detail::StringToSetting<Type>(s);
}

template <typename Type>
std::string SoapySDR::SettingToString(const Type &s)
{
    return SoapySDR::Detail::SettingToString(s);
}