Use std::unique_ptr for CheckIntegrityEntry and FileAllocationEntry

pull/106/head
Tatsuhiro Tsujikawa 2013-07-06 00:17:44 +09:00
parent c9e58779e1
commit 6b397c8125
24 changed files with 118 additions and 127 deletions

View File

@ -770,10 +770,11 @@ std::string AbstractCommand::resolveHostname
}
void AbstractCommand::prepareForNextAction
(const std::shared_ptr<CheckIntegrityEntry>& checkEntry)
(std::unique_ptr<CheckIntegrityEntry> checkEntry)
{
std::vector<std::unique_ptr<Command>> commands;
requestGroup_->processCheckIntegrityEntry(commands, checkEntry, e_);
requestGroup_->processCheckIntegrityEntry(commands, std::move(checkEntry),
e_);
e_->addCommand(std::move(commands));
e_->setNoWait(true);
}

View File

@ -179,8 +179,7 @@ public:
void setTimeout(time_t timeout) { timeout_ = timeout; }
void prepareForNextAction
(const std::shared_ptr<CheckIntegrityEntry>& checkEntry);
void prepareForNextAction(std::unique_ptr<CheckIntegrityEntry> checkEntry);
// Check if socket is connected. If socket is not connected and
// there are other addresses to try, command is created using

View File

@ -51,21 +51,22 @@ BtCheckIntegrityEntry::~BtCheckIntegrityEntry() {}
void BtCheckIntegrityEntry::onDownloadIncomplete
(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e)
{
const std::shared_ptr<PieceStorage>& ps = getRequestGroup()->getPieceStorage();
auto& ps = getRequestGroup()->getPieceStorage();
ps->onDownloadIncomplete();
if(getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY)) {
return;
}
const std::shared_ptr<DiskAdaptor>& diskAdaptor = ps->getDiskAdaptor();
const auto& diskAdaptor = ps->getDiskAdaptor();
if(diskAdaptor->isReadOnlyEnabled()) {
// Now reopen DiskAdaptor with read only disabled.
diskAdaptor->closeFile();
diskAdaptor->disableReadOnly();
diskAdaptor->openFile();
}
std::shared_ptr<BtFileAllocationEntry> entry
(new BtFileAllocationEntry(getRequestGroup()));
proceedFileAllocation(commands, entry, e);
proceedFileAllocation(commands,
make_unique<BtFileAllocationEntry>
(getRequestGroup()),
e);
}
void BtCheckIntegrityEntry::onDownloadFinished
@ -77,9 +78,10 @@ void BtCheckIntegrityEntry::onDownloadFinished
// behavior.
if(!getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY) &&
getRequestGroup()->getOption()->getAsBool(PREF_BT_HASH_CHECK_SEED)) {
std::shared_ptr<BtFileAllocationEntry> entry
(new BtFileAllocationEntry(getRequestGroup()));
proceedFileAllocation(commands, entry, e);
proceedFileAllocation(commands,
make_unique<BtFileAllocationEntry>
(getRequestGroup()),
e);
}
}

View File

@ -50,22 +50,23 @@ namespace aria2 {
CheckIntegrityCommand::CheckIntegrityCommand
(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e,
const std::shared_ptr<CheckIntegrityEntry>& entry):
RealtimeCommand(cuid, requestGroup, e),
entry_(entry)
CheckIntegrityEntry* entry)
: RealtimeCommand{cuid, requestGroup, e},
entry_{entry}
{}
CheckIntegrityCommand::~CheckIntegrityCommand() {}
CheckIntegrityCommand::~CheckIntegrityCommand()
{
getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry();
}
bool CheckIntegrityCommand::executeInternal()
{
if(getRequestGroup()->isHaltRequested()) {
getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry();
return true;
}
entry_->validateChunk();
if(entry_->finished()) {
getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry();
// Enable control file saving here. See also
// RequestGroup::processCheckIntegrityEntry() to know why this is
// needed.
@ -95,7 +96,6 @@ bool CheckIntegrityCommand::executeInternal()
bool CheckIntegrityCommand::handleException(Exception& e)
{
getDownloadEngine()->getCheckIntegrityMan()->dropPickedEntry();
A2_LOG_ERROR_EX(fmt(MSG_FILE_VALIDATION_FAILURE,
getCuid()),
e);

View File

@ -45,12 +45,12 @@ class CheckIntegrityEntry;
class CheckIntegrityCommand : public RealtimeCommand {
private:
std::shared_ptr<CheckIntegrityEntry> entry_;
CheckIntegrityEntry* entry_;
public:
CheckIntegrityCommand(cuid_t cuid,
RequestGroup* requestGroup,
DownloadEngine* e,
const std::shared_ptr<CheckIntegrityEntry>& entry);
CheckIntegrityEntry* entry);
virtual ~CheckIntegrityCommand();

View File

@ -47,17 +47,17 @@ CheckIntegrityDispatcherCommand::CheckIntegrityDispatcherCommand
(cuid_t cuid,
const std::shared_ptr<CheckIntegrityMan>& fileAllocMan,
DownloadEngine* e)
: SequentialDispatcherCommand<CheckIntegrityEntry>(cuid, fileAllocMan, e)
: SequentialDispatcherCommand<CheckIntegrityEntry>{cuid, fileAllocMan, e}
{
setStatusRealtime();
}
std::unique_ptr<Command> CheckIntegrityDispatcherCommand::createCommand
(const std::shared_ptr<CheckIntegrityEntry>& entry)
(CheckIntegrityEntry* entry)
{
cuid_t newCUID = getDownloadEngine()->newCUID();
A2_LOG_INFO(fmt("CUID#%" PRId64 " - Dispatching CheckIntegrityCommand CUID#%" PRId64 ".",
getCuid(), newCUID));
A2_LOG_INFO(fmt("CUID#%" PRId64 " - Dispatching CheckIntegrityCommand "
"CUID#%" PRId64 ".", getCuid(), newCUID));
return make_unique<CheckIntegrityCommand>
(newCUID, entry->getRequestGroup(), getDownloadEngine(), entry);
}

View File

@ -50,8 +50,7 @@ public:
const std::shared_ptr<CheckIntegrityMan>& checkMan,
DownloadEngine* e);
protected:
virtual std::unique_ptr<Command> createCommand
(const std::shared_ptr<CheckIntegrityEntry>& entry);
virtual std::unique_ptr<Command> createCommand(CheckIntegrityEntry* entry);
};
} // namespace aria2

View File

@ -86,11 +86,11 @@ void CheckIntegrityEntry::cutTrailingGarbage()
void CheckIntegrityEntry::proceedFileAllocation
(std::vector<std::unique_ptr<Command>>& commands,
const std::shared_ptr<FileAllocationEntry>& entry,
std::unique_ptr<FileAllocationEntry> entry,
DownloadEngine* e)
{
if(getRequestGroup()->needsFileAllocation()) {
e->getFileAllocationMan()->pushEntry(entry);
e->getFileAllocationMan()->pushEntry(std::move(entry));
} else {
entry->prepareForNextAction(commands, e);
}

View File

@ -56,7 +56,7 @@ protected:
void setValidator(std::unique_ptr<IteratableValidator> validator);
void proceedFileAllocation(std::vector<std::unique_ptr<Command>>& commands,
const std::shared_ptr<FileAllocationEntry>& entry,
std::unique_ptr<FileAllocationEntry> entry,
DownloadEngine* e);
public:
CheckIntegrityEntry(RequestGroup* requestGroup,

View File

@ -78,9 +78,10 @@ ChecksumCheckIntegrityEntry::onDownloadIncomplete
(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e)
{
if(redownload_) {
std::shared_ptr<FileAllocationEntry> entry
(new StreamFileAllocationEntry(getRequestGroup(), popNextCommand()));
proceedFileAllocation(commands, entry, e);
proceedFileAllocation(commands,
make_unique<StreamFileAllocationEntry>
(getRequestGroup(), popNextCommand()),
e);
}
}

View File

@ -330,8 +330,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
}
{
const std::shared_ptr<FileAllocationEntry>& entry =
e->getFileAllocationMan()->getPickedEntry();
auto& entry = e->getFileAllocationMan()->getPickedEntry();
if(entry) {
o << " [FileAlloc:#"
<< GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " "
@ -350,8 +349,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e)
}
#ifdef ENABLE_MESSAGE_DIGEST
{
const std::shared_ptr<CheckIntegrityEntry>& entry =
e->getCheckIntegrityMan()->getPickedEntry();
auto& entry = e->getCheckIntegrityMan()->getPickedEntry();
if(entry) {
o << " [Checksum:#"
<< GroupId::toAbbrevHex(entry->getRequestGroup()->getGID()) << " "

View File

@ -327,12 +327,12 @@ bool DownloadCommand::prepareForNextSegment() {
}
#ifdef ENABLE_MESSAGE_DIGEST
if(getDownloadContext()->getPieceHashType().empty()) {
std::shared_ptr<CheckIntegrityEntry> entry
(new ChecksumCheckIntegrityEntry(getRequestGroup()));
auto entry = make_unique<ChecksumCheckIntegrityEntry>(getRequestGroup());
if(entry->isValidationReady()) {
entry->initValidator();
entry->cutTrailingGarbage();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry);
getDownloadEngine()->getCheckIntegrityMan()->pushEntry
(std::move(entry));
}
}
#endif // ENABLE_MESSAGE_DIGEST

View File

@ -54,16 +54,19 @@ namespace aria2 {
FileAllocationCommand::FileAllocationCommand
(cuid_t cuid, RequestGroup* requestGroup, DownloadEngine* e,
const std::shared_ptr<FileAllocationEntry>& fileAllocationEntry):
RealtimeCommand(cuid, requestGroup, e),
fileAllocationEntry_(fileAllocationEntry) {}
FileAllocationEntry* fileAllocationEntry)
: RealtimeCommand{cuid, requestGroup, e},
fileAllocationEntry_{fileAllocationEntry}
{}
FileAllocationCommand::~FileAllocationCommand() {}
FileAllocationCommand::~FileAllocationCommand()
{
getDownloadEngine()->getFileAllocationMan()->dropPickedEntry();
}
bool FileAllocationCommand::executeInternal()
{
if(getRequestGroup()->isHaltRequested()) {
getDownloadEngine()->getFileAllocationMan()->dropPickedEntry();
return true;
}
fileAllocationEntry_->allocateChunk();
@ -72,8 +75,6 @@ bool FileAllocationCommand::executeInternal()
(fmt(MSG_ALLOCATION_COMPLETED,
static_cast<long int>(timer_.difference(global::wallclock())),
getRequestGroup()->getTotalLength()));
getDownloadEngine()->getFileAllocationMan()->dropPickedEntry();
std::vector<std::unique_ptr<Command>> commands;
fileAllocationEntry_->prepareForNextAction(commands, getDownloadEngine());
getDownloadEngine()->addCommand(std::move(commands));
@ -87,7 +88,6 @@ bool FileAllocationCommand::executeInternal()
bool FileAllocationCommand::handleException(Exception& e)
{
getDownloadEngine()->getFileAllocationMan()->dropPickedEntry();
A2_LOG_ERROR_EX(fmt(MSG_FILE_ALLOCATION_FAILURE,
getCuid()),
e);

View File

@ -47,12 +47,12 @@ class FileAllocationEntry;
class FileAllocationCommand : public RealtimeCommand {
private:
std::shared_ptr<FileAllocationEntry> fileAllocationEntry_;
FileAllocationEntry* fileAllocationEntry_;
Timer timer_;
public:
FileAllocationCommand(cuid_t cuid, RequestGroup* requestGroup,
DownloadEngine* e,
const std::shared_ptr<FileAllocationEntry>& fileAllocationEntry);
FileAllocationEntry* fileAllocationEntry);
virtual ~FileAllocationCommand();

View File

@ -47,11 +47,11 @@ FileAllocationDispatcherCommand::FileAllocationDispatcherCommand
(cuid_t cuid,
const std::shared_ptr<FileAllocationMan>& fileAllocMan,
DownloadEngine* e)
: SequentialDispatcherCommand<FileAllocationEntry>(cuid, fileAllocMan, e)
: SequentialDispatcherCommand<FileAllocationEntry>{cuid, fileAllocMan, e}
{}
std::unique_ptr<Command> FileAllocationDispatcherCommand::createCommand
(const std::shared_ptr<FileAllocationEntry>& entry)
(FileAllocationEntry* entry)
{
cuid_t newCUID = getDownloadEngine()->newCUID();
A2_LOG_INFO(fmt(MSG_FILE_ALLOCATION_DISPATCH, newCUID));

View File

@ -50,8 +50,7 @@ public:
const std::shared_ptr<FileAllocationMan>& fileAllocMan,
DownloadEngine* e);
protected:
virtual std::unique_ptr<Command> createCommand
(const std::shared_ptr<FileAllocationEntry>& entry);
virtual std::unique_ptr<Command> createCommand(FileAllocationEntry* entry);
};
} // namespace aria2

View File

@ -400,11 +400,12 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
getRequestGroup()->initPieceStorage();
if(getDownloadContext()->isChecksumVerificationNeeded()) {
A2_LOG_DEBUG("Zero length file exists. Verify checksum.");
std::shared_ptr<ChecksumCheckIntegrityEntry> entry
(new ChecksumCheckIntegrityEntry(getRequestGroup()));
auto entry = make_unique<ChecksumCheckIntegrityEntry>
(getRequestGroup());
entry->initValidator();
getPieceStorage()->getDiskAdaptor()->openExistingFile();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry);
getDownloadEngine()->getCheckIntegrityMan()->pushEntry
(std::move(entry));
sequence_ = SEQ_EXIT;
} else
#endif // ENABLE_MESSAGE_DIGEST
@ -434,10 +435,11 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
// HttpResponseCommand::handleOtherEncoding()
if(getDownloadContext()->isChecksumVerificationNeeded()) {
A2_LOG_DEBUG("Verify checksum for zero-length file");
std::shared_ptr<ChecksumCheckIntegrityEntry> entry
(new ChecksumCheckIntegrityEntry(getRequestGroup()));
auto entry = make_unique<ChecksumCheckIntegrityEntry>
(getRequestGroup());
entry->initValidator();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry);
getDownloadEngine()->getCheckIntegrityMan()->pushEntry
(std::move(entry));
sequence_ = SEQ_EXIT;
} else
#endif // ENABLE_MESSAGE_DIGEST
@ -465,8 +467,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
return false;
}
std::shared_ptr<CheckIntegrityEntry> checkIntegrityEntry =
getRequestGroup()->createCheckIntegrityEntry();
auto checkIntegrityEntry = getRequestGroup()->createCheckIntegrityEntry();
if(!checkIntegrityEntry) {
sequence_ = SEQ_DOWNLOAD_ALREADY_COMPLETED;
poolConnection();
@ -478,7 +479,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(int64_t totalLength)
// AbstractCommand::execute()
getSegmentMan()->getSegmentWithIndex(getCuid(), 0);
prepareForNextAction(checkIntegrityEntry);
prepareForNextAction(std::move(checkIntegrityEntry));
disableReadCheckSocket();
}

View File

@ -377,7 +377,7 @@ bool HttpResponseCommand::handleDefaultEncoding
getFileEntry()->poolRequest(getRequest());
}
prepareForNextAction(checkEntry);
prepareForNextAction(std::move(checkEntry));
if(getRequest()->getMethod() == Request::METHOD_HEAD) {
poolConnection();
@ -421,11 +421,11 @@ bool HttpResponseCommand::handleOtherEncoding
// See also FtpNegotiationCommand::onFileSizeDetermined()
if(getDownloadContext()->isChecksumVerificationNeeded()) {
A2_LOG_DEBUG("Zero length file exists. Verify checksum.");
std::shared_ptr<ChecksumCheckIntegrityEntry> entry
(new ChecksumCheckIntegrityEntry(getRequestGroup()));
auto entry = make_unique<ChecksumCheckIntegrityEntry>
(getRequestGroup());
entry->initValidator();
getPieceStorage()->getDiskAdaptor()->openExistingFile();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry);
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry));
} else
#endif // ENABLE_MESSAGE_DIGEST
{
@ -455,10 +455,10 @@ bool HttpResponseCommand::handleOtherEncoding
#ifdef ENABLE_MESSAGE_DIGEST
if(getDownloadContext()->isChecksumVerificationNeeded()) {
A2_LOG_DEBUG("Verify checksum for zero-length file");
auto entry = std::make_shared<ChecksumCheckIntegrityEntry>
auto entry = make_unique<ChecksumCheckIntegrityEntry>
(getRequestGroup());
entry->initValidator();
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry);
getDownloadEngine()->getCheckIntegrityMan()->pushEntry(std::move(entry));
} else
#endif // ENABLE_MESSAGE_DIGEST
{

View File

@ -216,18 +216,16 @@ void RequestGroup::closeFile()
// TODO The function name is not intuitive at all.. it does not convey
// that this function open file.
std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
std::unique_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
{
std::shared_ptr<BtProgressInfoFile> infoFile
(new DefaultBtProgressInfoFile(downloadContext_, pieceStorage_,
option_.get()));
std::shared_ptr<CheckIntegrityEntry> checkEntry;
auto infoFile = std::make_shared<DefaultBtProgressInfoFile>
(downloadContext_, pieceStorage_, option_.get());
if(option_->getAsBool(PREF_CHECK_INTEGRITY) &&
downloadContext_->isPieceHashVerificationAvailable()) {
// When checking piece hash, we don't care file is downloaded and
// infoFile exists.
loadAndOpenFile(infoFile);
checkEntry.reset(new StreamCheckIntegrityEntry(this));
return make_unique<StreamCheckIntegrityEntry>(this);
} else if(isPreLocalFileCheckEnabled() &&
(infoFile->exists() ||
(File(getFirstFilePath()).exists() &&
@ -242,10 +240,9 @@ std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
#ifdef ENABLE_MESSAGE_DIGEST
if(downloadContext_->isChecksumVerificationNeeded()) {
A2_LOG_INFO(MSG_HASH_CHECK_NOT_DONE);
ChecksumCheckIntegrityEntry* tempEntry =
new ChecksumCheckIntegrityEntry(this);
auto tempEntry = make_unique<ChecksumCheckIntegrityEntry>(this);
tempEntry->setRedownload(true);
checkEntry.reset(tempEntry);
return std::move(tempEntry);
} else
#endif // ENABLE_MESSAGE_DIGEST
{
@ -253,9 +250,10 @@ std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
gid_->toHex().c_str(),
downloadContext_->getBasePath().c_str()));
return nullptr;
}
} else {
checkEntry.reset(new StreamCheckIntegrityEntry(this));
return make_unique<StreamCheckIntegrityEntry>(this);
}
} else
#ifdef ENABLE_MESSAGE_DIGEST
@ -263,17 +261,15 @@ std::shared_ptr<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
downloadContext_->isChecksumVerificationAvailable()) {
pieceStorage_->markAllPiecesDone();
loadAndOpenFile(infoFile);
ChecksumCheckIntegrityEntry* tempEntry =
new ChecksumCheckIntegrityEntry(this);
auto tempEntry = make_unique<ChecksumCheckIntegrityEntry>(this);
tempEntry->setRedownload(true);
checkEntry.reset(tempEntry);
return std::move(tempEntry);
} else
#endif // ENABLE_MESSAGE_DIGEST
{
loadAndOpenFile(infoFile);
checkEntry.reset(new StreamCheckIntegrityEntry(this));
return make_unique<StreamCheckIntegrityEntry>(this);
}
return checkEntry;
}
void RequestGroup::createInitialCommand
@ -379,11 +375,7 @@ void RequestGroup::createInitialCommand
A2_LOG_NOTICE(_("For BitTorrent Magnet URI, enabling DHT is strongly"
" recommended. See --enable-dht option."));
}
std::shared_ptr<CheckIntegrityEntry> entry
(new BtCheckIntegrityEntry(this));
entry->onDownloadIncomplete(commands, e);
BtCheckIntegrityEntry{this}.onDownloadIncomplete(commands, e);
return;
}
removeDefunctControlFile(progressInfoFile);
@ -454,14 +446,14 @@ void RequestGroup::createInitialCommand
e->addCommand(std::move(command));
}
}
std::shared_ptr<CheckIntegrityEntry> entry(new BtCheckIntegrityEntry(this));
auto entry = make_unique<BtCheckIntegrityEntry>(this);
// --bt-seed-unverified=true is given and download has completed, skip
// validation for piece hashes.
if(option_->getAsBool(PREF_BT_SEED_UNVERIFIED) &&
pieceStorage_->downloadFinished()) {
entry->onDownloadFinished(commands, e);
} else {
processCheckIntegrityEntry(commands, entry, e);
processCheckIntegrityEntry(commands, std::move(entry), e);
}
return;
}
@ -485,10 +477,9 @@ void RequestGroup::createInitialCommand
(downloadContext_, std::shared_ptr<PieceStorage>(), option_.get()));
adjustFilename(progressInfoFile);
initPieceStorage();
std::shared_ptr<CheckIntegrityEntry> checkEntry =
createCheckIntegrityEntry();
auto checkEntry = createCheckIntegrityEntry();
if(checkEntry) {
processCheckIntegrityEntry(commands, checkEntry, e);
processCheckIntegrityEntry(commands, std::move(checkEntry), e);
}
}
} else {
@ -538,15 +529,15 @@ void RequestGroup::createInitialCommand
}
}
progressInfoFile_ = progressInfoFile;
std::shared_ptr<CheckIntegrityEntry> checkIntegrityEntry
(new StreamCheckIntegrityEntry(this));
processCheckIntegrityEntry(commands, checkIntegrityEntry, e);
processCheckIntegrityEntry(commands,
make_unique<StreamCheckIntegrityEntry>(this),
e);
}
}
void RequestGroup::processCheckIntegrityEntry
(std::vector<std::unique_ptr<Command>>& commands,
const std::shared_ptr<CheckIntegrityEntry>& entry,
std::unique_ptr<CheckIntegrityEntry> entry,
DownloadEngine* e)
{
int64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size();
@ -565,7 +556,7 @@ void RequestGroup::processCheckIntegrityEntry
// enableSaveControlFile() will be called after hash checking is
// done. See CheckIntegrityCommand.
disableSaveControlFile();
e->getCheckIntegrityMan()->pushEntry(entry);
e->getCheckIntegrityMan()->pushEntry(std::move(entry));
} else
#endif // ENABLE_MESSAGE_DIGEST
{

View File

@ -210,7 +210,7 @@ public:
return segmentMan_;
}
std::shared_ptr<CheckIntegrityEntry> createCheckIntegrityEntry();
std::unique_ptr<CheckIntegrityEntry> createCheckIntegrityEntry();
// Returns first bootstrap commands to initiate a download.
// If this is HTTP/FTP download and file size is unknown, only 1 command
@ -387,7 +387,7 @@ public:
void processCheckIntegrityEntry
(std::vector<std::unique_ptr<Command>>& commands,
const std::shared_ptr<CheckIntegrityEntry>& entry,
std::unique_ptr<CheckIntegrityEntry> entry,
DownloadEngine* e);
// Initializes pieceStorage_ and segmentMan_. We guarantee that

View File

@ -59,10 +59,11 @@ protected:
return e_;
}
public:
SequentialDispatcherCommand(cuid_t cuid,
const std::shared_ptr<SequentialPicker<T> >& picker,
DownloadEngine* e):
Command(cuid), picker_(picker), e_(e)
SequentialDispatcherCommand
(cuid_t cuid,
const std::shared_ptr<SequentialPicker<T>>& picker,
DownloadEngine* e)
: Command{cuid}, picker_{picker}, e_{e}
{
setStatusRealtime();
}
@ -83,8 +84,7 @@ public:
}
protected:
virtual std::unique_ptr<Command> createCommand
(const std::shared_ptr<T>& entry) = 0;
virtual std::unique_ptr<Command> createCommand(T* entry) = 0;
};
} // namespace aria2

View File

@ -45,15 +45,15 @@ namespace aria2 {
template<typename T>
class SequentialPicker {
private:
std::deque<std::shared_ptr<T> > entries_;
std::shared_ptr<T> pickedEntry_;
std::deque<std::unique_ptr<T>> entries_;
std::unique_ptr<T> pickedEntry_;
public:
bool isPicked() const
{
return pickedEntry_.get();
}
const std::shared_ptr<T>& getPickedEntry() const
const std::unique_ptr<T>& getPickedEntry() const
{
return pickedEntry_;
}
@ -68,20 +68,19 @@ public:
return !entries_.empty();
}
std::shared_ptr<T> pickNext()
T* pickNext()
{
std::shared_ptr<T> r;
if(hasNext()) {
r = entries_.front();
pickedEntry_ = std::move(entries_.front());
entries_.pop_front();
pickedEntry_ = r;
return pickedEntry_.get();
}
return r;
return nullptr;
}
void pushEntry(const std::shared_ptr<T>& entry)
void pushEntry(std::unique_ptr<T> entry)
{
entries_.push_back(entry);
entries_.push_back(std::move(entry));
}
size_t countEntryInQueue() const

View File

@ -52,14 +52,15 @@ StreamCheckIntegrityEntry::~StreamCheckIntegrityEntry() {}
void StreamCheckIntegrityEntry::onDownloadIncomplete
(std::vector<std::unique_ptr<Command>>& commands, DownloadEngine* e)
{
const std::shared_ptr<PieceStorage>& ps = getRequestGroup()->getPieceStorage();
auto& ps = getRequestGroup()->getPieceStorage();
ps->onDownloadIncomplete();
if(getRequestGroup()->getOption()->getAsBool(PREF_HASH_CHECK_ONLY)) {
return;
}
std::shared_ptr<FileAllocationEntry> entry
(new StreamFileAllocationEntry(getRequestGroup(), popNextCommand()));
proceedFileAllocation(commands, entry, e);
proceedFileAllocation(commands,
make_unique<StreamFileAllocationEntry>
(getRequestGroup(), popNextCommand()),
e);
}
void StreamCheckIntegrityEntry::onDownloadFinished

View File

@ -2,9 +2,9 @@
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
#include "a2functional.h"
typedef std::shared_ptr<int> Integer;
namespace aria2 {
class SequentialPickerTest:public CppUnit::TestFixture {
@ -26,8 +26,8 @@ void SequentialPickerTest::testPick()
CPPUNIT_ASSERT(!picker.hasNext());
CPPUNIT_ASSERT_EQUAL((size_t)0, picker.countEntryInQueue());
picker.pushEntry(Integer(new int(1)));
picker.pushEntry(Integer(new int(2)));
picker.pushEntry(make_unique<int>(1));
picker.pushEntry(make_unique<int>(2));
CPPUNIT_ASSERT(picker.hasNext());
CPPUNIT_ASSERT_EQUAL((size_t)2, picker.countEntryInQueue());
@ -35,7 +35,7 @@ void SequentialPickerTest::testPick()
picker.pickNext();
CPPUNIT_ASSERT(picker.isPicked());
CPPUNIT_ASSERT_EQUAL(*Integer(new int(1)), *picker.getPickedEntry());
CPPUNIT_ASSERT_EQUAL(1, *picker.getPickedEntry());
picker.dropPickedEntry();
@ -44,7 +44,7 @@ void SequentialPickerTest::testPick()
picker.pickNext();
CPPUNIT_ASSERT_EQUAL(*Integer(new int(2)), *picker.getPickedEntry());
CPPUNIT_ASSERT_EQUAL(2, *picker.getPickedEntry());
CPPUNIT_ASSERT(!picker.hasNext());
}