From: Joel Holdsworth Date: Sat, 22 Feb 2014 09:01:23 +0000 (+0000) Subject: Made DeviceManager only handle Device objects X-Git-Tag: pulseview-0.2.0~55 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=85843b14c48affe67820ec8c9e357cd26e0ed943;p=pulseview.git Made DeviceManager only handle Device objects --- diff --git a/pv/devicemanager.cpp b/pv/devicemanager.cpp index b854c050..5844e80e 100644 --- a/pv/devicemanager.cpp +++ b/pv/devicemanager.cpp @@ -49,12 +49,12 @@ DeviceManager::~DeviceManager() release_devices(); } -const list< shared_ptr >& DeviceManager::devices() const +const list< shared_ptr >& DeviceManager::devices() const { return _devices; } -void DeviceManager::use_device(shared_ptr dev_inst, +void DeviceManager::use_device(shared_ptr dev_inst, SigSession *owner) { assert(dev_inst); @@ -65,12 +65,12 @@ void DeviceManager::use_device(shared_ptr dev_inst, sr_dev_open(dev_inst->dev_inst()); } -void DeviceManager::release_device(shared_ptr dev_inst) +void DeviceManager::release_device(shared_ptr dev_inst) { assert(dev_inst); // Notify the owner, and remove the device from the used device list - map< shared_ptr, pv::SigSession*>::const_iterator + map< shared_ptr, pv::SigSession*>::const_iterator iter = _used_devices.find(dev_inst); assert(iter != _used_devices.end()); @@ -78,16 +78,16 @@ void DeviceManager::release_device(shared_ptr dev_inst) _used_devices.erase(dev_inst); } -list< shared_ptr > DeviceManager::driver_scan( +list< shared_ptr > DeviceManager::driver_scan( struct sr_dev_driver *const driver, GSList *const drvopts) { - list< shared_ptr > driver_devices; + list< shared_ptr > driver_devices; assert(driver); // Remove any device instances from this driver from the device // list. They will not be valid after the scan. - list< shared_ptr >::iterator i = _devices.begin(); + list< shared_ptr >::iterator i = _devices.begin(); while (i != _devices.end()) { if ((*i)->dev_inst()->driver == driver) i = _devices.erase(i); @@ -101,7 +101,7 @@ list< shared_ptr > DeviceManager::driver_scan( // Do the scan GSList *const devices = sr_driver_scan(driver, drvopts); for (GSList *l = devices; l; l = l->next) - driver_devices.push_back(shared_ptr( + driver_devices.push_back(shared_ptr( new device::Device((sr_dev_inst*)l->data))); g_slist_free(devices); driver_devices.sort(compare_devices); @@ -130,7 +130,7 @@ void DeviceManager::init_drivers() void DeviceManager::release_devices() { // Release all the used devices - for (map, SigSession*>::iterator i = + for (map, SigSession*>::iterator i = _used_devices.begin(); i != _used_devices.end(); i++) release_device((*i).first); @@ -153,7 +153,7 @@ void DeviceManager::scan_all_drivers() void DeviceManager::release_driver(struct sr_dev_driver *const driver) { assert(driver); - for (map, SigSession*>::iterator i = + for (map, SigSession*>::iterator i = _used_devices.begin(); i != _used_devices.end(); i++) if((*i).first->dev_inst()->driver == driver) { @@ -171,8 +171,8 @@ void DeviceManager::release_driver(struct sr_dev_driver *const driver) sr_dev_clear(driver); } -bool DeviceManager::compare_devices(shared_ptr a, - shared_ptr b) +bool DeviceManager::compare_devices(shared_ptr a, + shared_ptr b) { assert(a); assert(b); diff --git a/pv/devicemanager.h b/pv/devicemanager.h index 3ed74e07..2ce523dc 100644 --- a/pv/devicemanager.h +++ b/pv/devicemanager.h @@ -37,7 +37,7 @@ namespace pv { class SigSession; namespace device { -class DevInst; +class Device; } class DeviceManager @@ -47,15 +47,15 @@ public: ~DeviceManager(); - const std::list< boost::shared_ptr >& + const std::list< boost::shared_ptr >& devices() const; - void use_device(boost::shared_ptr dev_inst, + void use_device(boost::shared_ptr dev_inst, SigSession *owner); - void release_device(boost::shared_ptr dev_inst); + void release_device(boost::shared_ptr dev_inst); - std::list< boost::shared_ptr > driver_scan( + std::list< boost::shared_ptr > driver_scan( struct sr_dev_driver *const driver, GSList *const drvopts = NULL); @@ -68,13 +68,13 @@ private: void release_driver(struct sr_dev_driver *const driver); - static bool compare_devices(boost::shared_ptr a, - boost::shared_ptr b); + static bool compare_devices(boost::shared_ptr a, + boost::shared_ptr b); private: struct sr_context *const _sr_ctx; - std::list< boost::shared_ptr > _devices; - std::map< boost::shared_ptr, pv::SigSession*> + std::list< boost::shared_ptr > _devices; + std::map< boost::shared_ptr, pv::SigSession*> _used_devices; }; diff --git a/pv/dialogs/connect.cpp b/pv/dialogs/connect.cpp index 7b31942d..c9e8d82c 100644 --- a/pv/dialogs/connect.cpp +++ b/pv/dialogs/connect.cpp @@ -25,7 +25,7 @@ #include "connect.h" #include "pv/devicemanager.h" -#include "pv/device/devinst.h" +#include "pv/device/device.h" extern "C" { /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */ @@ -82,17 +82,17 @@ Connect::Connect(QWidget *parent, pv::DeviceManager &device_manager) : _layout.addWidget(&_button_box); } -shared_ptr Connect::get_selected_device() const +shared_ptr Connect::get_selected_device() const { const QListWidgetItem *const item = _device_list.currentItem(); if (!item) - return shared_ptr(); + return shared_ptr(); const sr_dev_inst *const sdi = (sr_dev_inst*)item->data( Qt::UserRole).value(); assert(sdi); - std::map >:: + std::map >:: const_iterator iter = _device_map.find(sdi); assert(iter != _device_map.end()); @@ -170,12 +170,12 @@ void Connect::scan_pressed() drvopts = g_slist_append(drvopts, src); } - const list< shared_ptr > devices = + const list< shared_ptr > devices = _device_manager.driver_scan(driver, drvopts); g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts); - BOOST_FOREACH(shared_ptr dev_inst, devices) + BOOST_FOREACH(shared_ptr dev_inst, devices) { assert(dev_inst); const sr_dev_inst *const sdi = dev_inst->dev_inst(); diff --git a/pv/dialogs/connect.h b/pv/dialogs/connect.h index 5a8b1cd0..1de0ed8d 100644 --- a/pv/dialogs/connect.h +++ b/pv/dialogs/connect.h @@ -40,7 +40,7 @@ namespace pv { class DeviceManager; namespace device { -class DevInst; +class Device; } namespace dialogs { @@ -52,7 +52,7 @@ class Connect : public QDialog public: Connect(QWidget *parent, pv::DeviceManager &device_manager); - boost::shared_ptr get_selected_device() const; + boost::shared_ptr get_selected_device() const; private: void populate_drivers(); @@ -83,7 +83,7 @@ private: QPushButton _scan_button; QListWidget _device_list; - std::map > + std::map > _device_map; QDialogButtonBox _button_box; diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index f72d1b70..9995e50a 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -25,6 +25,9 @@ #include #include +#include +#include + #include #include #include @@ -39,7 +42,7 @@ #include "mainwindow.h" #include "devicemanager.h" -#include "device/devinst.h" +#include "device/device.h" #include "dialogs/about.h" #include "dialogs/connect.h" #include "dialogs/storeprogress.h" @@ -289,8 +292,10 @@ void MainWindow::update_device_list( { assert(_sampling_bar); - const list< shared_ptr > &devices = - _device_manager.devices(); + list< shared_ptr > devices; + std::copy(_device_manager.devices().begin(), + _device_manager.devices().end(), std::back_inserter(devices)); + _sampling_bar->set_device_list(devices); if (!selected_device && !devices.empty()) { diff --git a/pv/sigsession.cpp b/pv/sigsession.cpp index 4469a3ee..aab5be7d 100644 --- a/pv/sigsession.cpp +++ b/pv/sigsession.cpp @@ -73,13 +73,16 @@ SigSession::SigSession(DeviceManager &device_manager) : SigSession::~SigSession() { + using pv::device::Device; + stop_capture(); if (_sampling_thread.joinable()) _sampling_thread.join(); - if (_dev_inst) - _device_manager.release_device(_dev_inst); + shared_ptr device(dynamic_pointer_cast(_dev_inst)); + if (device) + _device_manager.release_device(device); // TODO: This should not be necessary _session = NULL; @@ -92,13 +95,19 @@ shared_ptr SigSession::get_device() const void SigSession::set_device(shared_ptr dev_inst) { + using pv::device::Device; + // Ensure we are not capturing before setting the device stop_capture(); - if (_dev_inst) - _device_manager.release_device(_dev_inst); - if (dev_inst) - _device_manager.use_device(dev_inst, this); + shared_ptr old_device(dynamic_pointer_cast(_dev_inst)); + if (old_device) + _device_manager.release_device(old_device); + + shared_ptr new_device(dynamic_pointer_cast(dev_inst)); + if (new_device) + _device_manager.use_device(new_device, this); + _dev_inst = dev_inst; update_signals(dev_inst); }