From d632d1ad4f13cacc1821012f7cbaf8130fe257f4 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 20 Jul 2018 18:11:54 +0200 Subject: [PATCH] Fixes in model loader and caches * loader/multi cache can clear caches if that is ever needed * check if cache needs to be synchronized when loaded and if, do it * emit changed signal if caches are set --- .../simulation/aircraftmodelloader.cpp | 64 +++++++++++++------ .../simulation/aircraftmodelloader.h | 16 +++-- src/blackmisc/simulation/data/modelcaches.cpp | 26 ++++++++ src/blackmisc/simulation/data/modelcaches.h | 8 ++- 4 files changed, 89 insertions(+), 25 deletions(-) diff --git a/src/blackmisc/simulation/aircraftmodelloader.cpp b/src/blackmisc/simulation/aircraftmodelloader.cpp index dd85b6084..fb2f5b8cc 100644 --- a/src/blackmisc/simulation/aircraftmodelloader.cpp +++ b/src/blackmisc/simulation/aircraftmodelloader.cpp @@ -42,31 +42,46 @@ namespace BlackMisc connect(&m_settings, &CMultiSimulatorSettings::settingsChanged, this, &IAircraftModelLoader::onSettingsChanged); } - QString IAircraftModelLoader::enumToString(IAircraftModelLoader::LoadFinishedInfo info) + const QString &IAircraftModelLoader::enumToString(IAircraftModelLoader::LoadFinishedInfo info) { + static const QString loaded("cache loaded"); + static const QString skipped("loading skipped"); + static const QString parsed("parsed data"); + switch (info) { - case CacheLoaded: return "cache loaded"; - case LoadingSkipped: return "loading skipped"; - case ParsedData: return "parsed data"; + case CacheLoaded: return loaded; + case LoadingSkipped: return skipped; + case ParsedData: return parsed; default: break; } - return "??"; + + static const QString unknown("??"); + return unknown; } - QString IAircraftModelLoader::enumToString(IAircraftModelLoader::LoadModeFlag modeFlag) + const QString &IAircraftModelLoader::enumToString(IAircraftModelLoader::LoadModeFlag modeFlag) { + static const QString notSet("not set"); + static const QString directly("load directly"); + static const QString background("load in background"); + static const QString cacheFirst("cache first"); + static const QString cacheSkipped("cache skipped"); + static const QString cacheOnly("cacheOnly"); + switch (modeFlag) { - case NotSet: return "not set"; - case LoadDirectly: return "load directly"; - case LoadInBackground: return "load in background"; - case CacheFirst: return "cache first"; - case CacheSkipped: return "cache skipped"; - case CacheOnly: return "cache only"; + case NotSet: return notSet; + case LoadDirectly: return directly; + case LoadInBackground: return background; + case CacheFirst: return cacheFirst; + case CacheSkipped: return cacheSkipped; + case CacheOnly: return cacheOnly; default: break; } - return "??"; + + static const QString unknown("??"); + return unknown; } QString IAircraftModelLoader::enumToString(LoadMode mode) @@ -80,6 +95,11 @@ namespace BlackMisc return modes.join(", "); } + bool IAircraftModelLoader::needsCacheSynchronized(LoadMode mode) + { + return mode.testFlag(CacheFirst) || mode.testFlag(CacheOnly); + } + IAircraftModelLoader::~IAircraftModelLoader() { this->gracefulShutdown(); @@ -93,9 +113,9 @@ namespace BlackMisc CStatusMessage IAircraftModelLoader::setCachedModels(const CAircraftModelList &models, const CSimulatorInfo &simulator) { - const CSimulatorInfo sim = simulator.isSingleSimulator() ? simulator : this->getSimulator(); // support default value - this->setObjectInfo(sim); - return m_caches.setCachedModels(models, sim); + const CSimulatorInfo usedSimulator = simulator.isSingleSimulator() ? simulator : this->getSimulator(); // support default value + this->setObjectInfo(usedSimulator); + return m_caches.setCachedModels(models, usedSimulator); } CStatusMessage IAircraftModelLoader::replaceOrAddCachedModels(const CAircraftModelList &models, const CSimulatorInfo &simulator) @@ -202,7 +222,12 @@ namespace BlackMisc CStatusMessage IAircraftModelLoader::clearCache() { - return this->setCachedModels(CAircraftModelList()); + return m_caches.clearCachedModels(m_caches.getCurrentSimulator()); + } + + CStatusMessage IAircraftModelLoader::clearCache(const CSimulatorInfo &simulator) + { + return m_caches.clearCachedModels(simulator); } void IAircraftModelLoader::startLoading(LoadMode mode, const ModelConsolidationCallback &modelConsolidation, const QStringList &modelDirectories) @@ -211,6 +236,10 @@ namespace BlackMisc m_loadingInProgress = true; m_loadingMessages.clear(); + const CSimulatorInfo simulator = this->getSimulator(); + const bool needsCacheSynced = IAircraftModelLoader::needsCacheSynchronized(mode); + if (needsCacheSynced) { m_caches.synchronizeCache(simulator); } + const bool useCachedData = !mode.testFlag(CacheSkipped) && this->hasCachedData(); if (useCachedData && (mode.testFlag(CacheFirst) || mode.testFlag(CacheOnly))) { @@ -229,7 +258,6 @@ namespace BlackMisc } // really load from disk? - const CSimulatorInfo simulator = this->getSimulator(); const QStringList modelDirs = this->getInitializedModelDirectories(modelDirectories, simulator); if (m_skipLoadingEmptyModelDir && modelDirs.isEmpty()) { diff --git a/src/blackmisc/simulation/aircraftmodelloader.h b/src/blackmisc/simulation/aircraftmodelloader.h index d609ff1e7..d7c33253f 100644 --- a/src/blackmisc/simulation/aircraftmodelloader.h +++ b/src/blackmisc/simulation/aircraftmodelloader.h @@ -77,14 +77,17 @@ namespace BlackMisc }; //! Enum as string - static QString enumToString(LoadFinishedInfo info); + static const QString &enumToString(LoadFinishedInfo info); //! Enum as string - static QString enumToString(LoadModeFlag modeFlag); + static const QString &enumToString(LoadModeFlag modeFlag); //! Enum as string static QString enumToString(LoadMode mode); + //! Is that mode needing caches synchronized? + static bool needsCacheSynchronized(LoadMode mode); + //! Destructor virtual ~IAircraftModelLoader(); @@ -159,6 +162,12 @@ namespace BlackMisc //! \copydoc BlackMisc::Simulation::Data::IMultiSimulatorModelCaches::synchronizeCache void synchronizeModelCache(const CSimulatorInfo &simulator); + //! \copydoc BlackMisc::Simulation::Data::IMultiSimulatorModelCaches::clearCache + BlackMisc::CStatusMessage clearCache(); + + //! \copydoc BlackMisc::Simulation::Data::IMultiSimulatorModelCaches::clearCache + BlackMisc::CStatusMessage clearCache(const CSimulatorInfo &simulator); + //! \copydoc Settings::CMultiSimulatorSettings::getSpecializedSettings Settings::CSpecializedSimulatorSettings getCurrentSimulatorSettings() const; @@ -204,9 +213,6 @@ namespace BlackMisc //! Any cached data? bool hasCachedData() const; - //! Clear cache - BlackMisc::CStatusMessage clearCache(); - //! Start the loading process from disk virtual void startLoadingFromDisk(LoadMode mode, const ModelConsolidationCallback &modelConsolidation, const QStringList &modelDirectories) = 0; diff --git a/src/blackmisc/simulation/data/modelcaches.cpp b/src/blackmisc/simulation/data/modelcaches.cpp index 530775558..f025f185e 100644 --- a/src/blackmisc/simulation/data/modelcaches.cpp +++ b/src/blackmisc/simulation/data/modelcaches.cpp @@ -218,6 +218,8 @@ namespace BlackMisc Q_ASSERT_X(false, Q_FUNC_INFO, "wrong simulator"); return CStatusMessage(); } + + this->emitCacheChanged(simulator); } QDateTime IMultiSimulatorModelCaches::getCurrentCacheTimestamp() const @@ -228,6 +230,28 @@ namespace BlackMisc return this->getCacheTimestamp(sim); } + CStatusMessage IMultiSimulatorModelCaches::clearCachedModels(const CSimulatorInfo &simulator) + { + static const CAircraftModelList models; + return this->setCachedModels(models, simulator); + } + + void IMultiSimulatorModelCaches::synchronizeMultiCaches(const CSimulatorInfo &simulator) + { + for (const CSimulatorInfo &singleSimulator : simulator.asSingleSimulatorSet()) + { + this->synchronizeCache(singleSimulator); + } + } + + void IMultiSimulatorModelCaches::admitMultiCaches(const CSimulatorInfo &simulator) + { + for (const CSimulatorInfo &singleSimulator : simulator.asSingleSimulatorSet()) + { + this->admitMultiCaches(singleSimulator); + } + } + QDateTime CModelCaches::getCacheTimestamp(const CSimulatorInfo &simulator) const { Q_ASSERT_X(simulator.isSingleSimulator(), Q_FUNC_INFO, "No single simulator"); @@ -410,6 +434,8 @@ namespace BlackMisc Q_ASSERT_X(false, Q_FUNC_INFO, "wrong simulator"); return CStatusMessage(); } + + this->emitCacheChanged(simulator); } QDateTime CModelSetCaches::getCacheTimestamp(const CSimulatorInfo &simulator) const diff --git a/src/blackmisc/simulation/data/modelcaches.h b/src/blackmisc/simulation/data/modelcaches.h index 2b7a1ff90..9aac169fe 100644 --- a/src/blackmisc/simulation/data/modelcaches.h +++ b/src/blackmisc/simulation/data/modelcaches.h @@ -205,9 +205,14 @@ namespace BlackMisc //! \threadsafe QDateTime getCurrentCacheTimestamp() const; - //! Set cache + //! Set cached models + //! \threadsafe virtual BlackMisc::CStatusMessage setCachedModels(const BlackMisc::Simulation::CAircraftModelList &models, const BlackMisc::Simulation::CSimulatorInfo &simulator) = 0; + //! Clear cached models + //! \threadsafe + virtual BlackMisc::CStatusMessage clearCachedModels(const BlackMisc::Simulation::CSimulatorInfo &simulator); + //! Synchronize for given simulator virtual void synchronizeCache(const BlackMisc::Simulation::CSimulatorInfo &simulator) = 0; @@ -264,7 +269,6 @@ namespace BlackMisc //! Void version of synchronizeCurrentCache void onLastSelectionChanged(); - private: //! Emit cacheChanged() utility function (allows breakpoint) void emitCacheChanged(const BlackMisc::Simulation::CSimulatorInfo &simulator); };