From: Gerhard Sittig Date: Sun, 11 Jun 2017 07:54:52 +0000 (+0200) Subject: C++ binding: Allow to re-use ConfigKey::parse_string() for Option class X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=61a6d983bd37fcd035439dc5f2671a290ef46057;p=libsigrok.git 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. --- diff --git a/bindings/cxx/ConfigKey_methods.cpp b/bindings/cxx/ConfigKey_methods.cpp index 54af62e8..be70b06d 100644 --- a/bindings/cxx/ConfigKey_methods.cpp +++ b/bindings/cxx/ConfigKey_methods.cpp @@ -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); +} diff --git a/bindings/cxx/ConfigKey_methods.hpp b/bindings/cxx/ConfigKey_methods.hpp index f759cc4e..bbc7ce81 100644 --- a/bindings/cxx/ConfigKey_methods.hpp +++ b/bindings/cxx/ConfigKey_methods.hpp @@ -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; diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index e4340fdf..d292be30 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -1483,6 +1483,28 @@ vector 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) { diff --git a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp index b1ab7a5b..00a19b33 100644 --- a/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp +++ b/bindings/cxx/include/libsigrokcxx/libsigrokcxx.hpp @@ -917,6 +917,8 @@ public: Glib::VariantBase default_value() const; /** Possible values for this option, if a limited set. */ vector 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 structure_array);