Cleanup use of OctalCodePtrList and add allocateOctalCodePtr

This commit is contained in:
Ryan Huffman 2016-05-11 10:37:54 -07:00
parent e819ab8475
commit 35f147f557
6 changed files with 20 additions and 15 deletions

View file

@ -63,7 +63,7 @@ JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) : _rootOctalCode(
copyContents(other);
}
void JurisdictionMap::copyContents(const OctalCodePtr& rootCodeIn, const std::vector<OctalCodePtr>& endNodesIn) {
void JurisdictionMap::copyContents(const OctalCodePtr& rootCodeIn, const OctalCodePtrList& endNodesIn) {
init(rootCodeIn, endNodesIn);
}
@ -83,10 +83,10 @@ JurisdictionMap::~JurisdictionMap() {
JurisdictionMap::JurisdictionMap(NodeType_t type) : _rootOctalCode(nullptr) {
_nodeType = type;
auto rootCode = std::shared_ptr<unsigned char>(new unsigned char[1], std::default_delete<unsigned char[]>());
OctalCodePtr rootCode = allocateOctalCodePtr(1);
*rootCode = 0;
std::vector<OctalCodePtr> emptyEndNodes;
OctalCodePtrList emptyEndNodes;
init(rootCode, emptyEndNodes);
}
@ -127,7 +127,7 @@ JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHe
std::tuple<OctalCodePtr, OctalCodePtrList> JurisdictionMap::getRootOctalCodeAndEndNodes() const {
std::lock_guard<std::mutex> lock(_octalCodeMutex);
return std::tuple<OctalCodePtr, std::vector<OctalCodePtr>>(_rootOctalCode, _endNodes);
return std::tuple<OctalCodePtr, OctalCodePtrList>(_rootOctalCode, _endNodes);
}
OctalCodePtr JurisdictionMap::getRootOctalCode() const {
@ -140,7 +140,7 @@ OctalCodePtrList JurisdictionMap::getEndNodeOctalCodes() const {
return _endNodes;
}
void JurisdictionMap::init(OctalCodePtr rootOctalCode, const std::vector<OctalCodePtr>& endNodes) {
void JurisdictionMap::init(OctalCodePtr rootOctalCode, const OctalCodePtrList& endNodes) {
std::lock_guard<std::mutex> lock(_octalCodeMutex);
_rootOctalCode = rootOctalCode;
_endNodes = endNodes;
@ -291,7 +291,7 @@ int JurisdictionMap::unpackFromPacket(ReceivedMessage& message) {
_endNodes.clear();
if (bytes > 0 && bytes <= message.getBytesLeftToRead()) {
_rootOctalCode = std::shared_ptr<unsigned char>(new unsigned char[bytes], std::default_delete<unsigned char[]>());
_rootOctalCode = allocateOctalCodePtr(bytes);
message.read(reinterpret_cast<char*>(_rootOctalCode.get()), bytes);
// if and only if there's a root jurisdiction, also include the end nodes
@ -303,7 +303,7 @@ int JurisdictionMap::unpackFromPacket(ReceivedMessage& message) {
message.readPrimitive(&bytes);
if (bytes <= message.getBytesLeftToRead()) {
auto endNodeCode = std::shared_ptr<unsigned char>(new unsigned char[bytes], std::default_delete<unsigned char[]>());
auto endNodeCode = allocateOctalCodePtr(bytes);
message.read(reinterpret_cast<char*>(endNodeCode.get()), bytes);
// if the endNodeCode was 0 length then don't add it

View file

@ -42,8 +42,8 @@ public:
// application constructors
JurisdictionMap(const char* filename);
// JurisdictionMap(unsigned char* rootOctalCode, const std::vector<unsigned char*>& endNodes);
JurisdictionMap(const char* rootHextString, const char* endNodesHextString);
~JurisdictionMap();
Area isMyJurisdiction(const unsigned char* nodeOctalCode, int childIndex) const;
@ -70,7 +70,7 @@ public:
private:
void copyContents(const JurisdictionMap& other); // use assignment instead
void init(OctalCodePtr rootOctalCode, const std::vector<OctalCodePtr>& endNodes);
void init(OctalCodePtr rootOctalCode, const OctalCodePtrList& endNodes);
mutable std::mutex _octalCodeMutex;
OctalCodePtr _rootOctalCode { nullptr };

View file

@ -116,14 +116,14 @@ void OctreeSceneStats::copyFromOther(const OctreeSceneStats& other) {
// Now copy the values from the other
if (other._jurisdictionRoot) {
auto bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._jurisdictionRoot.get()));
_jurisdictionRoot = OctalCodePtr(new unsigned char[bytes]);
_jurisdictionRoot = allocateOctalCodePtr(bytes);
memcpy(_jurisdictionRoot.get(), other._jurisdictionRoot.get(), bytes);
}
for (size_t i = 0; i < other._jurisdictionEndNodes.size(); i++) {
auto& endNodeCode = other._jurisdictionEndNodes[i];
if (endNodeCode) {
auto bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodeCode.get()));
auto endNodeCodeCopy = OctalCodePtr(new unsigned char[bytes]);
auto endNodeCodeCopy = allocateOctalCodePtr(bytes);
memcpy(endNodeCodeCopy.get(), endNodeCode.get(), bytes);
_jurisdictionEndNodes.push_back(endNodeCodeCopy);
}
@ -470,7 +470,7 @@ int OctreeSceneStats::unpackFromPacket(ReceivedMessage& packet) {
_jurisdictionRoot = nullptr;
_jurisdictionEndNodes.clear();
} else {
_jurisdictionRoot = OctalCodePtr(new unsigned char[bytes]);
_jurisdictionRoot = allocateOctalCodePtr(bytes);
packet.read(reinterpret_cast<char*>(_jurisdictionRoot.get()), bytes);
// if and only if there's a root jurisdiction, also include the end elements
@ -484,7 +484,7 @@ int OctreeSceneStats::unpackFromPacket(ReceivedMessage& packet) {
packet.readPrimitive(&bytes);
auto endNodeCode = OctalCodePtr(new unsigned char[bytes]);
auto endNodeCode = allocateOctalCodePtr(bytes);
packet.read(reinterpret_cast<char*>(endNodeCode.get()), bytes);
_jurisdictionEndNodes.push_back(endNodeCode);

View file

@ -147,7 +147,7 @@ public:
OctalCodePtr getJurisdictionRoot() const { return _jurisdictionRoot; }
/// Returns list of OctCodes for end elements of the jurisdiction of this particular octree server
const std::vector<OctalCodePtr>& getJurisdictionEndNodes() const { return _jurisdictionEndNodes; }
const OctalCodePtrList& getJurisdictionEndNodes() const { return _jurisdictionEndNodes; }
bool isMoving() const { return _isMoving; }
bool isFullScene() const { return _isFullScene; }

View file

@ -304,6 +304,10 @@ bool isAncestorOf(const unsigned char* possibleAncestor, const unsigned char* po
return true;
}
OctalCodePtr allocateOctalCodePtr(size_t size) {
return OctalCodePtr(new unsigned char[size], std::default_delete<unsigned char[]>());
}
OctalCodePtr hexStringToOctalCode(const QString& input) {
const int HEX_NUMBER_BASE = 16;
const int HEX_BYTE_SIZE = 2;
@ -311,7 +315,7 @@ OctalCodePtr hexStringToOctalCode(const QString& input) {
int byteArrayIndex = 0;
// allocate byte array based on half of string length
auto bytes = std::shared_ptr<unsigned char>(new unsigned char[(input.length()) / HEX_BYTE_SIZE]);
auto bytes = allocateOctalCodePtr(input.length() / HEX_BYTE_SIZE);
// loop through the string - 2 bytes at a time converting
// it to decimal equivalent and store in byte array

View file

@ -61,6 +61,7 @@ typedef enum {
OctalCodeComparison compareOctalCodes(const unsigned char* code1, const unsigned char* code2);
OctalCodePtr allocateOctalCodePtr(size_t size);
QString octalCodeToHexString(const unsigned char* octalCode);
OctalCodePtr hexStringToOctalCode(const QString& input);