]> sigrok.org Git - libsigrok.git/commitdiff
C++: Add internal lookup to find Channel object from sr_channel *.
authorMartin Ling <redacted>
Thu, 24 Jul 2014 02:22:44 +0000 (03:22 +0100)
committerMartin Ling <redacted>
Thu, 24 Jul 2014 20:00:45 +0000 (21:00 +0100)
bindings/cxx/classes.cpp
bindings/cxx/include/libsigrok/libsigrok.hpp

index af3b484038d882755d835f430a306d9d88732586..7b8d8531f899a74ca3fa30822933d5d3f3386adf 100644 (file)
@@ -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<shared_ptr<Channel>> Device::get_channels()
 {
        vector<shared_ptr<Channel>> result;
-       for (auto channel : channels)
+       for (auto entry : channels)
                result.push_back(static_pointer_cast<Channel>(
-                       channel->get_shared_pointer(this)));
+                       entry.second->get_shared_pointer(this)));
        return result;
 }
 
+shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr)
+{
+       return static_pointer_cast<Channel>(
+               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()
index 65eea9e2ac08710baf43090d3eacd3ba27c443b7..3deb29e6a98db27d055cb00c65f95c3d6dafa0ec 100644 (file)
@@ -291,7 +291,8 @@ public:
 protected:
        Device(struct sr_dev_inst *structure);
        ~Device();
-       vector<Channel *> channels;
+       shared_ptr<Channel> get_channel(struct sr_channel *ptr);
+       map<struct sr_channel *, Channel *> channels;
        /** Deleter needed to allow shared_ptr use with protected destructor. */
        class Deleter
        {