Driver::~Driver()
{
- for (auto device : _devices)
- delete device;
}
string Driver::name()
_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)
/* Free option list. */
g_slist_free_full(option_list, g_free);
+
/* Create device objects. */
+ vector<shared_ptr<HardwareDevice>> 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<HardwareDevice>(
+ 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<shared_ptr<HardwareDevice>> result;
- for (auto device : _devices)
- result.push_back(device->get_shared_pointer(_parent));
return result;
}
check(sr_dev_close(_structure));
}
-HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) :
- ParentOwned(structure),
+HardwareDevice::HardwareDevice(shared_ptr<Driver> driver,
+ struct sr_dev_inst *structure) :
+ UserOwned(structure),
Device(structure),
_driver(driver)
{
shared_ptr<Driver> HardwareDevice::driver()
{
- return _driver->get_shared_pointer(_parent);
+ return _driver;
}
Channel::Channel(struct sr_channel *structure) :
/** A real hardware device, connected via a driver */
class SR_API HardwareDevice :
- public ParentOwned<HardwareDevice, Context, struct sr_dev_inst>,
+ public UserOwned<HardwareDevice, struct sr_dev_inst>,
public Device
{
public:
/** Driver providing this device. */
shared_ptr<Driver> driver();
protected:
- HardwareDevice(Driver *driver, struct sr_dev_inst *structure);
+ HardwareDevice(shared_ptr<Driver> driver, struct sr_dev_inst *structure);
~HardwareDevice();
shared_ptr<Device> get_shared_from_this();
- Driver *_driver;
+ shared_ptr<Driver> _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;
};