downloadmanager return shared_ptr to FileFutures instead of bare pointers

This commit is contained in:
Michael Nilsson
2016-04-05 11:33:58 -04:00
parent e23a1f3e3c
commit 0d0f761d6e
6 changed files with 24 additions and 25 deletions

View File

@@ -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<openspace::DownloadManager::FileFuture> f) {
_bytes->setText(
QString("%1 / %2")
.arg(f->currentSize)

View File

@@ -40,7 +40,7 @@ Q_OBJECT
public:
InfoWidget(QString name, int totalBytes = -1);
void update(openspace::DownloadManager::FileFuture* f);
void update(std::shared_ptr<openspace::DownloadManager::FileFuture> f);
void update(libtorrent::torrent_status s);
void error(QString message);

View File

@@ -230,7 +230,7 @@ void SyncWidget::setSceneFiles(QMap<QString, QString> sceneFiles) {
}
void SyncWidget::clear() {
for (openspace::DownloadManager::FileFuture* f : _futures)
for (std::shared_ptr<openspace::DownloadManager::FileFuture> 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<openspace::DownloadManager::FileFuture> 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<FileFuture*> toRemove;
for (FileFuture* f : _futures) {
std::vector<std::shared_ptr<FileFuture>> toRemove;
for (std::shared_ptr<FileFuture> 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<FileFuture> 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<FileFuture> 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<openspace::DownloadManager::FileFuture*>& futures)
const std::vector<std::shared_ptr<openspace::DownloadManager::FileFuture>>& futures)
{
while (_mutex.test_and_set()) {}
_futuresToAdd.insert(_futuresToAdd.end(), futures.begin(), futures.end());

View File

@@ -83,7 +83,7 @@ private:
void clear();
QStringList selectedScenes() const;
void handleFileFutureAddition(const std::vector<openspace::DownloadManager::FileFuture*>& futures);
void handleFileFutureAddition(const std::vector<std::shared_ptr<openspace::DownloadManager::FileFuture>>& futures);
void handleDirectFiles();
void handleFileRequest();
@@ -101,10 +101,10 @@ private:
QList<FileRequest> _fileRequests;
QList<TorrentFile> _torrentFiles;
std::vector<openspace::DownloadManager::FileFuture*> _futures;
std::map<openspace::DownloadManager::FileFuture*, InfoWidget*> _futureInfoWidgetMap;
std::vector<std::shared_ptr<openspace::DownloadManager::FileFuture>> _futures;
std::map<std::shared_ptr<openspace::DownloadManager::FileFuture>, InfoWidget*> _futureInfoWidgetMap;
std::vector<openspace::DownloadManager::FileFuture*> _futuresToAdd;
std::vector<std::shared_ptr<openspace::DownloadManager::FileFuture>> _futuresToAdd;
std::atomic_flag _mutex;
};

View File

@@ -63,20 +63,20 @@ public:
using DownloadProgressCallback = std::function<void(const FileFuture&)>;
using DownloadFinishedCallback = std::function<void(const FileFuture&)>;
using AsyncDownloadFinishedCallback =
std::function<void(const std::vector<FileFuture*>&)>;
std::function<void(const std::vector<std::shared_ptr<FileFuture>>&)>;
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<FileFuture> downloadFile(const std::string& url, const ghoul::filesystem::File& file,
bool overrideFile = true,
DownloadFinishedCallback finishedCallback = DownloadFinishedCallback(),
DownloadProgressCallback progressCallback = DownloadProgressCallback()
);
std::vector<FileFuture*> downloadRequestFiles(const std::string& identifier,
std::vector<std::shared_ptr<FileFuture>> downloadRequestFiles(const std::string& identifier,
const ghoul::filesystem::Directory& destination, int version,
bool overrideFiles = true,
DownloadFinishedCallback finishedCallback = DownloadFinishedCallback(),

View File

@@ -50,7 +50,7 @@ namespace {
const std::string RequestApplicationVersion = "application_version";
struct ProgressInformation {
openspace::DownloadManager::FileFuture* future;
std::shared_ptr<openspace::DownloadManager::FileFuture> 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::FileFuture> 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<FileFuture> future = std::make_shared<FileFuture>(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::FileFuture*> DownloadManager::downloadRequestFiles(
std::vector<std::shared_ptr<DownloadManager::FileFuture>> DownloadManager::downloadRequestFiles(
const std::string& identifier, const ghoul::filesystem::Directory& destination,
int version, bool overrideFiles, DownloadFinishedCallback finishedCallback,
DownloadProgressCallback progressCallback)
{
std::vector<FileFuture*> futures;
std::vector<std::shared_ptr<FileFuture>> 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::FileFuture*> DownloadManager::downloadRequestFiles(
LDEBUG("\tLine: " << line << " ; Dest: " << destination.path() + "/" + file);
FileFuture* future = DlManager.downloadFile(
std::shared_ptr<FileFuture> future = DlManager.downloadFile(
line,
destination.path() + "/" + file,
overrideFiles,
@@ -244,7 +244,7 @@ std::vector<DownloadManager::FileFuture*> DownloadManager::downloadRequestFiles(
isFinished = true;
};
FileFuture* f = downloadFile(
std::shared_ptr<FileFuture> 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<FileFuture*> f = downloadRequestFiles(
std::vector<std::shared_ptr<FileFuture>> f = downloadRequestFiles(
identifier,
destination,
version,