From 01f0a864dc312c19185f67937244def2027dfd29 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Wed, 6 May 2020 20:33:27 +0200 Subject: [PATCH] Yet more cleanup of Asset-related files --- include/openspace/scene/asset.h | 3 +- .../openspace/util/resourcesynchronization.h | 6 +-- .../openspace/util/synchronizationwatcher.h | 6 --- modules/imgui/src/guiassetcomponent.cpp | 4 +- src/engine/openspaceengine.cpp | 7 ++-- src/scene/asset.cpp | 38 ++++++++++--------- src/util/resourcesynchronization.cpp | 6 +-- 7 files changed, 33 insertions(+), 37 deletions(-) diff --git a/include/openspace/scene/asset.h b/include/openspace/scene/asset.h index 94e9600697..b7b7793b49 100644 --- a/include/openspace/scene/asset.h +++ b/include/openspace/scene/asset.h @@ -81,8 +81,7 @@ public: void addSynchronization(std::shared_ptr synchronization); void clearSynchronizations(); - const std::vector>& - ownSynchronizations() const; + std::vector ownSynchronizations() const; void syncStateChanged(ResourceSynchronization* sync, ResourceSynchronization::State state); diff --git a/include/openspace/util/resourcesynchronization.h b/include/openspace/util/resourcesynchronization.h index 605f049dc1..9a96ea6d2c 100644 --- a/include/openspace/util/resourcesynchronization.h +++ b/include/openspace/util/resourcesynchronization.h @@ -68,9 +68,9 @@ public: State state() const; const std::string& name() const; - bool isResolved(); - bool isRejected(); - bool isSyncing(); + bool isResolved() const; + bool isRejected() const; + bool isSyncing() const; CallbackHandle addStateChangeCallback(StateChangeCallback cb); void removeStateChangeCallback(CallbackHandle id); diff --git a/include/openspace/util/synchronizationwatcher.h b/include/openspace/util/synchronizationwatcher.h index 2e3632de66..d830218cb8 100644 --- a/include/openspace/util/synchronizationwatcher.h +++ b/include/openspace/util/synchronizationwatcher.h @@ -53,12 +53,6 @@ public: ResourceSynchronization::StateChangeCallback callback; }; - /*using SyncStateChangeCallback = - std::function, - ResourceSynchronization::State - )>;*/ - WatchHandle watchSynchronization( std::shared_ptr synchronization, ResourceSynchronization::StateChangeCallback callback diff --git a/modules/imgui/src/guiassetcomponent.cpp b/modules/imgui/src/guiassetcomponent.cpp index 34fb9d629a..35d8a60317 100644 --- a/modules/imgui/src/guiassetcomponent.cpp +++ b/modules/imgui/src/guiassetcomponent.cpp @@ -107,7 +107,7 @@ void GuiAssetComponent::renderTree(const Asset& asset, const std::string& relati const std::vector>& requested = asset.requestedAssets(); const std::vector>& required = asset.requiredAssets(); - const std::vector>& resourceSyncs = + const std::vector& resourceSyncs = asset.ownSynchronizations(); if (requested.empty() && required.empty() && resourceSyncs.empty()) { @@ -126,7 +126,7 @@ void GuiAssetComponent::renderTree(const Asset& asset, const std::string& relati } if (!resourceSyncs.empty() && ImGui::TreeNode("Resource Synchronizations")) { - for (const std::shared_ptr& sync : resourceSyncs) { + for (ResourceSynchronization* sync : resourceSyncs) { std::string resourceText = sync->directory() + " " + syncStateToString(sync->state()); if (sync->state() == ResourceSynchronization::State::Syncing) { diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 0cfe7c1295..66c45fec15 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -762,12 +762,11 @@ void OpenSpaceEngine::loadSingleAsset(const std::string& assetPath) { std::vector> allAssets = _assetManager->rootAsset()->subTreeAssets(); - std::unordered_set> resourceSyncs; + std::unordered_set resourceSyncs; for (const std::shared_ptr& a : allAssets) { - std::vector> syncs = - a->ownSynchronizations(); + std::vector syncs = a->ownSynchronizations(); - for (const std::shared_ptr& s : syncs) { + for (ResourceSynchronization* s : syncs) { ZoneScopedN("Update resource synchronization") if (s->state() == ResourceSynchronization::State::Syncing) { diff --git a/src/scene/asset.cpp b/src/scene/asset.cpp index 242f0029ef..4a5a9bad0c 100644 --- a/src/scene/asset.cpp +++ b/src/scene/asset.cpp @@ -33,19 +33,19 @@ #include #include +namespace openspace { + namespace { constexpr const char* _loggerCat = "Asset"; - float syncProgress(const std::vector>& assets) - { + float syncProgress(const std::vector>& assets) { size_t nTotalBytes = 0; size_t nSyncedBytes = 0; - for (const std::shared_ptr& a : assets) { - const std::vector>& s = - a->ownSynchronizations(); + for (const std::shared_ptr& a : assets) { + const std::vector& s = a->ownSynchronizations(); - for (const std::shared_ptr& sync : s) { + for (ResourceSynchronization* sync : s) { if (sync->nTotalBytesIsKnown()) { nTotalBytes += sync->nTotalBytes(); nSyncedBytes += sync->nSynchronizedBytes(); @@ -64,7 +64,6 @@ namespace { } } // namespace -namespace openspace { Asset::Asset(AssetLoader* loader, SynchronizationWatcher* watcher) : _state(State::SyncResolved) @@ -221,23 +220,28 @@ bool Asset::isSyncResolveReady() { return false; } - const std::vector>& syncs = - ownSynchronizations(); + const std::vector& syncs = ownSynchronizations(); auto unresolvedOwnSynchronization = std::find_if( syncs.cbegin(), syncs.cend(), - [](const std::shared_ptr& s) { return !s->isResolved(); } + [](ResourceSynchronization* s) { return !s->isResolved(); } ); // To be considered resolved, all own synchronizations need to be resolved return unresolvedOwnSynchronization == syncs.cend(); } -const std::vector>& -Asset::ownSynchronizations() const -{ - return _synchronizations; +std::vector Asset::ownSynchronizations() const { + std::vector res; + res.reserve(_synchronizations.size()); + std::transform( + _synchronizations.begin(), _synchronizations.end(), + std::back_inserter(res), + std::mem_fn(&std::shared_ptr::get) + ); + + return res; } std::vector> Asset::subTreeAssets() const { @@ -389,7 +393,7 @@ bool Asset::startSynchronizations() { } // Now synchronize its own synchronizations - for (const std::shared_ptr& s : ownSynchronizations()) { + for (ResourceSynchronization* s : ownSynchronizations()) { if (!s->isResolved()) { s->start(); } @@ -411,7 +415,7 @@ bool Asset::cancelAllSynchronizations() { } ); - for (const std::shared_ptr& s : ownSynchronizations()) { + for (ResourceSynchronization* s : ownSynchronizations()) { if (s->isSyncing()) { cancelledAnySync = true; s->cancel(); @@ -438,7 +442,7 @@ bool Asset::cancelUnwantedSynchronizations() { } ); - for (const std::shared_ptr& s : ownSynchronizations()) { + for (ResourceSynchronization* s : ownSynchronizations()) { if (s->isSyncing()) { cancelledAnySync = true; s->cancel(); diff --git a/src/util/resourcesynchronization.cpp b/src/util/resourcesynchronization.cpp index 2fb379ce09..7199b7a82a 100644 --- a/src/util/resourcesynchronization.cpp +++ b/src/util/resourcesynchronization.cpp @@ -95,15 +95,15 @@ ResourceSynchronization::State ResourceSynchronization::state() const { return _state; } -bool ResourceSynchronization::isResolved() { +bool ResourceSynchronization::isResolved() const { return _state == State::Resolved; } -bool ResourceSynchronization::isRejected() { +bool ResourceSynchronization::isRejected() const { return _state == State::Rejected; } -bool ResourceSynchronization::isSyncing() { +bool ResourceSynchronization::isSyncing() const { return _state == State::Syncing; }