switch to copy-and-swap for AgentSocket and Agent

This commit is contained in:
Stephen Birarda 2013-02-19 16:59:14 -08:00
parent 9d5a800c6f
commit bd02f48943
4 changed files with 27 additions and 33 deletions

View file

@ -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;

View file

@ -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__) */

View file

@ -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;
}

View file

@ -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__) */