]> sigrok.org Git - pulseview.git/commitdiff
Moved DeviceManager::use_device and release_device into DevInst
authorJoel Holdsworth <redacted>
Sat, 22 Feb 2014 09:23:33 +0000 (09:23 +0000)
committerJoel Holdsworth <redacted>
Sat, 1 Mar 2014 11:02:11 +0000 (11:02 +0000)
pv/device/devinst.cpp
pv/device/devinst.h
pv/devicemanager.cpp
pv/devicemanager.h
pv/sigsession.cpp
pv/sigsession.h

index 5fe2951756e4b602fca4da0fa4b379f3cfc19ef0..adbf8968595e80cc66441dcd48985fc18d25df5b 100644 (file)
 
 #include "devinst.h"
 
+#include <pv/sigsession.h>
+
 namespace pv {
 namespace device {
 
 DevInst::DevInst(sr_dev_inst *sdi) :
-       _sdi(sdi)
+       _sdi(sdi),
+       _owner(NULL)
 {
        assert(_sdi);
 }
@@ -40,6 +43,28 @@ sr_dev_inst* DevInst::dev_inst() const
        return _sdi;
 }
 
+void DevInst::use(SigSession *owner)
+{
+       assert(owner);
+       assert(!_owner);
+       _owner = owner;
+       sr_dev_open(_sdi);
+}
+
+void DevInst::release()
+{
+       if (_owner) {
+               _owner->release_device(this);
+               _owner = NULL;
+               sr_dev_close(_sdi);
+       }
+}
+
+SigSession* DevInst::owner() const
+{
+       return _owner;
+}
+
 GVariant* DevInst::get_config(const sr_probe_group *group, int key)
 {
        GVariant *data = NULL;
index 6e81f49c686fcc8ab1424ed2836ae6e859e5e348..9098027eb84e0674da978a65c38e67df3485c36f 100644 (file)
@@ -36,6 +36,9 @@ struct sr_probe;
 struct sr_probe_group;
 
 namespace pv {
+
+class SigSession;
+
 namespace device {
 
 class DevInst : public QObject
@@ -48,6 +51,12 @@ protected:
 public:
        sr_dev_inst* dev_inst() const;
 
+       void use(SigSession *owner);
+
+       void release();
+
+       SigSession* owner() const;
+
        virtual std::string format_device_title() const = 0;
 
        GVariant* get_config(const sr_probe_group *group, int key);
@@ -71,6 +80,7 @@ signals:
 
 protected:
        sr_dev_inst *const _sdi;
+       SigSession *_owner;
 };
 
 } // device
index 5844e80e75325ad91d1e9f93d8368e4e7630321e..fb487a8163feb38861cf0f384a792d2704ebbf2c 100644 (file)
@@ -26,6 +26,8 @@
 #include <stdexcept>
 #include <string>
 
+#include <boost/foreach.hpp>
+
 #include <libsigrok/libsigrok.h>
 
 using boost::shared_ptr;
@@ -54,30 +56,6 @@ const list< shared_ptr<pv::device::Device> >& DeviceManager::devices() const
        return _devices;
 }
 
-void DeviceManager::use_device(shared_ptr<device::Device> dev_inst,
-       SigSession *owner)
-{
-       assert(dev_inst);
-       assert(owner);
-
-       _used_devices[dev_inst] = owner;
-
-       sr_dev_open(dev_inst->dev_inst());
-}
-
-void DeviceManager::release_device(shared_ptr<device::Device> dev_inst)
-{
-       assert(dev_inst);
-
-       // Notify the owner, and remove the device from the used device list
-       map< shared_ptr<device::Device>, pv::SigSession*>::const_iterator
-               iter = _used_devices.find(dev_inst);
-       assert(iter != _used_devices.end());
-
-       (*iter).second->release_device(dev_inst);
-       _used_devices.erase(dev_inst);
-}
-
 list< shared_ptr<device::Device> > DeviceManager::driver_scan(
        struct sr_dev_driver *const driver, GSList *const drvopts)
 {
@@ -130,11 +108,10 @@ void DeviceManager::init_drivers()
 void DeviceManager::release_devices()
 {
        // Release all the used devices
-       for (map<shared_ptr<device::Device>, SigSession*>::iterator i =
-               _used_devices.begin(); i != _used_devices.end(); i++)
-               release_device((*i).first);
-
-       _used_devices.clear();
+       BOOST_FOREACH(shared_ptr<device::Device> dev, _devices) {
+               assert(dev);
+               dev->release();
+       }
 
        // Clear all the drivers
        sr_dev_driver **const drivers = sr_driver_list();
@@ -152,20 +129,11 @@ void DeviceManager::scan_all_drivers()
 
 void DeviceManager::release_driver(struct sr_dev_driver *const driver)
 {
-       assert(driver);
-       for (map<shared_ptr<device::Device>, SigSession*>::iterator i =
-               _used_devices.begin(); i != _used_devices.end(); i++)
-               if((*i).first->dev_inst()->driver == driver)
-               {
-                       // Notify the current owner of the device
-                       (*i).second->release_device((*i).first);
-
-                       // Remove it from the used device list
-                       _used_devices.erase(i);
-
-                       // Close the device instance
-                       sr_dev_close((*i).first->dev_inst());
-               }
+       BOOST_FOREACH(shared_ptr<device::Device> dev, _devices) {
+               assert(dev);
+               if(dev->dev_inst()->driver == driver)
+                       dev->release();
+       }
 
        // Clear all the old device instances from this driver
        sr_dev_clear(driver);
index 2ce523dc6251d424921bba5a696a2fbcdcd100bd..acef8a42d93ba2b1f1d1f5475717ab3a97557079 100644 (file)
@@ -24,7 +24,6 @@
 #include <glib.h>
 
 #include <list>
-#include <map>
 #include <string>
 
 #include <boost/shared_ptr.hpp>
@@ -50,11 +49,6 @@ public:
        const std::list< boost::shared_ptr<pv::device::Device> >&
                devices() const;
 
-       void use_device(boost::shared_ptr<pv::device::Device> dev_inst,
-               SigSession *owner);
-
-       void release_device(boost::shared_ptr<pv::device::Device> dev_inst);
-
        std::list< boost::shared_ptr<pv::device::Device> > driver_scan(
                struct sr_dev_driver *const driver,
                GSList *const drvopts = NULL);
@@ -74,8 +68,6 @@ private:
 private:
        struct sr_context *const _sr_ctx;
        std::list< boost::shared_ptr<pv::device::Device> > _devices;
-       std::map< boost::shared_ptr<pv::device::Device>, pv::SigSession*>
-               _used_devices;
 };
 
 } // namespace pv
index aab5be7d23bcb5b4ba2ed85d777cb40c5f80c2f7..90fb2798abb7f5aa63e766c9973b0cc61c71fc5d 100644 (file)
@@ -80,9 +80,7 @@ SigSession::~SigSession()
        if (_sampling_thread.joinable())
                _sampling_thread.join();
 
-       shared_ptr<Device> device(dynamic_pointer_cast<Device>(_dev_inst));
-       if (device)
-               _device_manager.release_device(device);
+       _dev_inst->release();
 
        // TODO: This should not be necessary
        _session = NULL;
@@ -100,21 +98,20 @@ void SigSession::set_device(shared_ptr<device::DevInst> dev_inst)
        // Ensure we are not capturing before setting the device
        stop_capture();
 
-       shared_ptr<Device> old_device(dynamic_pointer_cast<Device>(_dev_inst));
-       if (old_device)
-               _device_manager.release_device(old_device);
+       if (_dev_inst)
+               _dev_inst->release();
 
-       shared_ptr<Device> new_device(dynamic_pointer_cast<Device>(dev_inst));
-       if (new_device)
-               _device_manager.use_device(new_device, this);
+       if (dev_inst)
+               dev_inst->use(this);
 
        _dev_inst = dev_inst;
        update_signals(dev_inst);
 }
 
-void SigSession::release_device(shared_ptr<device::DevInst> dev_inst)
+void SigSession::release_device(device::DevInst *dev_inst)
 {
        (void)dev_inst;
+       assert(_dev_inst.get() == dev_inst);
 
        assert(_capture_state == Stopped);
        _dev_inst = shared_ptr<device::DevInst>();
index 6b2e75999c06da96430924d8674fd64c90ccd422..bed7474d12957eeb578d4d91b7d465af36b65ae6 100644 (file)
@@ -83,7 +83,7 @@ public:
         */
        void set_device(boost::shared_ptr<device::DevInst> dev_inst);
 
-       void release_device(boost::shared_ptr<device::DevInst> dev_inst);
+       void release_device(device::DevInst *dev_inst);
 
        void load_file(const std::string &name,
                boost::function<void (const QString)> error_handler);