use a unique_ptr for linkedData in Node

This commit is contained in:
Stephen Birarda 2015-11-18 18:03:31 -08:00
parent b47aadff2b
commit 271387f96e
10 changed files with 26 additions and 21 deletions

View file

@ -9,6 +9,7 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
#include <memory>
#include <signal.h> #include <signal.h>
#include <AddressManager.h> #include <AddressManager.h>
@ -227,8 +228,9 @@ void AssignmentClientMonitor::handleChildStatusPacket(QSharedPointer<NLPacket> p
matchingNode = DependencyManager::get<LimitedNodeList>()->addOrUpdateNode matchingNode = DependencyManager::get<LimitedNodeList>()->addOrUpdateNode
(senderID, NodeType::Unassigned, senderSockAddr, senderSockAddr, false, false); (senderID, NodeType::Unassigned, senderSockAddr, senderSockAddr, false, false);
childData = new AssignmentClientChildData(Assignment::Type::AllTypes); auto childData = std::unique_ptr<AssignmentClientChildData>
matchingNode->setLinkedData(childData); { new AssignmentClientChildData(Assignment::Type::AllTypes) };
matchingNode->setLinkedData(std::move(childData));
} else { } else {
// tell unknown assignment-client child to exit. // tell unknown assignment-client child to exit.
qDebug() << "Asking unknown child at" << senderSockAddr << "to exit."; qDebug() << "Asking unknown child at" << senderSockAddr << "to exit.";

View file

@ -14,6 +14,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <math.h> #include <math.h>
#include <memory>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -652,7 +653,7 @@ void AudioMixer::run() {
nodeList->addNodeTypeToInterestSet(NodeType::Agent); nodeList->addNodeTypeToInterestSet(NodeType::Agent);
nodeList->linkedDataCreateCallback = [](Node* node) { nodeList->linkedDataCreateCallback = [](Node* node) {
node->setLinkedData(new AudioMixerClientData()); node->setLinkedData(std::unique_ptr<AudioMixerClientData> { new AudioMixerClientData });
}; };
// wait until we have the domain-server settings, otherwise we bail // wait until we have the domain-server settings, otherwise we bail

View file

@ -11,6 +11,7 @@
#include <cfloat> #include <cfloat>
#include <random> #include <random>
#include <memory>
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
#include <QtCore/QDateTime> #include <QtCore/QDateTime>
@ -519,7 +520,7 @@ void AvatarMixer::run() {
nodeList->addNodeTypeToInterestSet(NodeType::Agent); nodeList->addNodeTypeToInterestSet(NodeType::Agent);
nodeList->linkedDataCreateCallback = [] (Node* node) { nodeList->linkedDataCreateCallback = [] (Node* node) {
node->setLinkedData(new AvatarMixerClientData()); node->setLinkedData(std::unique_ptr<AvatarMixerClientData> { new AvatarMixerClientData });
}; };
// setup the timer that will be fired on the broadcast thread // setup the timer that will be fired on the broadcast thread

View file

@ -46,8 +46,8 @@ void EntityServer::handleEntityPacket(QSharedPointer<NLPacket> packet, SharedNod
} }
} }
OctreeQueryNode* EntityServer::createOctreeQueryNode() { std::unique_ptr<OctreeQueryNode> EntityServer::createOctreeQueryNode() {
return new EntityNodeData(); return std::unique_ptr<OctreeQueryNode> { new EntityNodeData() };
} }
OctreePointer EntityServer::createTree() { OctreePointer EntityServer::createTree() {

View file

@ -14,6 +14,8 @@
#include "../octree/OctreeServer.h" #include "../octree/OctreeServer.h"
#include <memory>
#include "EntityItem.h" #include "EntityItem.h"
#include "EntityServerConsts.h" #include "EntityServerConsts.h"
#include "EntityTree.h" #include "EntityTree.h"
@ -26,7 +28,7 @@ public:
~EntityServer(); ~EntityServer();
// Subclasses must implement these methods // Subclasses must implement these methods
virtual OctreeQueryNode* createOctreeQueryNode() override ; virtual std::unique_ptr<OctreeQueryNode> createOctreeQueryNode() override ;
virtual char getMyNodeType() const override { return NodeType::EntityServer; } virtual char getMyNodeType() const override { return NodeType::EntityServer; }
virtual PacketType getMyQueryMessageType() const override { return PacketType::EntityQuery; } virtual PacketType getMyQueryMessageType() const override { return PacketType::EntityQuery; }
virtual const char* getMyServerName() const override { return MODEL_SERVER_NAME; } virtual const char* getMyServerName() const override { return MODEL_SERVER_NAME; }

View file

@ -1108,9 +1108,9 @@ void OctreeServer::run() {
#endif #endif
nodeList->linkedDataCreateCallback = [] (Node* node) { nodeList->linkedDataCreateCallback = [] (Node* node) {
OctreeQueryNode* newQueryNodeData = _instance->createOctreeQueryNode(); auto queryNodeData = _instance->createOctreeQueryNode();
newQueryNodeData->init(); queryNodeData->init();
node->setLinkedData(newQueryNodeData); node->setLinkedData(std::move(queryNodeData));
}; };
srand((unsigned)time(0)); srand((unsigned)time(0));

View file

@ -12,6 +12,8 @@
#ifndef hifi_OctreeServer_h #ifndef hifi_OctreeServer_h
#define hifi_OctreeServer_h #define hifi_OctreeServer_h
#include <memory>
#include <QStringList> #include <QStringList>
#include <QDateTime> #include <QDateTime>
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
@ -61,7 +63,7 @@ public:
quint64 getLoadElapsedTime() const { return (_persistThread) ? _persistThread->getLoadElapsedTime() : 0; } quint64 getLoadElapsedTime() const { return (_persistThread) ? _persistThread->getLoadElapsedTime() : 0; }
// Subclasses must implement these methods // Subclasses must implement these methods
virtual OctreeQueryNode* createOctreeQueryNode() = 0; virtual std::unique_ptr<OctreeQueryNode> createOctreeQueryNode() = 0;
virtual char getMyNodeType() const = 0; virtual char getMyNodeType() const = 0;
virtual PacketType getMyQueryMessageType() const = 0; virtual PacketType getMyQueryMessageType() const = 0;
virtual const char* getMyServerName() const = 0; virtual const char* getMyServerName() const = 0;

View file

@ -11,6 +11,8 @@
#include "DomainServer.h" #include "DomainServer.h"
#include <memory>
#include <QDir> #include <QDir>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
@ -1640,7 +1642,7 @@ void DomainServer::refreshStaticAssignmentAndAddToQueue(SharedAssignmentPointer&
void DomainServer::nodeAdded(SharedNodePointer node) { void DomainServer::nodeAdded(SharedNodePointer node) {
// we don't use updateNodeWithData, so add the DomainServerNodeData to the node here // we don't use updateNodeWithData, so add the DomainServerNodeData to the node here
node->setLinkedData(new DomainServerNodeData()); node->setLinkedData(std::unique_ptr<DomainServerNodeData> { new DomainServerNodeData() });
} }
void DomainServer::nodeKilled(SharedNodePointer node) { void DomainServer::nodeKilled(SharedNodePointer node) {

View file

@ -48,7 +48,6 @@ Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket,
NetworkPeer(uuid, publicSocket, localSocket, parent), NetworkPeer(uuid, publicSocket, localSocket, parent),
_type(type), _type(type),
_connectionSecret(connectionSecret), _connectionSecret(connectionSecret),
_linkedData(NULL),
_isAlive(true), _isAlive(true),
_pingMs(-1), // "Uninitialized" _pingMs(-1), // "Uninitialized"
_clockSkewUsec(0), _clockSkewUsec(0),
@ -61,10 +60,6 @@ Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket,
setType(_type); setType(_type);
} }
Node::~Node() {
delete _linkedData;
}
void Node::setType(char type) { void Node::setType(char type) {
_type = type; _type = type;

View file

@ -12,6 +12,7 @@
#ifndef hifi_Node_h #ifndef hifi_Node_h
#define hifi_Node_h #define hifi_Node_h
#include <memory>
#include <ostream> #include <ostream>
#include <stdint.h> #include <stdint.h>
@ -34,7 +35,6 @@ public:
const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket,
bool canAdjustLocks, bool canRez, const QUuid& connectionSecret = QUuid(), bool canAdjustLocks, bool canRez, const QUuid& connectionSecret = QUuid(),
QObject* parent = 0); QObject* parent = 0);
~Node();
bool operator==(const Node& otherNode) const { return _uuid == otherNode._uuid; } bool operator==(const Node& otherNode) const { return _uuid == otherNode._uuid; }
bool operator!=(const Node& otherNode) const { return !(*this == otherNode); } bool operator!=(const Node& otherNode) const { return !(*this == otherNode); }
@ -45,8 +45,8 @@ public:
const QUuid& getConnectionSecret() const { return _connectionSecret; } const QUuid& getConnectionSecret() const { return _connectionSecret; }
void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; } void setConnectionSecret(const QUuid& connectionSecret) { _connectionSecret = connectionSecret; }
NodeData* getLinkedData() const { return _linkedData; } NodeData* getLinkedData() const { return _linkedData.get(); }
void setLinkedData(NodeData* linkedData) { _linkedData = linkedData; } void setLinkedData(std::unique_ptr<NodeData> linkedData) { _linkedData = std::move(linkedData); }
bool isAlive() const { return _isAlive; } bool isAlive() const { return _isAlive; }
void setAlive(bool isAlive) { _isAlive = isAlive; } void setAlive(bool isAlive) { _isAlive = isAlive; }
@ -75,7 +75,7 @@ private:
NodeType_t _type; NodeType_t _type;
QUuid _connectionSecret; QUuid _connectionSecret;
NodeData* _linkedData; std::unique_ptr<NodeData> _linkedData;
bool _isAlive; bool _isAlive;
int _pingMs; int _pingMs;
int _clockSkewUsec; int _clockSkewUsec;