From bd02f4894333f787fbf327f1d6c1b9c97028af61 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 19 Feb 2013 16:59:14 -0800 Subject: [PATCH] switch to copy-and-swap for AgentSocket and Agent --- shared/src/Agent.cpp | 34 ++++++++++++---------------------- shared/src/Agent.h | 4 +++- shared/src/AgentSocket.cpp | 18 +++++++++--------- shared/src/AgentSocket.h | 4 +++- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index 229db3150f..99f8eb9724 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -18,7 +18,7 @@ Agent::Agent(AgentSocket *agentPublicSocket, AgentSocket *agentLocalSocket, char activeSocket = NULL; type = agentType; - linkedData = 0; + linkedData = NULL; } Agent::Agent(const Agent &otherAgent) { @@ -38,30 +38,20 @@ Agent::Agent(const Agent &otherAgent) { // copy over linkedData } -Agent& Agent::operator=(const Agent &otherAgent) { - if (this != &otherAgent) { - // deallocate old memory - delete publicSocket; - delete localSocket; - delete linkedData; - - publicSocket = new AgentSocket(*otherAgent.publicSocket); - localSocket = new AgentSocket(*otherAgent.localSocket); - - if (otherAgent.activeSocket == otherAgent.publicSocket) { - activeSocket = publicSocket; - } else if (otherAgent.activeSocket == otherAgent.localSocket) { - activeSocket = localSocket; - } else { - activeSocket = NULL; - } - - type = otherAgent.type; - } - +Agent& Agent::operator=(Agent otherAgent) { + swap(*this, otherAgent); return *this; } +void Agent::swap(Agent &first, Agent &second) { + using std::swap; + swap(first.publicSocket, second.publicSocket); + swap(first.localSocket, second.localSocket); + swap(first.activeSocket, second.activeSocket); + swap(first.type, second.type); + swap(first.linkedData, second.linkedData); +} + Agent::~Agent() { delete publicSocket; delete localSocket; diff --git a/shared/src/Agent.h b/shared/src/Agent.h index ba6abf14b3..1800451c84 100644 --- a/shared/src/Agent.h +++ b/shared/src/Agent.h @@ -18,7 +18,7 @@ class Agent { Agent(); Agent(AgentSocket *agentPublicSocket, AgentSocket *agentLocalSocket, char agentType); Agent(const Agent &otherAgent); - Agent& operator=(const Agent &otherAgent); + Agent& operator=(Agent otherAgent); ~Agent(); bool matches(AgentSocket *otherPublicSocket, AgentSocket *otherLocalSocket, char otherAgentType); @@ -28,6 +28,8 @@ class Agent { int pingMsecs; bool isSelf; AgentData *linkedData; + private: + void swap(Agent &first, Agent &second); }; #endif /* defined(__hifi__Agent__) */ diff --git a/shared/src/AgentSocket.cpp b/shared/src/AgentSocket.cpp index e25f39b31a..e44c00394f 100644 --- a/shared/src/AgentSocket.cpp +++ b/shared/src/AgentSocket.cpp @@ -20,18 +20,18 @@ AgentSocket::AgentSocket(const AgentSocket &otherAgentSocket) { port = otherAgentSocket.port; } -AgentSocket& AgentSocket::operator=(const AgentSocket &otherAgentSocket) { - - if (this != &otherAgentSocket) { - delete address; - address = new char[MAX_ADDRESS_SIZE]; - strcpy(address, otherAgentSocket.address); - port = otherAgentSocket.port; - } - +AgentSocket& AgentSocket::operator=(AgentSocket otherAgentSocket) { + swap(*this, otherAgentSocket); return *this; } +void AgentSocket::swap(AgentSocket &first, AgentSocket &second) { + using std::swap; + + swap(first.address, second.address); + swap(first.port, second.port); +} + AgentSocket::~AgentSocket() { delete address; } \ No newline at end of file diff --git a/shared/src/AgentSocket.h b/shared/src/AgentSocket.h index bd24422635..6c9277ff0f 100644 --- a/shared/src/AgentSocket.h +++ b/shared/src/AgentSocket.h @@ -15,10 +15,12 @@ class AgentSocket { public: AgentSocket(); AgentSocket(const AgentSocket &otherAgentSocket); - AgentSocket& operator=(const AgentSocket &otherAgentSocket); + AgentSocket& operator=(AgentSocket otherAgentSocket); ~AgentSocket(); char *address; unsigned short port; + private: + void swap(AgentSocket &first, AgentSocket &second); }; #endif /* defined(__hifi__AgentSocket__) */