]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
cxx: Implement more of EnumValue in template.
[libsigrok.git] / bindings / cxx / classes.cpp
index 549e07b0843d92be6a4cc4de2ae156b370821182..3e8acc0abb41d1a4558b117d0dc932f59e67eb95 100644 (file)
@@ -400,7 +400,7 @@ bool Configurable::config_check(const ConfigKey *key,
 
        for (gsize i = 0; i < num_opts; i++)
        {
-               if ((opts[i] & SR_CONF_MASK) == key->id())
+               if ((opts[i] & SR_CONF_MASK) == (uint32_t) key->id())
                {
                        g_variant_unref(gvar_opts);
                        return true;
@@ -413,16 +413,16 @@ bool Configurable::config_check(const ConfigKey *key,
 }
 
 Device::Device(struct sr_dev_inst *structure) :
-       Configurable(structure->driver, structure, NULL),
+       Configurable(sr_dev_inst_driver_get(structure), structure, NULL),
        _structure(structure)
 {
-       for (GSList *entry = structure->channels; entry; entry = entry->next)
+       for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next)
        {
                auto channel = (struct sr_channel *) entry->data;
                _channels[channel] = new Channel(channel);
        }
 
-       for (GSList *entry = structure->channel_groups; entry; entry = entry->next)
+       for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next)
        {
                auto group = (struct sr_channel_group *) entry->data;
                _channel_groups[group->name] = new ChannelGroup(this, group);
@@ -437,39 +437,35 @@ Device::~Device()
                delete entry.second;
 }
 
-string Device::description()
+string Device::vendor()
 {
-       ostringstream s;
-
-       vector<string> parts =
-               {vendor(), model(), version()};
-
-       for (string part : parts)
-               if (part.length() > 0)
-                       s << part;
+       return valid_string(sr_dev_inst_vendor_get(_structure));
+}
 
-       return s.str();
+string Device::model()
+{
+       return valid_string(sr_dev_inst_model_get(_structure));
 }
 
-string Device::vendor()
+string Device::version()
 {
-       return valid_string(_structure->vendor);
+       return valid_string(sr_dev_inst_version_get(_structure));
 }
 
-string Device::model()
+string Device::serial_number()
 {
-       return valid_string(_structure->model);
+       return valid_string(sr_dev_inst_sernum_get(_structure));
 }
 
-string Device::version()
+string Device::connection_id()
 {
-       return valid_string(_structure->version);
+       return valid_string(sr_dev_inst_connid_get(_structure));
 }
 
 vector<shared_ptr<Channel>> Device::channels()
 {
        vector<shared_ptr<Channel>> result;
-       for (auto channel = _structure->channels; channel; channel = channel->next)
+       for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next)
                result.push_back(
                        _channels[(struct sr_channel *) channel->data]->get_shared_pointer(
                                get_shared_from_this()));
@@ -570,7 +566,7 @@ unsigned int Channel::index()
 ChannelGroup::ChannelGroup(Device *device,
                struct sr_channel_group *structure) :
        ParentOwned(structure),
-       Configurable(device->_structure->driver, device->_structure, structure)
+       Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure)
 {
        for (GSList *entry = structure->channels; entry; entry = entry->next)
                _channels.push_back(device->_channels[(struct sr_channel *)entry->data]);
@@ -706,7 +702,7 @@ DatafeedCallbackData::DatafeedCallbackData(Session *session,
 void DatafeedCallbackData::run(const struct sr_dev_inst *sdi,
        const struct sr_datafeed_packet *pkt)
 {
-       auto device = _session->_devices[sdi];
+       auto device = _session->get_device(sdi);
        auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter());
        _callback(device, packet);
 }
@@ -787,6 +783,7 @@ Session::Session(shared_ptr<Context> context) :
 Session::Session(shared_ptr<Context> context, string filename) :
        UserOwned(_structure),
        _context(context),
+       _filename(filename),
        _saving(false)
 {
        check(sr_session_load(filename.c_str(), &_structure));
@@ -795,9 +792,7 @@ Session::Session(shared_ptr<Context> context, string filename) :
        for (GSList *dev = dev_list; dev; dev = dev->next)
        {
                auto sdi = (struct sr_dev_inst *) dev->data;
-               auto device = new SessionDevice(sdi);
-               _devices[sdi] = shared_ptr<SessionDevice>(device,
-                       SessionDevice::Deleter());
+               _owned_devices[sdi] = new SessionDevice(sdi);
        }
        _context->_session = this;
 }
@@ -811,12 +806,26 @@ Session::~Session()
 
        for (auto entry : _source_callbacks)
                delete entry.second;
+
+       for (auto entry : _owned_devices)
+               delete entry.second;
+}
+
+shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi)
+{
+       if (_owned_devices.count(sdi))
+               return static_pointer_cast<Device>(
+                       _owned_devices[sdi]->get_shared_pointer(this));
+       else if (_other_devices.count(sdi))
+               return _other_devices[sdi];
+       else
+               throw Error(SR_ERR_BUG);
 }
 
 void Session::add_device(shared_ptr<Device> device)
 {
        check(sr_session_dev_add(_structure, device->_structure));
-       _devices[device->_structure] = device;
+       _other_devices[device->_structure] = device;
 }
 
 vector<shared_ptr<Device>> Session::devices()
@@ -827,14 +836,14 @@ vector<shared_ptr<Device>> Session::devices()
        for (GSList *dev = dev_list; dev; dev = dev->next)
        {
                auto sdi = (struct sr_dev_inst *) dev->data;
-               result.push_back(_devices[sdi]);
+               result.push_back(get_device(sdi));
        }
        return result;
 }
 
 void Session::remove_devices()
 {
-       _devices.clear();
+       _other_devices.clear();
        check(sr_session_dev_remove_all(_structure));
 }
 
@@ -888,7 +897,7 @@ void Session::append(shared_ptr<Packet> packet)
                        {
                                GVariant *samplerate;
 
-                               check(sr_config_get(packet->_device->_structure->driver,
+                               check(sr_config_get(sr_dev_inst_driver_get(packet->_device->_structure),
                                        packet->_device->_structure, NULL, SR_CONF_SAMPLERATE,
                                        &samplerate));
 
@@ -1034,6 +1043,11 @@ void Session::set_trigger(shared_ptr<Trigger> trigger)
        _trigger = trigger;
 }
 
+string Session::filename()
+{
+       return _filename;
+}
+
 Packet::Packet(shared_ptr<Device> device,
        const struct sr_datafeed_packet *structure) :
        UserOwned(structure),