From: Martin Ling Date: Thu, 24 Jul 2014 02:22:44 +0000 (+0100) Subject: C++: Add internal lookup to find Channel object from sr_channel *. X-Git-Tag: libsigrok-0.4.0~1205 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4178d9712f3e67f83aa1eb49a5f5299e9ce1515b;p=libsigrok.git C++: Add internal lookup to find Channel object from sr_channel *. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index af3b4840..7b8d8531 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -344,14 +344,14 @@ Device::Device(struct sr_dev_inst *structure) : for (GSList *entry = structure->channels; entry; entry = entry->next) { auto channel = (struct sr_channel *) entry->data; - channels.push_back(new Channel(channel)); + channels[channel] = new Channel(channel); } } Device::~Device() { - for (auto channel : channels) - delete channel; + for (auto entry : channels) + delete entry.second; } string Device::get_vendor() @@ -372,12 +372,18 @@ string Device::get_version() vector> Device::get_channels() { vector> result; - for (auto channel : channels) + for (auto entry : channels) result.push_back(static_pointer_cast( - channel->get_shared_pointer(this))); + entry.second->get_shared_pointer(this))); return result; } +shared_ptr Device::get_channel(struct sr_channel *ptr) +{ + return static_pointer_cast( + channels[ptr]->get_shared_pointer(this)); +} + void Device::open() { check(sr_dev_open(structure)); @@ -465,12 +471,7 @@ ChannelGroup::ChannelGroup(HardwareDevice *device, Configurable(device->structure->driver, device->structure, structure) { for (GSList *entry = structure->channels; entry; entry = entry->next) - { - auto channel = (struct sr_channel *) entry->data; - for (auto device_channel : device->channels) - if (channel == device_channel->structure) - channels.push_back(device_channel); - } + channels.push_back(device->channels[(struct sr_channel *)entry->data]); } ChannelGroup::~ChannelGroup() diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index 65eea9e2..3deb29e6 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -291,7 +291,8 @@ public: protected: Device(struct sr_dev_inst *structure); ~Device(); - vector channels; + shared_ptr get_channel(struct sr_channel *ptr); + map channels; /** Deleter needed to allow shared_ptr use with protected destructor. */ class Deleter {