C++: Add internal lookup to find Channel object from sr_channel *.

This commit is contained in:
Martin Ling 2014-07-24 03:22:44 +01:00
parent 7649683c2a
commit 4178d9712f
2 changed files with 14 additions and 12 deletions

View File

@ -344,14 +344,14 @@ Device::Device(struct sr_dev_inst *structure) :
for (GSList *entry = structure->channels; entry; entry = entry->next) for (GSList *entry = structure->channels; entry; entry = entry->next)
{ {
auto channel = (struct sr_channel *) entry->data; auto channel = (struct sr_channel *) entry->data;
channels.push_back(new Channel(channel)); channels[channel] = new Channel(channel);
} }
} }
Device::~Device() Device::~Device()
{ {
for (auto channel : channels) for (auto entry : channels)
delete channel; delete entry.second;
} }
string Device::get_vendor() string Device::get_vendor()
@ -372,12 +372,18 @@ string Device::get_version()
vector<shared_ptr<Channel>> Device::get_channels() vector<shared_ptr<Channel>> Device::get_channels()
{ {
vector<shared_ptr<Channel>> result; vector<shared_ptr<Channel>> result;
for (auto channel : channels) for (auto entry : channels)
result.push_back(static_pointer_cast<Channel>( result.push_back(static_pointer_cast<Channel>(
channel->get_shared_pointer(this))); entry.second->get_shared_pointer(this)));
return result; 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() void Device::open()
{ {
check(sr_dev_open(structure)); check(sr_dev_open(structure));
@ -465,12 +471,7 @@ ChannelGroup::ChannelGroup(HardwareDevice *device,
Configurable(device->structure->driver, device->structure, structure) Configurable(device->structure->driver, device->structure, structure)
{ {
for (GSList *entry = structure->channels; entry; entry = entry->next) for (GSList *entry = structure->channels; entry; entry = entry->next)
{ channels.push_back(device->channels[(struct sr_channel *)entry->data]);
auto channel = (struct sr_channel *) entry->data;
for (auto device_channel : device->channels)
if (channel == device_channel->structure)
channels.push_back(device_channel);
}
} }
ChannelGroup::~ChannelGroup() ChannelGroup::~ChannelGroup()

View File

@ -291,7 +291,8 @@ public:
protected: protected:
Device(struct sr_dev_inst *structure); Device(struct sr_dev_inst *structure);
~Device(); ~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. */ /** Deleter needed to allow shared_ptr use with protected destructor. */
class Deleter class Deleter
{ {