From 0d0f761d6ef520010b50fd9c498e16ef48a1108a Mon Sep 17 00:00:00 2001 From: Michael Nilsson Date: Tue, 5 Apr 2016 11:33:58 -0400 Subject: [PATCH] downloadmanager return shared_ptr to FileFutures instead of bare pointers --- apps/Launcher/infowidget.cpp | 2 +- apps/Launcher/infowidget.h | 2 +- apps/Launcher/syncwidget.cpp | 15 +++++++-------- apps/Launcher/syncwidget.h | 8 ++++---- include/openspace/engine/downloadmanager.h | 6 +++--- src/engine/downloadmanager.cpp | 16 ++++++++-------- 6 files changed, 24 insertions(+), 25 deletions(-) diff --git a/apps/Launcher/infowidget.cpp b/apps/Launcher/infowidget.cpp index ff2537c407..ddde63591a 100644 --- a/apps/Launcher/infowidget.cpp +++ b/apps/Launcher/infowidget.cpp @@ -75,7 +75,7 @@ InfoWidget::InfoWidget(QString name, int totalBytes) setLayout(layout); } -void InfoWidget::update(openspace::DownloadManager::FileFuture* f) { +void InfoWidget::update(std::shared_ptr f) { _bytes->setText( QString("%1 / %2") .arg(f->currentSize) diff --git a/apps/Launcher/infowidget.h b/apps/Launcher/infowidget.h index 2099a774af..2eaec279b9 100644 --- a/apps/Launcher/infowidget.h +++ b/apps/Launcher/infowidget.h @@ -40,7 +40,7 @@ Q_OBJECT public: InfoWidget(QString name, int totalBytes = -1); - void update(openspace::DownloadManager::FileFuture* f); + void update(std::shared_ptr f); void update(libtorrent::torrent_status s); void error(QString message); diff --git a/apps/Launcher/syncwidget.cpp b/apps/Launcher/syncwidget.cpp index 6ed87b2544..0710378173 100644 --- a/apps/Launcher/syncwidget.cpp +++ b/apps/Launcher/syncwidget.cpp @@ -230,7 +230,7 @@ void SyncWidget::setSceneFiles(QMap sceneFiles) { } void SyncWidget::clear() { - for (openspace::DownloadManager::FileFuture* f : _futures) + for (std::shared_ptr f : _futures) f->abortDownload = true; using libtorrent::torrent_handle; @@ -254,7 +254,7 @@ void SyncWidget::handleDirectFiles() { for (const DirectFile& f : _directFiles) { LDEBUG(f.url.toStdString() << " -> " << f.destination.toStdString()); - openspace::DownloadManager::FileFuture* future = DlManager.downloadFile( + std::shared_ptr future = DlManager.downloadFile( f.url.toStdString(), absPath("${SCENE}/" + f.module.toStdString() + "/" + f.destination.toStdString()), OverwriteFiles @@ -571,8 +571,8 @@ void SyncWidget::handleTimer() { using namespace libtorrent; using FileFuture = openspace::DownloadManager::FileFuture; - std::vector toRemove; - for (FileFuture* f : _futures) { + std::vector> toRemove; + for (std::shared_ptr f : _futures) { InfoWidget* w = _futureInfoWidgetMap[f]; if (CleanInfoWidgets && (f->isFinished || f->isAborted)) { @@ -585,13 +585,12 @@ void SyncWidget::handleTimer() { w->update(f); } - for (FileFuture* f : toRemove) { + for (std::shared_ptr f : toRemove) { _futures.erase(std::remove(_futures.begin(), _futures.end(), f), _futures.end()); - delete f; } while (_mutex.test_and_set()) {} - for (openspace::DownloadManager::FileFuture* f : _futuresToAdd) { + for (std::shared_ptr f : _futuresToAdd) { InfoWidget* w = new InfoWidget(QString::fromStdString(f->filePath), -1); _downloadLayout->insertWidget(_downloadLayout->count() - 1, w); @@ -679,7 +678,7 @@ void SyncWidget::handleTimer() { } void SyncWidget::handleFileFutureAddition( - const std::vector& futures) + const std::vector>& futures) { while (_mutex.test_and_set()) {} _futuresToAdd.insert(_futuresToAdd.end(), futures.begin(), futures.end()); diff --git a/apps/Launcher/syncwidget.h b/apps/Launcher/syncwidget.h index b4b2c108e4..e17f54be7f 100644 --- a/apps/Launcher/syncwidget.h +++ b/apps/Launcher/syncwidget.h @@ -83,7 +83,7 @@ private: void clear(); QStringList selectedScenes() const; - void handleFileFutureAddition(const std::vector& futures); + void handleFileFutureAddition(const std::vector>& futures); void handleDirectFiles(); void handleFileRequest(); @@ -101,10 +101,10 @@ private: QList _fileRequests; QList _torrentFiles; - std::vector _futures; - std::map _futureInfoWidgetMap; + std::vector> _futures; + std::map, InfoWidget*> _futureInfoWidgetMap; - std::vector _futuresToAdd; + std::vector> _futuresToAdd; std::atomic_flag _mutex; }; diff --git a/include/openspace/engine/downloadmanager.h b/include/openspace/engine/downloadmanager.h index 653dd1edd1..53b90cf683 100644 --- a/include/openspace/engine/downloadmanager.h +++ b/include/openspace/engine/downloadmanager.h @@ -63,20 +63,20 @@ public: using DownloadProgressCallback = std::function; using DownloadFinishedCallback = std::function; using AsyncDownloadFinishedCallback = - std::function&)>; + std::function>&)>; DownloadManager(std::string requestURL, int applicationVersion, bool useMultithreadedDownload = true); // callers responsibility to delete // callbacks happen on a different thread - FileFuture* downloadFile(const std::string& url, const ghoul::filesystem::File& file, + std::shared_ptr downloadFile(const std::string& url, const ghoul::filesystem::File& file, bool overrideFile = true, DownloadFinishedCallback finishedCallback = DownloadFinishedCallback(), DownloadProgressCallback progressCallback = DownloadProgressCallback() ); - std::vector downloadRequestFiles(const std::string& identifier, + std::vector> downloadRequestFiles(const std::string& identifier, const ghoul::filesystem::Directory& destination, int version, bool overrideFiles = true, DownloadFinishedCallback finishedCallback = DownloadFinishedCallback(), diff --git a/src/engine/downloadmanager.cpp b/src/engine/downloadmanager.cpp index 7c69aa0638..e4f608fd36 100644 --- a/src/engine/downloadmanager.cpp +++ b/src/engine/downloadmanager.cpp @@ -50,7 +50,7 @@ namespace { const std::string RequestApplicationVersion = "application_version"; struct ProgressInformation { - openspace::DownloadManager::FileFuture* future; + std::shared_ptr future; std::chrono::system_clock::time_point startTime; const openspace::DownloadManager::DownloadProgressCallback* callback; }; @@ -133,14 +133,14 @@ DownloadManager::DownloadManager(std::string requestURL, int applicationVersion, // TODO: Allow for multiple requestURLs } -DownloadManager::FileFuture* DownloadManager::downloadFile( +std::shared_ptr DownloadManager::downloadFile( const std::string& url, const ghoul::filesystem::File& file, bool overrideFile, DownloadFinishedCallback finishedCallback, DownloadProgressCallback progressCallback) { if (!overrideFile && FileSys.fileExists(file)) return nullptr; - FileFuture* future = new FileFuture(file.filename()); + std::shared_ptr future = std::make_shared(file.filename()); FILE* fp = fopen(file.path().c_str(), "wb"); LDEBUG("Start downloading file: '" << url << "' into file '" << file.path() << "'"); @@ -196,12 +196,12 @@ DownloadManager::FileFuture* DownloadManager::downloadFile( return future; } -std::vector DownloadManager::downloadRequestFiles( +std::vector> DownloadManager::downloadRequestFiles( const std::string& identifier, const ghoul::filesystem::Directory& destination, int version, bool overrideFiles, DownloadFinishedCallback finishedCallback, DownloadProgressCallback progressCallback) { - std::vector futures; + std::vector> futures; FileSys.createDirectory(destination, ghoul::filesystem::FileSystem::Recursive::Yes); // TODO: Check s ---abock // TODO: Escaping is necessary ---abock @@ -232,7 +232,7 @@ std::vector DownloadManager::downloadRequestFiles( LDEBUG("\tLine: " << line << " ; Dest: " << destination.path() + "/" + file); - FileFuture* future = DlManager.downloadFile( + std::shared_ptr future = DlManager.downloadFile( line, destination.path() + "/" + file, overrideFiles, @@ -244,7 +244,7 @@ std::vector DownloadManager::downloadRequestFiles( isFinished = true; }; - FileFuture* f = downloadFile( + std::shared_ptr f = downloadFile( fullRequest, requestFile, true, @@ -261,7 +261,7 @@ void DownloadManager::downloadRequestFilesAsync(const std::string& identifier, AsyncDownloadFinishedCallback callback) { auto downloadFunction = [this, identifier, destination, version, overrideFiles, callback](){ - std::vector f = downloadRequestFiles( + std::vector> f = downloadRequestFiles( identifier, destination, version,