C++ binding: Allow to re-use ConfigKey::parse_string() for Option class
Split the data type detection from the actual data type conversion in the ConfigKey::parse_string() method. Allow the Option class to re-use the public ConfigKey method, to share the maximum amount of data type conversion code for both Driver and InputFormat option specs. This is the least intrusive yet most reliable and consistent approach in the libsigrok C++ binding that allows applications to support driver scan and input file format options from command line arguments.
This commit is contained in:
parent
276d7b18bb
commit
61a6d983bd
|
@ -70,12 +70,12 @@ static inline double stod( const std::string& str )
|
|||
}
|
||||
#endif
|
||||
|
||||
Glib::VariantBase ConfigKey::parse_string(string value) const
|
||||
Glib::VariantBase ConfigKey::parse_string(string value, enum sr_datatype dt)
|
||||
{
|
||||
GVariant *variant;
|
||||
uint64_t p, q;
|
||||
|
||||
switch (data_type()->id())
|
||||
switch (dt)
|
||||
{
|
||||
case SR_T_UINT64:
|
||||
check(sr_parse_sizestring(value.c_str(), &p));
|
||||
|
@ -116,3 +116,8 @@ Glib::VariantBase ConfigKey::parse_string(string value) const
|
|||
return Glib::VariantBase(variant, false);
|
||||
}
|
||||
|
||||
Glib::VariantBase ConfigKey::parse_string(string value) const
|
||||
{
|
||||
enum sr_datatype dt = (enum sr_datatype)(data_type()->id());
|
||||
return parse_string(value, dt);
|
||||
}
|
||||
|
|
|
@ -7,4 +7,5 @@
|
|||
/** Get configuration key by string identifier. */
|
||||
static const ConfigKey *get_by_identifier(string identifier);
|
||||
/** Parse a string argument into the appropriate type for this key. */
|
||||
static Glib::VariantBase parse_string(string value, enum sr_datatype dt);
|
||||
Glib::VariantBase parse_string(string value) const;
|
||||
|
|
|
@ -1483,6 +1483,28 @@ vector<Glib::VariantBase> Option::values() const
|
|||
return result;
|
||||
}
|
||||
|
||||
Glib::VariantBase Option::parse_string(string value)
|
||||
{
|
||||
enum sr_datatype dt;
|
||||
Glib::VariantBase dflt = default_value();
|
||||
GVariant *tmpl = dflt.gobj();
|
||||
|
||||
if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_UINT64)) {
|
||||
dt = SR_T_UINT64;
|
||||
} else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_STRING)) {
|
||||
dt = SR_T_STRING;
|
||||
} else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_BOOLEAN)) {
|
||||
dt = SR_T_BOOL;
|
||||
} else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_DOUBLE)) {
|
||||
dt = SR_T_FLOAT;
|
||||
} else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_INT32)) {
|
||||
dt = SR_T_INT32;
|
||||
} else {
|
||||
throw Error(SR_ERR_BUG);
|
||||
}
|
||||
return ConfigKey::parse_string(value, dt);
|
||||
}
|
||||
|
||||
OutputFormat::OutputFormat(const struct sr_output_module *structure) :
|
||||
_structure(structure)
|
||||
{
|
||||
|
|
|
@ -917,6 +917,8 @@ public:
|
|||
Glib::VariantBase default_value() const;
|
||||
/** Possible values for this option, if a limited set. */
|
||||
vector<Glib::VariantBase> values() const;
|
||||
/** Parse a string argument into the appropriate type for this option. */
|
||||
Glib::VariantBase parse_string(string value);
|
||||
private:
|
||||
Option(const struct sr_option *structure,
|
||||
shared_ptr<const struct sr_option *> structure_array);
|
||||
|
|
Loading…
Reference in New Issue