]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
C++ binding: Allow to re-use ConfigKey::parse_string() for Option class
[libsigrok.git] / bindings / cxx / classes.cpp
index 1c5b97b0149de12dc832741de715cf0c9d7ad0fd..d292be304bac01401827ae6e9f967cc5afa175e5 100644 (file)
@@ -768,7 +768,7 @@ vector<shared_ptr<Channel>> ChannelGroup::channels()
        return result;
 }
 
-Trigger::Trigger(shared_ptr<Context> context, string name) : 
+Trigger::Trigger(shared_ptr<Context> context, string name) :
        _structure(sr_trigger_new(name.c_str())),
        _context(move(context))
 {
@@ -812,7 +812,7 @@ TriggerStage::TriggerStage(struct sr_trigger_stage *structure) :
 TriggerStage::~TriggerStage()
 {
 }
-       
+
 int TriggerStage::number() const
 {
        return _structure->stage;
@@ -1212,6 +1212,11 @@ void *Analog::data_pointer()
        return _structure->data;
 }
 
+void Analog::get_data_as_float(float *dest)
+{
+       check(sr_analog_to_float(_structure, dest));
+}
+
 unsigned int Analog::num_samples() const
 {
        return _structure->num_samples;
@@ -1227,6 +1232,58 @@ vector<shared_ptr<Channel>> Analog::channels()
        return result;
 }
 
+unsigned int Analog::unitsize() const
+{
+       return _structure->encoding->unitsize;
+}
+
+bool Analog::is_signed() const
+{
+       return _structure->encoding->is_signed;
+}
+
+bool Analog::is_float() const
+{
+       return _structure->encoding->is_float;
+}
+
+bool Analog::is_bigendian() const
+{
+       return _structure->encoding->is_bigendian;
+}
+
+int Analog::digits() const
+{
+       return _structure->encoding->digits;
+}
+
+bool Analog::is_digits_decimal() const
+{
+       return _structure->encoding->is_digits_decimal;
+}
+
+shared_ptr<Rational> Analog::scale()
+{
+       unique_ptr<Rational> scale;
+       scale.reset(new Rational(&(_structure->encoding->scale)));
+
+       if (scale)
+               return scale->share_owned_by(shared_from_this());
+       else
+               throw Error(SR_ERR_NA);
+}
+
+shared_ptr<Rational> Analog::offset()
+{
+       unique_ptr<Rational> offset;
+       offset.reset(new Rational(&(_structure->encoding->offset)));
+
+       if (offset)
+               return offset->share_owned_by(shared_from_this());
+       else
+               throw Error(SR_ERR_NA);
+}
+
 const Quantity *Analog::mq() const
 {
        return Quantity::get(_structure->meaning->mq);
@@ -1242,6 +1299,36 @@ vector<const QuantityFlag *> Analog::mq_flags() const
        return QuantityFlag::flags_from_mask(_structure->meaning->mqflags);
 }
 
+Rational::Rational(const struct sr_rational *structure) :
+       _structure(structure)
+{
+}
+
+Rational::~Rational()
+{
+}
+
+shared_ptr<Rational> Rational::share_owned_by(shared_ptr<Analog> _parent)
+{
+       return static_pointer_cast<Rational>(
+               ParentOwned::share_owned_by(_parent));
+}
+
+int64_t Rational::numerator() const
+{
+       return _structure->p;
+}
+
+uint64_t Rational::denominator() const
+{
+       return _structure->q;
+}
+
+float Rational::value() const
+{
+       return (float)(_structure->p) / (float)(_structure->q);
+}
+
 InputFormat::InputFormat(const struct sr_input_module *structure) :
        _structure(structure)
 {
@@ -1320,7 +1407,7 @@ void Input::send(void *data, size_t length)
 {
        auto gstr = g_string_new_len(static_cast<char *>(data), length);
        auto ret = sr_input_send(_structure, gstr);
-       g_string_free(gstr, false);
+       g_string_free(gstr, true);
        check(ret);
 }
 
@@ -1329,6 +1416,11 @@ void Input::end()
        check(sr_input_end(_structure));
 }
 
+void Input::reset()
+{
+       check(sr_input_reset(_structure));
+}
+
 Input::~Input()
 {
        sr_input_free(_structure);
@@ -1391,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)
 {