Use std::unique_ptr for FileAllocationIterator

pull/106/head
Tatsuhiro Tsujikawa 2013-07-05 21:54:07 +09:00
parent 345ba415a5
commit f8d305fe63
9 changed files with 31 additions and 38 deletions

View File

@ -145,30 +145,21 @@ void AbstractSingleDiskAdaptor::truncate(int64_t length)
diskWriter_->truncate(length);
}
std::shared_ptr<FileAllocationIterator>
std::unique_ptr<FileAllocationIterator>
AbstractSingleDiskAdaptor::fileAllocationIterator()
{
switch(getFileAllocationMethod()) {
#ifdef HAVE_SOME_FALLOCATE
case(DiskAdaptor::FILE_ALLOC_FALLOC): {
std::shared_ptr<FallocFileAllocationIterator> h
(new FallocFileAllocationIterator
(diskWriter_.get(), size() ,totalLength_));
return h;
}
case(DiskAdaptor::FILE_ALLOC_FALLOC):
return make_unique<FallocFileAllocationIterator>
(diskWriter_.get(), size() ,totalLength_);
#endif // HAVE_SOME_FALLOCATE
case(DiskAdaptor::FILE_ALLOC_TRUNC): {
std::shared_ptr<TruncFileAllocationIterator> h
(new TruncFileAllocationIterator
(diskWriter_.get(), size(), totalLength_));
return h;
}
default: {
std::shared_ptr<AdaptiveFileAllocationIterator> h
(new AdaptiveFileAllocationIterator
(diskWriter_.get(), size(), totalLength_));
return h;
}
case(DiskAdaptor::FILE_ALLOC_TRUNC):
return make_unique<TruncFileAllocationIterator>
(diskWriter_.get(), size(), totalLength_);
default:
return make_unique<AdaptiveFileAllocationIterator>
(diskWriter_.get(), size(), totalLength_);
}
}

View File

@ -73,7 +73,7 @@ public:
virtual void truncate(int64_t length);
virtual std::shared_ptr<FileAllocationIterator> fileAllocationIterator();
virtual std::unique_ptr<FileAllocationIterator> fileAllocationIterator();
// Make sure that DiskWriter is set before calling this function.
virtual void enableReadOnly();

View File

@ -38,6 +38,7 @@
#include "RecoverableException.h"
#include "LogFactory.h"
#include "Logger.h"
#include "a2functional.h"
#ifdef HAVE_FALLOCATE
# include "FallocFileAllocationIterator.h"
#endif // HAVE_FALLOCATE
@ -66,20 +67,20 @@ void AdaptiveFileAllocationIterator::allocateChunk()
offset_ += len;
}
A2_LOG_DEBUG("File system supports fallocate.");
allocator_.reset
(new FallocFileAllocationIterator(stream_, offset_, totalLength_));
allocator_ = make_unique<FallocFileAllocationIterator>
(stream_, offset_, totalLength_);
} catch(RecoverableException& e) {
A2_LOG_DEBUG("File system does not support fallocate.");
std::shared_ptr<SingleFileAllocationIterator> salloc
(new SingleFileAllocationIterator(stream_, offset_, totalLength_));
auto salloc = make_unique<SingleFileAllocationIterator>
(stream_, offset_, totalLength_);
salloc->init();
allocator_ = salloc;
allocator_ = std::move(salloc);
}
#else // !HAVE_FALLOCATE
std::shared_ptr<SingleFileAllocationIterator> salloc
(new SingleFileAllocationIterator(stream_, offset_, totalLength_));
auto salloc = make_unique<SingleFileAllocationIterator>
(stream_, offset_, totalLength_);
salloc->init();
allocator_ = salloc;
allocator_ = std::move(salloc);
#endif // !HAVE_FALLOCATE
allocator_->allocateChunk();
} else {

View File

@ -46,7 +46,7 @@ class BinaryStream;
class AdaptiveFileAllocationIterator:public FileAllocationIterator
{
private:
std::shared_ptr<FileAllocationIterator> allocator_;
std::unique_ptr<FileAllocationIterator> allocator_;
BinaryStream* stream_;

View File

@ -78,12 +78,12 @@ public:
fileEntries_.assign(first, last);
}
const std::vector<std::shared_ptr<FileEntry> >& getFileEntries() const
const std::vector<std::shared_ptr<FileEntry>>& getFileEntries() const
{
return fileEntries_;
}
virtual std::shared_ptr<FileAllocationIterator> fileAllocationIterator() = 0;
virtual std::unique_ptr<FileAllocationIterator> fileAllocationIterator() = 0;
virtual void enableReadOnly() {}

View File

@ -42,9 +42,10 @@
namespace aria2 {
FileAllocationEntry::FileAllocationEntry(RequestGroup* requestGroup,
std::unique_ptr<Command> nextCommand):
RequestGroupEntry(requestGroup, std::move(nextCommand)),
fileAllocationIterator_(requestGroup->getPieceStorage()->getDiskAdaptor()->fileAllocationIterator())
std::unique_ptr<Command> nextCommand)
: RequestGroupEntry{requestGroup, std::move(nextCommand)},
fileAllocationIterator_{requestGroup->getPieceStorage()->getDiskAdaptor()
->fileAllocationIterator()}
{}
FileAllocationEntry:: ~FileAllocationEntry()

View File

@ -50,7 +50,7 @@ class DownloadEngine;
class FileAllocationEntry : public RequestGroupEntry, public ProgressAwareEntry {
private:
std::shared_ptr<FileAllocationIterator> fileAllocationIterator_;
std::unique_ptr<FileAllocationIterator> fileAllocationIterator_;
public:
FileAllocationEntry(RequestGroup* requestGroup,
std::unique_ptr<Command> nextCommand =

View File

@ -481,10 +481,10 @@ int64_t MultiDiskAdaptor::size()
return size;
}
std::shared_ptr<FileAllocationIterator>
std::unique_ptr<FileAllocationIterator>
MultiDiskAdaptor::fileAllocationIterator()
{
return std::make_shared<MultiFileAllocationIterator>(this);
return make_unique<MultiFileAllocationIterator>(this);
}
void MultiDiskAdaptor::enableReadOnly()

View File

@ -150,7 +150,7 @@ public:
virtual int64_t size();
virtual std::shared_ptr<FileAllocationIterator> fileAllocationIterator();
virtual std::unique_ptr<FileAllocationIterator> fileAllocationIterator();
virtual void enableReadOnly();