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); diskWriter_->truncate(length);
} }
std::shared_ptr<FileAllocationIterator> std::unique_ptr<FileAllocationIterator>
AbstractSingleDiskAdaptor::fileAllocationIterator() AbstractSingleDiskAdaptor::fileAllocationIterator()
{ {
switch(getFileAllocationMethod()) { switch(getFileAllocationMethod()) {
#ifdef HAVE_SOME_FALLOCATE #ifdef HAVE_SOME_FALLOCATE
case(DiskAdaptor::FILE_ALLOC_FALLOC): { case(DiskAdaptor::FILE_ALLOC_FALLOC):
std::shared_ptr<FallocFileAllocationIterator> h return make_unique<FallocFileAllocationIterator>
(new FallocFileAllocationIterator (diskWriter_.get(), size() ,totalLength_);
(diskWriter_.get(), size() ,totalLength_));
return h;
}
#endif // HAVE_SOME_FALLOCATE #endif // HAVE_SOME_FALLOCATE
case(DiskAdaptor::FILE_ALLOC_TRUNC): { case(DiskAdaptor::FILE_ALLOC_TRUNC):
std::shared_ptr<TruncFileAllocationIterator> h return make_unique<TruncFileAllocationIterator>
(new TruncFileAllocationIterator (diskWriter_.get(), size(), totalLength_);
(diskWriter_.get(), size(), totalLength_)); default:
return h; return make_unique<AdaptiveFileAllocationIterator>
} (diskWriter_.get(), size(), totalLength_);
default: {
std::shared_ptr<AdaptiveFileAllocationIterator> h
(new AdaptiveFileAllocationIterator
(diskWriter_.get(), size(), totalLength_));
return h;
}
} }
} }

View File

@ -73,7 +73,7 @@ public:
virtual void truncate(int64_t length); 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. // Make sure that DiskWriter is set before calling this function.
virtual void enableReadOnly(); virtual void enableReadOnly();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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