]> sigrok.org Git - libsigrok.git/blame - bindings/cxx/ConfigKey_methods.cpp
C++: Use move() and avoid passing containers by value
[libsigrok.git] / bindings / cxx / ConfigKey_methods.cpp
CommitLineData
3b161085 1const DataType *ConfigKey::data_type() const
c23c8659 2{
2fb60e23 3 const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, _id);
c23c8659
ML
4 if (!info)
5 throw Error(SR_ERR_NA);
6 return DataType::get(info->datatype);
7}
8
3b161085 9string ConfigKey::identifier() const
c23c8659 10{
2fb60e23 11 const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, _id);
c23c8659
ML
12 if (!info)
13 throw Error(SR_ERR_NA);
14 return valid_string(info->id);
15}
16
3b161085 17string ConfigKey::description() const
c23c8659 18{
2fb60e23 19 const struct sr_key_info *info = sr_key_info_get(SR_KEY_CONFIG, _id);
c23c8659
ML
20 if (!info)
21 throw Error(SR_ERR_NA);
22 return valid_string(info->name);
23}
24
0bc1a761 25const ConfigKey *ConfigKey::get_by_identifier(string identifier)
c23c8659 26{
2fb60e23 27 const struct sr_key_info *info = sr_key_info_name_get(SR_KEY_CONFIG, identifier.c_str());
c23c8659
ML
28 if (!info)
29 throw Error(SR_ERR_ARG);
30 return get(info->key);
31}
32
6ec6c43b 33#include <config.h>
d92de05a
MC
34
35#ifndef HAVE_STOI_STOD
36
37/* Fallback implementation of stoi and stod */
38
39#include <cstdlib>
40#include <cerrno>
41#include <stdexcept>
42#include <limits>
43
44static inline int stoi( const std::string& str )
45{
46 char *endptr;
47 errno = 0;
48 const long ret = std::strtol(str.c_str(), &endptr, 10);
49 if (endptr == str.c_str())
50 throw std::invalid_argument("stoi");
51 else if (errno == ERANGE ||
52 ret < std::numeric_limits<int>::min() ||
53 ret > std::numeric_limits<int>::max())
54 throw std::out_of_range("stoi");
55 else
56 return ret;
57}
58
59static inline double stod( const std::string& str )
60{
61 char *endptr;
62 errno = 0;
63 const double ret = std::strtod(str.c_str(), &endptr);
64 if (endptr == str.c_str())
65 throw std::invalid_argument("stod");
66 else if (errno == ERANGE)
67 throw std::out_of_range("stod");
68 else
69 return ret;
70}
71#endif
72
c23c8659
ML
73Glib::VariantBase ConfigKey::parse_string(string value) const
74{
75 GVariant *variant;
76 uint64_t p, q;
77
3b161085 78 switch (data_type()->id())
c23c8659
ML
79 {
80 case SR_T_UINT64:
81 check(sr_parse_sizestring(value.c_str(), &p));
82 variant = g_variant_new_uint64(p);
83 break;
84 case SR_T_STRING:
85 variant = g_variant_new_string(value.c_str());
86 break;
87 case SR_T_BOOL:
88 variant = g_variant_new_boolean(sr_parse_boolstring(value.c_str()));
89 break;
90 case SR_T_FLOAT:
91 variant = g_variant_new_double(stod(value));
92 break;
93 case SR_T_RATIONAL_PERIOD:
94 check(sr_parse_period(value.c_str(), &p, &q));
95 variant = g_variant_new("(tt)", p, q);
96 break;
97 case SR_T_RATIONAL_VOLT:
98 check(sr_parse_voltage(value.c_str(), &p, &q));
99 variant = g_variant_new("(tt)", p, q);
100 break;
101 case SR_T_INT32:
102 variant = g_variant_new_int32(stoi(value));
103 break;
104 default:
105 throw Error(SR_ERR_BUG);
106 }
107
7009a392 108 return Glib::VariantBase(variant, false);
c23c8659
ML
109}
110