]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
C++: handle uint32_t SR_CONF keys which contain key capabilities
[libsigrok.git] / bindings / cxx / classes.cpp
index ec9b6d8f289e8bddd5825fe3b3b33601951f066a..7a6daa92df7c664c8251f18133fc69586a5aa740 100644 (file)
@@ -20,6 +20,7 @@
 #include "libsigrok/libsigrok.hpp"
 
 #include <sstream>
+#include <cmath>
 
 namespace sigrok
 {
@@ -255,8 +256,6 @@ Driver::Driver(struct sr_dev_driver *structure) :
 
 Driver::~Driver()
 {
-       for (auto device : _devices)
-               delete device;
 }
 
 string Driver::name()
@@ -279,11 +278,6 @@ vector<shared_ptr<HardwareDevice>> Driver::scan(
                _initialized = true;
        }
 
-       /* Clear all existing instances. */
-       for (auto device : _devices)
-               delete device;
-       _devices.clear();
-
        /* Translate scan options to GSList of struct sr_config pointers. */
        GSList *option_list = NULL;
        for (auto entry : options)
@@ -302,20 +296,20 @@ vector<shared_ptr<HardwareDevice>> Driver::scan(
        /* Free option list. */
        g_slist_free_full(option_list, g_free);
 
+
        /* Create device objects. */
+       vector<shared_ptr<HardwareDevice>> result;
        for (GSList *device = device_list; device; device = device->next)
        {
                auto sdi = (struct sr_dev_inst *) device->data;
-               _devices.push_back(new HardwareDevice(this, sdi));
+               result.push_back(shared_ptr<HardwareDevice>(
+                       new HardwareDevice(shared_from_this(), sdi),
+                       HardwareDevice::Deleter()));
        }
 
        /* Free GSList returned from scan. */
        g_slist_free(device_list);
 
-       /* Create list of shared pointers to device instances for return. */
-       vector<shared_ptr<HardwareDevice>> result;
-       for (auto device : _devices)
-               result.push_back(device->get_shared_pointer(_parent));
        return result;
 }
 
@@ -362,18 +356,18 @@ vector<const ConfigKey *> Configurable::config_keys(const ConfigKey *key)
 {
        GVariant *gvar_opts;
        gsize num_opts;
-       const int32_t *opts;
+       const uint32_t *opts;
        vector<const ConfigKey *> result;
 
        check(sr_config_list(
                config_driver, config_sdi, config_channel_group,
                key->id(), &gvar_opts));
 
-       opts = (const int32_t *) g_variant_get_fixed_array(
-               gvar_opts, &num_opts, sizeof(int32_t));
+       opts = (const uint32_t *) g_variant_get_fixed_array(
+               gvar_opts, &num_opts, sizeof(uint32_t));
 
        for (gsize i = 0; i < num_opts; i++)
-               result.push_back(ConfigKey::get(opts[i]));
+               result.push_back(ConfigKey::get(opts[i] & SR_CONF_MASK));
 
        g_variant_unref(gvar_opts);
 
@@ -385,18 +379,18 @@ bool Configurable::config_check(const ConfigKey *key,
 {
        GVariant *gvar_opts;
        gsize num_opts;
-       const int32_t *opts;
+       const uint32_t *opts;
 
        if (sr_config_list(config_driver, config_sdi, config_channel_group,
                        index_key->id(), &gvar_opts) != SR_OK)
                return false;
 
-       opts = (const int32_t *) g_variant_get_fixed_array(
-               gvar_opts, &num_opts, sizeof(int32_t));
+       opts = (const uint32_t *) g_variant_get_fixed_array(
+               gvar_opts, &num_opts, sizeof(uint32_t));
 
        for (gsize i = 0; i < num_opts; i++)
        {
-               if (opts[i] == key->id())
+               if ((opts[i] & SR_CONF_MASK) == key->id())
                {
                        g_variant_unref(gvar_opts);
                        return true;
@@ -500,8 +494,9 @@ void Device::close()
        check(sr_dev_close(_structure));
 }
 
-HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) :
-       ParentOwned(structure),
+HardwareDevice::HardwareDevice(shared_ptr<Driver> driver,
+               struct sr_dev_inst *structure) :
+       UserOwned(structure),
        Device(structure),
        _driver(driver)
 {
@@ -518,7 +513,7 @@ shared_ptr<Device> HardwareDevice::get_shared_from_this()
 
 shared_ptr<Driver> HardwareDevice::driver()
 {
-       return _driver->get_shared_pointer(_parent);
+       return _driver;
 }
 
 Channel::Channel(struct sr_channel *structure) :