From: Martin Ling Date: Sun, 14 Sep 2014 04:33:24 +0000 (+0100) Subject: C++: Make hardware device instances user owned. X-Git-Tag: libsigrok-0.4.0~1000 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a4e47454580b83972f052b5ace8e687d54d9425c;p=libsigrok.git C++: Make hardware device instances user owned. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index 42ef6e7d..43350b06 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -256,8 +256,6 @@ Driver::Driver(struct sr_dev_driver *structure) : Driver::~Driver() { - for (auto device : _devices) - delete device; } string Driver::name() @@ -280,11 +278,6 @@ vector> Driver::scan( _initialized = true; } - /* Clear all existing instances. */ - for (auto device : _devices) - delete device; - _devices.clear(); - /* Translate scan options to GSList of struct sr_config pointers. */ GSList *option_list = NULL; for (auto entry : options) @@ -303,20 +296,20 @@ vector> Driver::scan( /* Free option list. */ g_slist_free_full(option_list, g_free); + /* Create device objects. */ + vector> result; for (GSList *device = device_list; device; device = device->next) { auto sdi = (struct sr_dev_inst *) device->data; - _devices.push_back(new HardwareDevice(this, sdi)); + result.push_back(shared_ptr( + new HardwareDevice(shared_from_this(), sdi), + HardwareDevice::Deleter())); } /* Free GSList returned from scan. */ g_slist_free(device_list); - /* Create list of shared pointers to device instances for return. */ - vector> result; - for (auto device : _devices) - result.push_back(device->get_shared_pointer(_parent)); return result; } @@ -501,8 +494,9 @@ void Device::close() check(sr_dev_close(_structure)); } -HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) : - ParentOwned(structure), +HardwareDevice::HardwareDevice(shared_ptr driver, + struct sr_dev_inst *structure) : + UserOwned(structure), Device(structure), _driver(driver) { @@ -519,7 +513,7 @@ shared_ptr HardwareDevice::get_shared_from_this() shared_ptr HardwareDevice::driver() { - return _driver->get_shared_pointer(_parent); + return _driver; } Channel::Channel(struct sr_channel *structure) : diff --git a/bindings/cxx/include/libsigrok/libsigrok.hpp b/bindings/cxx/include/libsigrok/libsigrok.hpp index 7b8c76d4..972bde88 100644 --- a/bindings/cxx/include/libsigrok/libsigrok.hpp +++ b/bindings/cxx/include/libsigrok/libsigrok.hpp @@ -379,17 +379,24 @@ protected: /** A real hardware device, connected via a driver */ class SR_API HardwareDevice : - public ParentOwned, + public UserOwned, public Device { public: /** Driver providing this device. */ shared_ptr driver(); protected: - HardwareDevice(Driver *driver, struct sr_dev_inst *structure); + HardwareDevice(shared_ptr driver, struct sr_dev_inst *structure); ~HardwareDevice(); shared_ptr get_shared_from_this(); - Driver *_driver; + shared_ptr _driver; + /** Deleter needed to allow shared_ptr use with protected destructor. */ + class Deleter + { + public: + void operator()(HardwareDevice *device) { delete device; } + }; + friend class Deleter; friend class Driver; friend class ChannelGroup; };