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 member fields
Browse files Browse the repository at this point in the history
  • Loading branch information
hugbug committed Feb 9, 2016
1 parent 8aa171f commit 695d67d
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 61 deletions.
10 changes: 2 additions & 8 deletions daemon/connect/Connection.cpp
Expand Up @@ -168,10 +168,6 @@ Connection::~Connection()
debug("Destroying Connection");

Disconnect();

#ifndef DISABLE_TLS
delete m_tlsSocket;
#endif
}

void Connection::SetSuppressErrors(bool suppressErrors)
Expand Down Expand Up @@ -911,8 +907,7 @@ bool Connection::StartTls(bool isClient, const char* certFile, const char* keyFi
{
debug("Starting TLS");

delete m_tlsSocket;
m_tlsSocket = new ConTlsSocket(m_socket, isClient, certFile, keyFile, m_cipher, this);
m_tlsSocket = std::make_unique<ConTlsSocket>(m_socket, isClient, certFile, keyFile, m_cipher, this);
m_tlsSocket->SetSuppressErrors(m_suppressErrors);

return m_tlsSocket->Start();
Expand All @@ -923,8 +918,7 @@ void Connection::CloseTls()
if (m_tlsSocket)
{
m_tlsSocket->Close();
delete m_tlsSocket;
m_tlsSocket = nullptr;
m_tlsSocket.reset();
}
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/connect/Connection.h
Expand Up @@ -88,8 +88,8 @@ class Connection
TlsSocket(socket, isClient, certFile, keyFile, cipher), m_owner(owner) {}
};

ConTlsSocket* m_tlsSocket;
bool m_tlsError;
std::unique_ptr<ConTlsSocket> m_tlsSocket;
bool m_tlsError;
#endif
#ifndef HAVE_GETADDRINFO
#ifndef HAVE_GETHOSTBYNAME_R
Expand Down
15 changes: 7 additions & 8 deletions daemon/connect/WebDownloader.cpp
Expand Up @@ -246,7 +246,7 @@ WebDownloader::EStatus WebDownloader::CreateConnection(URL *url)

bool tls = !strcasecmp(url->GetProtocol(), "https");

m_connection = new Connection(url->GetHost(), port, tls);
m_connection = std::make_unique<Connection>(url->GetHost(), port, tls);

return adRunning;
}
Expand Down Expand Up @@ -347,10 +347,10 @@ WebDownloader::EStatus WebDownloader::DownloadBody()
int writtenLen = 0;

#ifndef DISABLE_GZIP
m_gUnzipStream = nullptr;
m_gUnzipStream.reset();
if (m_gzip)
{
m_gUnzipStream = new GUnzipStream(1024*10);
m_gUnzipStream = std::make_unique<GUnzipStream>(1024*10);
}
#endif

Expand Down Expand Up @@ -402,7 +402,7 @@ WebDownloader::EStatus WebDownloader::DownloadBody()
}

#ifndef DISABLE_GZIP
delete m_gUnzipStream;
m_gUnzipStream.reset();
#endif

m_outFile.Close();
Expand Down Expand Up @@ -661,15 +661,15 @@ void WebDownloader::Stop()

bool WebDownloader::Terminate()
{
Connection* connection = m_connection;
std::unique_ptr<Connection> connection = std::move(m_connection);
bool terminated = Kill();
if (terminated && connection)
{
debug("Terminating connection");
connection->SetSuppressErrors(true);
connection->Cancel();
connection->Disconnect();
delete connection;
connection.reset();
}
return terminated;
}
Expand All @@ -684,8 +684,7 @@ void WebDownloader::FreeConnection()
{
m_connection->Disconnect();
}
delete m_connection;
m_connection = nullptr;
m_connection.reset();
m_connectionMutex.Unlock();
}
}
4 changes: 2 additions & 2 deletions daemon/connect/WebDownloader.h
Expand Up @@ -52,7 +52,7 @@ class WebDownloader : public Thread, public Subject
private:
CString m_url;
CString m_outputFilename;
Connection* m_connection;
std::unique_ptr<Connection> m_connection;
Mutex m_connectionMutex;
EStatus m_status;
time_t m_lastUpdateTime;
Expand All @@ -67,7 +67,7 @@ class WebDownloader : public Thread, public Subject
bool m_gzip;
bool m_retry;
#ifndef DISABLE_GZIP
GUnzipStream* m_gUnzipStream;
std::unique_ptr<GUnzipStream> m_gUnzipStream;
#endif

void SetStatus(EStatus status);
Expand Down
10 changes: 2 additions & 8 deletions daemon/feed/FeedFilter.cpp
Expand Up @@ -35,15 +35,9 @@ FeedFilter::Term::Term()
m_float = false;
m_intParam = 0;
m_floatParam = 0.0;
m_regEx = nullptr;
m_refValues = nullptr;
}

FeedFilter::Term::~Term()
{
delete m_regEx;
}

bool FeedFilter::Term::Match(FeedItemInfo& feedItemInfo)
{
const char* strValue = nullptr;
Expand Down Expand Up @@ -185,13 +179,13 @@ bool FeedFilter::Term::MatchRegex(const char* strValue)
{
if (!m_regEx)
{
m_regEx = new RegEx(m_param, m_refValues == nullptr ? 0 : 100);
m_regEx = std::make_unique<RegEx>(m_param, m_refValues == nullptr ? 0 : 100);
}

bool found = m_regEx->Match(strValue);
if (found)
{
FillRegExRefValues(strValue, m_regEx);
FillRegExRefValues(strValue, m_regEx.get());
}
return found;
}
Expand Down
3 changes: 1 addition & 2 deletions daemon/feed/FeedFilter.h
Expand Up @@ -60,7 +60,7 @@ class FeedFilter
int64 m_intParam;
double m_floatParam;
bool m_float;
RegEx* m_regEx;
std::unique_ptr<RegEx> m_regEx;
RefValues* m_refValues;

bool GetFieldData(const char* field, FeedItemInfo* feedItemInfo,
Expand All @@ -78,7 +78,6 @@ class FeedFilter
public:
Term();
Term(Term&&) = delete; // catch performance issues
~Term();
void SetRefValues(RefValues* refValues) { m_refValues = refValues; }
bool Compile(char* token);
bool Match(FeedItemInfo& feedItemInfo);
Expand Down
1 change: 1 addition & 0 deletions daemon/main/nzbget.h
Expand Up @@ -199,6 +199,7 @@ using namespace MSXML;
#include <algorithm>
#include <iostream>
#include <fstream>
#include <memory>

#ifdef HAVE_LIBGNUTLS
#include <gnutls/gnutls.h>
Expand Down
28 changes: 1 addition & 27 deletions daemon/remote/RemoteClient.cpp
Expand Up @@ -32,32 +32,6 @@
#include "Util.h"
#include "FileSystem.h"

RemoteClient::RemoteClient()
{
m_connection = nullptr;
m_verbose = true;

/*
printf("sizeof(SNZBRequestBase)=%i\n", sizeof(SNZBRequestBase));
printf("sizeof(SNZBDownloadRequest)=%i\n", sizeof(SNZBDownloadRequest));
printf("sizeof(SNZBListRequest)=%i\n", sizeof(SNZBListRequest));
printf("sizeof(SNZBListResponse)=%i\n", sizeof(SNZBListResponse));
printf("sizeof(SNZBListResponseFileEntry)=%i\n", sizeof(SNZBListResponseFileEntry));
printf("sizeof(SNZBLogRequest)=%i\n", sizeof(SNZBLogRequest));
printf("sizeof(SNZBLogResponse)=%i\n", sizeof(SNZBLogResponse));
printf("sizeof(SNZBLogResponseEntry)=%i\n", sizeof(SNZBLogResponseEntry));
printf("sizeof(SNZBPauseUnpauseRequest)=%i\n", sizeof(SNZBPauseUnpauseRequest));
printf("sizeof(SNZBSetDownloadRateRequest)=%i\n", sizeof(SNZBSetDownloadRateRequest));
printf("sizeof(SNZBEditQueueRequest)=%i\n", sizeof(SNZBEditQueueRequest));
printf("sizeof(SNZBDumpDebugRequest)=%i\n", sizeof(SNZBDumpDebugRequest));
*/
}

RemoteClient::~RemoteClient()
{
delete m_connection;
}

void RemoteClient::printf(const char * msg,...)
{
if (m_verbose)
Expand All @@ -82,7 +56,7 @@ bool RemoteClient::InitConnection()
const char* controlIp = !strcmp(g_Options->GetControlIp(), "0.0.0.0") ? "127.0.0.1" : g_Options->GetControlIp();

// Create a connection to the server
m_connection = new Connection(controlIp, g_Options->GetControlPort(), false);
m_connection = std::make_unique<Connection>(controlIp, g_Options->GetControlPort(), false);

bool OK = m_connection->Connect();
if (!OK)
Expand Down
6 changes: 2 additions & 4 deletions daemon/remote/RemoteClient.h
Expand Up @@ -47,8 +47,8 @@ class RemoteClient
bool m_match;
};

Connection* m_connection;
bool m_verbose;
std::unique_ptr<Connection> m_connection;
bool m_verbose = true;

bool InitConnection();
void InitMessageBase(SNzbRequestBase* messageBase, int request, int size);
Expand All @@ -57,8 +57,6 @@ class RemoteClient
void perror(const char* msg);

public:
RemoteClient();
~RemoteClient();
void SetVerbose(bool verbose) { m_verbose = verbose; };
bool RequestServerDownload(const char* nzbFilename, const char* nzbContent, const char* category,
bool addFirst, bool addPaused, int priority,
Expand Down

0 comments on commit 695d67d

Please sign in to comment.