mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 10:10:05 +00:00 
			
		
		
		
	Add mutexes to marker manager
This commit is contained in:
		
							parent
							
								
									a8845514c6
								
							
						
					
					
						commit
						ee998232f7
					
				
					 1 changed files with 73 additions and 54 deletions
				
			
		|  | @ -5,6 +5,7 @@ | |||
| #include <scwx/util/logger.hpp> | ||||
| 
 | ||||
| #include <filesystem> | ||||
| #include <shared_mutex> | ||||
| #include <vector> | ||||
| #include <string> | ||||
| 
 | ||||
|  | @ -41,6 +42,7 @@ public: | |||
|    MarkerManager* self_; | ||||
| 
 | ||||
|    boost::asio::thread_pool threadPool_ {1u}; | ||||
|    std::shared_mutex        markerRecordLock_ {}; | ||||
| 
 | ||||
|    void                          InitializeMarkerSettings(); | ||||
|    void                          ReadMarkerSettings(); | ||||
|  | @ -109,6 +111,8 @@ void MarkerManager::Impl::ReadMarkerSettings() | |||
|    logger_->info("Reading location marker settings"); | ||||
| 
 | ||||
|    boost::json::value markerJson = nullptr; | ||||
|    { | ||||
|       std::unique_lock lock(markerRecordLock_); | ||||
| 
 | ||||
|       // Determine if marker settings exists
 | ||||
|       if (std::filesystem::exists(markerSettingsPath_)) | ||||
|  | @ -142,6 +146,7 @@ void MarkerManager::Impl::ReadMarkerSettings() | |||
| 
 | ||||
|          logger_->debug("{} location marker entries", markerRecords_.size()); | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|    Q_EMIT self_->MarkersUpdated(); | ||||
| } | ||||
|  | @ -150,6 +155,7 @@ void MarkerManager::Impl::WriteMarkerSettings() | |||
| { | ||||
|    logger_->info("Saving location marker settings"); | ||||
| 
 | ||||
|    std::shared_lock lock(markerRecordLock_); | ||||
|    auto markerJson = boost::json::value_from(markerRecords_); | ||||
|    util::json::WriteJsonFile(markerSettingsPath_, markerJson); | ||||
| } | ||||
|  | @ -203,6 +209,7 @@ size_t MarkerManager::marker_count() | |||
| 
 | ||||
| std::optional<types::MarkerInfo> MarkerManager::get_marker(size_t index) | ||||
| { | ||||
|    std::shared_lock lock(p->markerRecordLock_); | ||||
|    if (index >= p->markerRecords_.size()) | ||||
|    { | ||||
|       return {}; | ||||
|  | @ -214,6 +221,8 @@ std::optional<types::MarkerInfo> MarkerManager::get_marker(size_t index) | |||
| 
 | ||||
| void MarkerManager::set_marker(size_t index, const types::MarkerInfo& marker) | ||||
| { | ||||
|    { | ||||
|       std::unique_lock lock(p->markerRecordLock_); | ||||
|       if (index >= p->markerRecords_.size()) | ||||
|       { | ||||
|          return; | ||||
|  | @ -221,25 +230,32 @@ void MarkerManager::set_marker(size_t index, const types::MarkerInfo& marker) | |||
|       std::shared_ptr<MarkerManager::Impl::MarkerRecord>& markerRecord = | ||||
|          p->markerRecords_[index]; | ||||
|       markerRecord->markerInfo_ = marker; | ||||
|    } | ||||
|    Q_EMIT MarkerChanged(index); | ||||
|    Q_EMIT MarkersUpdated(); | ||||
| } | ||||
| 
 | ||||
| void MarkerManager::add_marker(const types::MarkerInfo& marker) | ||||
| { | ||||
|    { | ||||
|       std::unique_lock lock(p->markerRecordLock_); | ||||
|       p->markerRecords_.emplace_back(std::make_shared<Impl::MarkerRecord>(marker)); | ||||
|    } | ||||
|    Q_EMIT MarkerAdded(); | ||||
|    Q_EMIT MarkersUpdated(); | ||||
| } | ||||
| 
 | ||||
| void MarkerManager::remove_marker(size_t index) | ||||
| { | ||||
|    { | ||||
|       std::unique_lock lock(p->markerRecordLock_); | ||||
|       if (index >= p->markerRecords_.size()) | ||||
|       { | ||||
|          return; | ||||
|       } | ||||
| 
 | ||||
|       p->markerRecords_.erase(std::next(p->markerRecords_.begin(), index)); | ||||
|    } | ||||
| 
 | ||||
|    Q_EMIT MarkerRemoved(index); | ||||
|    Q_EMIT MarkersUpdated(); | ||||
|  | @ -247,6 +263,8 @@ void MarkerManager::remove_marker(size_t index) | |||
| 
 | ||||
| void MarkerManager::move_marker(size_t from, size_t to) | ||||
| { | ||||
|    { | ||||
|       std::unique_lock lock(p->markerRecordLock_); | ||||
|       if (from >= p->markerRecords_.size() || to >= p->markerRecords_.size()) | ||||
|       { | ||||
|          return; | ||||
|  | @ -271,6 +289,7 @@ void MarkerManager::move_marker(size_t from, size_t to) | |||
|          } | ||||
|          p->markerRecords_[to] = markerRecord; | ||||
|       } | ||||
|    } | ||||
|    Q_EMIT MarkersUpdated(); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 AdenKoperczak
						AdenKoperczak