Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#168: unique smart pointers for local variables
Browse files Browse the repository at this point in the history
  • Loading branch information
hugbug committed Feb 17, 2016
1 parent 36fe905 commit c96d225
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 152 deletions.
6 changes: 2 additions & 4 deletions daemon/connect/Connection.cpp
Expand Up @@ -455,7 +455,7 @@ char* Connection::ReadLine(char* buffer, int size, int* bytesReadOut)
return buffer;
}

Connection* Connection::Accept()
std::unique_ptr<Connection> Connection::Accept()
{
debug("Accepting connection");

Expand All @@ -474,9 +474,7 @@ Connection* Connection::Accept()
return nullptr;
}

Connection* con = new Connection(socket, m_tls);

return con;
return std::make_unique<Connection>(socket, m_tls);
}

int Connection::TryRecv(char* buffer, int size)
Expand Down
4 changes: 2 additions & 2 deletions daemon/connect/Connection.h
Expand Up @@ -97,7 +97,6 @@ class Connection
#endif
#endif

Connection(SOCKET socket, bool tls);
void ReportError(const char* msgPrefix, const char* msgArg, bool PrintErrCode, int herrno = 0, const char* herrMsg = nullptr);
virtual void PrintError(const char* errMsg);
bool DoConnect();
Expand All @@ -115,6 +114,7 @@ class Connection

public:
Connection(const char* host, int port, bool tls);
Connection(SOCKET socket, bool tls);
virtual ~Connection();
static void Init();
static void Final();
Expand All @@ -127,7 +127,7 @@ class Connection
char* ReadLine(char* buffer, int size, int* bytesRead);
void ReadBuffer(char** buffer, int *bufLen);
int WriteLine(const char* buffer);
Connection* Accept();
std::unique_ptr<Connection> Accept();
void Cancel();
const char* GetHost() { return m_host; }
int GetPort() { return m_port; }
Expand Down
32 changes: 15 additions & 17 deletions daemon/feed/FeedCoordinator.cpp
Expand Up @@ -362,16 +362,16 @@ void FeedCoordinator::FeedCompleted(FeedDownloader* feedDownloader)
FeedScriptController::ExecuteScripts(
!Util::EmptyStr(feedInfo->GetFeedScript()) ? feedInfo->GetFeedScript(): g_Options->GetFeedScript(),
feedInfo->GetOutputFilename(), feedInfo->GetId());
FeedFile* feedFile = FeedFile::Create(feedInfo->GetOutputFilename());
std::unique_ptr<FeedFile> feedFile = std::make_unique<FeedFile>(feedInfo->GetOutputFilename());
FileSystem::DeleteFile(feedInfo->GetOutputFilename());

NzbList addedNzbs;

m_downloadsMutex.Lock();
if (feedFile)
if (feedFile->Parse())
{
ProcessFeed(feedInfo, feedFile->GetFeedItemInfos(), &addedNzbs);
delete feedFile;
feedFile.reset();
}
feedInfo->SetLastUpdate(Util::CurrentTime());
feedInfo->SetForce(false);
Expand All @@ -398,10 +398,11 @@ void FeedCoordinator::FilterFeed(FeedInfo* feedInfo, FeedItemInfos* feedItemInfo
{
debug("Filtering feed %s", feedInfo->GetName());

FeedFilter* feedFilter = nullptr;
if (feedInfo->GetFilter() && strlen(feedInfo->GetFilter()) > 0)
std::unique_ptr<FeedFilter> feedFilter;

if (!Util::EmptyStr(feedInfo->GetFilter()))
{
feedFilter = new FeedFilter(feedInfo->GetFilter());
feedFilter = std::make_unique<FeedFilter>(feedInfo->GetFilter());
}

for (FeedItemInfo& feedItemInfo : feedItemInfos)
Expand All @@ -420,8 +421,6 @@ void FeedCoordinator::FilterFeed(FeedInfo* feedInfo, FeedItemInfos* feedItemInfo
feedFilter->Match(feedItemInfo);
}
}

delete feedFilter;
}

void FeedCoordinator::ProcessFeed(FeedInfo* feedInfo, FeedItemInfos* feedItemInfos, NzbList* addedNzbs)
Expand Down Expand Up @@ -525,7 +524,7 @@ bool FeedCoordinator::PreviewFeed(int id, const char* name, const char* url, con
{
debug("Preview feed %s", name);

FeedInfo* feedInfo = new FeedInfo(id, name, url, backlog, interval,
std::unique_ptr<FeedInfo> feedInfo = std::make_unique<FeedInfo>(id, name, url, backlog, interval,
filter, pauseNzb, category, priority, feedScript);
feedInfo->SetPreview(true);

Expand Down Expand Up @@ -564,7 +563,7 @@ bool FeedCoordinator::PreviewFeed(int id, const char* name, const char* url, con
}
}

StartFeedDownload(feedInfo, true);
StartFeedDownload(feedInfo.get(), true);
m_downloadsMutex.Unlock();

// wait until the download in a separate thread completes
Expand All @@ -575,27 +574,26 @@ bool FeedCoordinator::PreviewFeed(int id, const char* name, const char* url, con

// now can process the feed

FeedFile* feedFile = nullptr;
std::unique_ptr<FeedFile> feedFile;

if (feedInfo->GetStatus() == FeedInfo::fsFinished)
{
FeedScriptController::ExecuteScripts(
!Util::EmptyStr(feedInfo->GetFeedScript()) ? feedInfo->GetFeedScript(): g_Options->GetFeedScript(),
feedInfo->GetOutputFilename(), feedInfo->GetId());
feedFile = FeedFile::Create(feedInfo->GetOutputFilename());
feedFile = std::make_unique<FeedFile>(feedInfo->GetOutputFilename());
}

FileSystem::DeleteFile(feedInfo->GetOutputFilename());

if (!feedFile)
if (!feedFile || !feedFile->Parse())
{
delete feedInfo;
return false;
}

feedItemInfos = feedFile->GetFeedItemInfos();
feedItemInfos->Retain();
delete feedFile;
feedFile.reset();

for (FeedItemInfo& feedItemInfo : feedItemInfos)
{
Expand All @@ -608,8 +606,8 @@ bool FeedCoordinator::PreviewFeed(int id, const char* name, const char* url, con
}
}

FilterFeed(feedInfo, feedItemInfos);
delete feedInfo;
FilterFeed(feedInfo.get(), feedItemInfos);
feedInfo.reset();

if (cacheTimeSec > 0 && *cacheId != '\0' && !hasCache)
{
Expand Down
28 changes: 10 additions & 18 deletions daemon/feed/FeedFile.cpp
Expand Up @@ -91,7 +91,7 @@ void FeedFile::ParseSubject(FeedItemInfo& feedItemInfo)
}

#ifdef WIN32
FeedFile* FeedFile::Create(const char* fileName)
bool FeedFile::Parse()
{
CoInitialize(nullptr);

Expand All @@ -101,7 +101,7 @@ FeedFile* FeedFile::Create(const char* fileName)
hr = doc.CreateInstance(MSXML::CLSID_DOMDocument);
if (FAILED(hr))
{
return nullptr;
return false;
}

// Load the XML document file...
Expand Down Expand Up @@ -129,17 +129,12 @@ FeedFile* FeedFile::Create(const char* fileName)
_bstr_t r(doc->GetparseError()->reason);
const char* errMsg = r;
error("Error parsing rss feed: %s", errMsg);
return nullptr;
return false;
}

FeedFile* file = new FeedFile(fileName);
if (!file->ParseFeed(doc))
{
delete file;
file = nullptr;
}
bool ok = ParseFeed(doc);

return file;
return ok;
}

void FeedFile::EncodeUrl(const char* filename, char* url, int bufLen)
Expand Down Expand Up @@ -378,29 +373,26 @@ bool FeedFile::ParseFeed(IUnknown* nzb)

#else

FeedFile* FeedFile::Create(const char* fileName)
bool FeedFile::Parse()
{
FeedFile* file = new FeedFile(fileName);

xmlSAXHandler SAX_handler = {0};
SAX_handler.startElement = reinterpret_cast<startElementSAXFunc>(SAX_StartElement);
SAX_handler.endElement = reinterpret_cast<endElementSAXFunc>(SAX_EndElement);
SAX_handler.characters = reinterpret_cast<charactersSAXFunc>(SAX_characters);
SAX_handler.error = reinterpret_cast<errorSAXFunc>(SAX_error);
SAX_handler.getEntity = reinterpret_cast<getEntitySAXFunc>(SAX_getEntity);

file->m_ignoreNextError = false;
m_ignoreNextError = false;

int ret = xmlSAXUserParseFile(&SAX_handler, file, fileName);
int ret = xmlSAXUserParseFile(&SAX_handler, this, m_fileName);

if (ret != 0)
{
error("Failed to parse rss feed");
delete file;
file = nullptr;
return false;
}

return file;
return true;
}

void FeedFile::Parse_StartElement(const char *name, const char **atts)
Expand Down
4 changes: 2 additions & 2 deletions daemon/feed/FeedFile.h
Expand Up @@ -35,7 +35,6 @@ class FeedFile
FeedItemInfos* m_feedItemInfos;
CString m_fileName;

FeedFile(const char* fileName);
void ParseSubject(FeedItemInfo& feedItemInfo);
#ifdef WIN32
bool ParseFeed(IUnknown* nzb);
Expand All @@ -57,8 +56,9 @@ class FeedFile
#endif

public:
FeedFile(const char* fileName);
virtual ~FeedFile();
static FeedFile* Create(const char* fileName);
bool Parse();
FeedItemInfos* GetFeedItemInfos() { return m_feedItemInfos; }

void LogDebugInfo();
Expand Down
17 changes: 6 additions & 11 deletions daemon/queue/QueueEditor.cpp
Expand Up @@ -376,21 +376,19 @@ bool QueueEditor::EditList(DownloadQueue* downloadQueue, IdList* idList, NameLis
m_downloadQueue = downloadQueue;
bool ok = true;

std::unique_ptr<IdList> nameIdList;

if (nameList)
{
idList = new IdList();
nameIdList = std::make_unique<IdList>();
idList = nameIdList.get();
ok = BuildIdListFromNameList(idList, nameList, matchMode, action);
}

ok = ok && (InternEditList(nullptr, idList, action, offset, text) || matchMode == DownloadQueue::mmRegEx);

m_downloadQueue->Save();

if (nameList)
{
delete idList;
}

return ok;
}

Expand Down Expand Up @@ -714,13 +712,12 @@ bool QueueEditor::BuildIdListFromNameList(IdList* idList, NameList* nameList, Do

for (CString& name : nameList)
{
RegEx *regEx = nullptr;
std::unique_ptr<RegEx> regEx;
if (matchMode == DownloadQueue::mmRegEx)
{
regEx = new RegEx(name);
regEx = std::make_unique<RegEx>(name);
if (!regEx->IsValid())
{
delete regEx;
return false;
}
}
Expand Down Expand Up @@ -759,8 +756,6 @@ bool QueueEditor::BuildIdListFromNameList(IdList* idList, NameList* nameList, Do
}
}

delete regEx;

if (!found && (matchMode == DownloadQueue::mmName))
{
return false;
Expand Down
31 changes: 15 additions & 16 deletions daemon/remote/BinRpc.cpp
Expand Up @@ -210,64 +210,64 @@ void BinRpcProcessor::Dispatch()
return;
}

BinCommand* command = nullptr;
std::unique_ptr<BinCommand> command;

switch (ntohl(m_messageBase.m_type))
{
case rrDownload:
command = new DownloadBinCommand();
command = std::make_unique<DownloadBinCommand>();
break;

case rrList:
command = new ListBinCommand();
command = std::make_unique<ListBinCommand>();
break;

case rrLog:
command = new LogBinCommand();
command = std::make_unique<LogBinCommand>();
break;

case rrPauseUnpause:
command = new PauseUnpauseBinCommand();
command = std::make_unique<PauseUnpauseBinCommand>();
break;

case rrEditQueue:
command = new EditQueueBinCommand();
command = std::make_unique<EditQueueBinCommand>();
break;

case rrSetDownloadRate:
command = new SetDownloadRateBinCommand();
command = std::make_unique<SetDownloadRateBinCommand>();
break;

case rrDumpDebug:
command = new DumpDebugBinCommand();
command = std::make_unique<DumpDebugBinCommand>();
break;

case rrShutdown:
command = new ShutdownBinCommand();
command = std::make_unique<ShutdownBinCommand>();
break;

case rrReload:
command = new ReloadBinCommand();
command = std::make_unique<ReloadBinCommand>();
break;

case rrVersion:
command = new VersionBinCommand();
command = std::make_unique<VersionBinCommand>();
break;

case rrPostQueue:
command = new PostQueueBinCommand();
command = std::make_unique<PostQueueBinCommand>();
break;

case rrWriteLog:
command = new WriteLogBinCommand();
command = std::make_unique<WriteLogBinCommand>();
break;

case rrScan:
command = new ScanBinCommand();
command = std::make_unique<ScanBinCommand>();
break;

case rrHistory:
command = new HistoryBinCommand();
command = std::make_unique<HistoryBinCommand>();
break;

default:
Expand All @@ -280,7 +280,6 @@ void BinRpcProcessor::Dispatch()
command->SetConnection(m_connection);
command->SetMessageBase(&m_messageBase);
command->Execute();
delete command;
}
}

Expand Down

0 comments on commit c96d225

Please sign in to comment.