From: Martin Ling Date: Fri, 29 Aug 2014 13:01:51 +0000 (+0100) Subject: C++: Fix shared pointer handling for Device base class. X-Git-Tag: libsigrok-0.4.0~1083 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d01d2314879988e9d9ab80872889340e89ec8cc8;p=libsigrok.git C++: Fix shared pointer handling for Device base class. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 2b26f1a8..bb7cec69 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -419,14 +419,14 @@ vector> Device::get_channels() vector> result; for (auto entry : channels) result.push_back(static_pointer_cast( - entry.second->get_shared_pointer(this))); + entry.second->get_shared_pointer(get_shared_from_this()))); return result; } shared_ptr Device::get_channel(struct sr_channel *ptr) { return static_pointer_cast( - channels[ptr]->get_shared_pointer(this)); + channels[ptr]->get_shared_pointer(get_shared_from_this())); } map> @@ -438,7 +438,7 @@ Device::get_channel_groups() auto name = entry.first; auto channel_group = entry.second; result[name] = static_pointer_cast( - channel_group->get_shared_pointer(this)); + channel_group->get_shared_pointer(get_shared_from_this())); } return result; } @@ -464,6 +464,12 @@ HardwareDevice::~HardwareDevice() { } +shared_ptr HardwareDevice::get_shared_from_this() +{ + return static_pointer_cast( + static_pointer_cast(shared_from_this())); +} + shared_ptr HardwareDevice::get_driver() { return static_pointer_cast(driver->get_shared_pointer(parent)); @@ -735,9 +741,6 @@ vector> Session::get_devices() for (GSList *dev = dev_list; dev; dev = dev->next) { auto sdi = (struct sr_dev_inst *) dev->data; - if (devices.count(sdi) == 0) - devices[sdi] = shared_ptr( - new Device(sdi), Device::Deleter()); result.push_back(devices[sdi]); } return result; @@ -1199,6 +1202,12 @@ InputDevice::~InputDevice() { } +shared_ptr InputDevice::get_shared_from_this() +{ + return static_pointer_cast( + static_pointer_cast(shared_from_this())); +} + Option::Option(const struct sr_option *structure, shared_ptr structure_array) : structure(structure), diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index f05205a6..6595802e 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -289,9 +289,7 @@ protected: }; /** A generic device, either hardware or virtual */ -class SR_API Device : - public enable_shared_from_this, - public Configurable +class SR_API Device : public Configurable { public: /** Description identifying this device. */ @@ -313,6 +311,7 @@ public: protected: Device(struct sr_dev_inst *structure); ~Device(); + virtual shared_ptr get_shared_from_this() = 0; shared_ptr get_channel(struct sr_channel *ptr); struct sr_dev_inst *structure; map channels; @@ -342,6 +341,7 @@ public: protected: HardwareDevice(Driver *driver, struct sr_dev_inst *structure); ~HardwareDevice(); + shared_ptr get_shared_from_this(); Driver *driver; friend class Driver; friend class ChannelGroup; @@ -765,6 +765,7 @@ public: protected: Input(shared_ptr context, const struct sr_input *structure); ~Input(); + shared_ptr get_shared_from_this(); const struct sr_input *structure; shared_ptr context; InputDevice *device; @@ -787,6 +788,7 @@ class SR_API InputDevice : protected: InputDevice(shared_ptr input, struct sr_dev_inst *sdi); ~InputDevice(); + shared_ptr get_shared_from_this(); shared_ptr input; /** Deleter needed to allow shared_ptr use with protected destructor. */ class Deleter