mirror of
				https://github.com/f4exb/sdrangel.git
				synced 2025-10-31 04:50:29 -04:00 
			
		
		
		
	Merge pull request #913 from ChiefGokhlayeh/bugfix/#681-sdrangel-with-2-plutosdr
Use shared_ptr to keep track of DeviceScan across multiple collections
This commit is contained in:
		
						commit
						56ebd6fad3
					
				| @ -19,6 +19,7 @@ | |||||||
| #include <cstdio> | #include <cstdio> | ||||||
| #include <cstring> | #include <cstring> | ||||||
| #include <regex> | #include <regex> | ||||||
|  | #include <memory> | ||||||
| #include <iio.h> | #include <iio.h> | ||||||
| 
 | 
 | ||||||
| #include <QtGlobal> | #include <QtGlobal> | ||||||
| @ -49,6 +50,7 @@ void DevicePlutoSDRScan::scan() | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     m_scans.clear(); |     m_scans.clear(); | ||||||
|  |     m_scans.reserve(num_contexts); | ||||||
| 
 | 
 | ||||||
|     for (i = 0; i < num_contexts; i++) |     for (i = 0; i < num_contexts; i++) | ||||||
|     { |     { | ||||||
| @ -64,17 +66,25 @@ void DevicePlutoSDRScan::scan() | |||||||
| 
 | 
 | ||||||
|         if (pch) |         if (pch) | ||||||
|         { |         { | ||||||
|             m_scans.push_back({std::string(description), std::string("TBD"), std::string(uri)}); |             // As device scan is used across multiple vectors it's best to use a
 | ||||||
|             m_urilMap[m_scans.back().m_uri] = &m_scans.back(); |             // managed pointer, as to keep track of when it's safe to delete.
 | ||||||
|  |             std::shared_ptr<DeviceScan> dev_scan = std::make_shared<DeviceScan>( | ||||||
|  |                 DeviceScan({ | ||||||
|  |                     std::string(description), | ||||||
|  |                     std::string("TBD"), | ||||||
|  |                     std::string(uri) | ||||||
|  |                 })); | ||||||
|  |             m_scans.push_back(dev_scan); | ||||||
|  |             m_urilMap[m_scans.back()->m_uri] = m_scans.back(); | ||||||
| 
 | 
 | ||||||
|             std::regex desc_regex(".*serial=(.+)"); |             std::regex desc_regex(".*serial=(.+)"); | ||||||
|             std::smatch desc_match; |             std::smatch desc_match; | ||||||
|             std::regex_search(m_scans.back().m_name, desc_match, desc_regex); |             std::regex_search(m_scans.back()->m_name, desc_match, desc_regex); | ||||||
| 
 | 
 | ||||||
|             if (desc_match.size() == 2) |             if (desc_match.size() == 2) | ||||||
|             { |             { | ||||||
|                 m_scans.back().m_serial = desc_match[1]; |                 m_scans.back()->m_serial = desc_match[1]; | ||||||
|                 m_serialMap[m_scans.back().m_serial] = &m_scans.back(); |                 m_serialMap[m_scans.back()->m_serial] = m_scans.back(); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @ -86,7 +96,7 @@ void DevicePlutoSDRScan::scan() | |||||||
| const std::string* DevicePlutoSDRScan::getURIAt(unsigned int index) const | const std::string* DevicePlutoSDRScan::getURIAt(unsigned int index) const | ||||||
| { | { | ||||||
|     if (index < m_scans.size()) { |     if (index < m_scans.size()) { | ||||||
|         return &(m_scans[index].m_uri); |         return &(m_scans[index]->m_uri); | ||||||
|     } else { |     } else { | ||||||
|         return 0; |         return 0; | ||||||
|     } |     } | ||||||
| @ -95,7 +105,7 @@ const std::string* DevicePlutoSDRScan::getURIAt(unsigned int index) const | |||||||
| const std::string* DevicePlutoSDRScan::getSerialAt(unsigned int index) const | const std::string* DevicePlutoSDRScan::getSerialAt(unsigned int index) const | ||||||
| { | { | ||||||
|     if (index < m_scans.size()) { |     if (index < m_scans.size()) { | ||||||
|         return &(m_scans[index].m_serial); |         return &(m_scans[index]->m_serial); | ||||||
|     } else { |     } else { | ||||||
|         return 0; |         return 0; | ||||||
|     } |     } | ||||||
| @ -104,7 +114,7 @@ const std::string* DevicePlutoSDRScan::getSerialAt(unsigned int index) const | |||||||
| const std::string* DevicePlutoSDRScan::getURIFromSerial( | const std::string* DevicePlutoSDRScan::getURIFromSerial( | ||||||
|         const std::string& serial) const |         const std::string& serial) const | ||||||
| { | { | ||||||
|     std::map<std::string, DeviceScan*>::const_iterator it = m_serialMap.find(serial); |     std::map<std::string, std::shared_ptr<DeviceScan>>::const_iterator it = m_serialMap.find(serial); | ||||||
|     if (it == m_serialMap.end()) { |     if (it == m_serialMap.end()) { | ||||||
|         return 0; |         return 0; | ||||||
|     } else { |     } else { | ||||||
| @ -114,11 +124,11 @@ const std::string* DevicePlutoSDRScan::getURIFromSerial( | |||||||
| 
 | 
 | ||||||
| void DevicePlutoSDRScan::getSerials(std::vector<std::string>& serials) const | void DevicePlutoSDRScan::getSerials(std::vector<std::string>& serials) const | ||||||
| { | { | ||||||
|     std::vector<DeviceScan>::const_iterator it = m_scans.begin(); |     std::vector<std::shared_ptr<DeviceScan>>::const_iterator it = m_scans.begin(); | ||||||
|     serials.clear(); |     serials.clear(); | ||||||
| 
 | 
 | ||||||
|     for (; it != m_scans.end(); ++it) { |     for (; it != m_scans.end(); ++it) { | ||||||
|         serials.push_back(it->m_serial); |         serials.push_back((*it)->m_serial); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -23,6 +23,7 @@ | |||||||
| #include <string> | #include <string> | ||||||
| #include <vector> | #include <vector> | ||||||
| #include <map> | #include <map> | ||||||
|  | #include <memory> | ||||||
| 
 | 
 | ||||||
| #include "plugin/plugininterface.h" | #include "plugin/plugininterface.h" | ||||||
| #include "export.h" | #include "export.h" | ||||||
| @ -46,9 +47,9 @@ public: | |||||||
|     void enumOriginDevices(const QString& hardwareId, PluginInterface::OriginDevices& originDevices); |     void enumOriginDevices(const QString& hardwareId, PluginInterface::OriginDevices& originDevices); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     std::vector<DeviceScan> m_scans; |     std::vector<std::shared_ptr<DeviceScan>> m_scans; | ||||||
|     std::map<std::string, DeviceScan*> m_serialMap; |     std::map<std::string, std::shared_ptr<DeviceScan>> m_serialMap; | ||||||
|     std::map<std::string, DeviceScan*> m_urilMap; |     std::map<std::string, std::shared_ptr<DeviceScan>> m_urilMap; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user