Base classe for shared named trees

This commit is contained in:
Atlante45 2014-02-25 16:12:05 -08:00
parent 4dea4b586b
commit 80337d2409
2 changed files with 119 additions and 0 deletions

View file

@ -0,0 +1,64 @@
//
// LocalVoxelsList.cpp
// hifi
//
// Created by Clément Brisset on 2/24/14.
// Copyright (c) 2014 High Fidelity, Inc. All rights reserved.
//
#include "LocalVoxelsList.h"
static void doNothing(VoxelTree* t) {
// do nothing
}
LocalVoxelsList* LocalVoxelsList::_instance = NULL;
LocalVoxelsList* LocalVoxelsList::getInstance() {
if (!_instance) {
_instance = new LocalVoxelsList();
}
return _instance;
}
LocalVoxelsList::LocalVoxelsList() {
}
LocalVoxelsList::~LocalVoxelsList() {
_instance = NULL;
}
StrongVoxelTreePointer LocalVoxelsList::getTree(QString treeName) {
return _trees.value(treeName);
}
void LocalVoxelsList::addPersistantTree(QString treeName, VoxelTree* tree) {
StrongVoxelTreePointer treePtr(tree, doNothing);
_persistantTree.push_back(treePtr);
_trees.insert(treeName, treePtr);
qDebug() << "[DEBUG] LocalVoxelsList : added persistant tree (" << treeName << ")" << endl;
}
void LocalVoxelsList::insert(QString treeName, StrongVoxelTreePointer& tree) {
// If the key don't already exist or the value is null
if (!_trees.contains(treeName) || !_trees.value(treeName)) {
_trees.insert(treeName, tree);
qDebug() << "[DEBUG] LocalVoxelsList : added local tree (" << treeName << ")" << endl;
} else {
// if not we replace the tree created by the user with the existing one
tree = _trees.value(treeName);
qDebug() << "[DEBUG] LocalVoxelsList : local tree already exist (" << treeName << ")"<< endl;
}
}
void LocalVoxelsList::remove(QString treeName) {
// if the tree is not used anymore (no strong pointer)
if (!_trees.value(treeName, StrongVoxelTreePointer(NULL))) {
// then remove it from the list
qDebug() << "[DEBUG] LocalVoxelsList : removed unused tree (" << treeName << ")" << endl;
_trees.remove(treeName);
} else {
qDebug() << "[DEBUG] LocalVoxelsList : tree still in use (" << treeName << ")" << endl;
}
}

View file

@ -0,0 +1,55 @@
//
// LocalVoxelsList.h
// hifi
//
// Created by Clément Brisset on 2/24/14.
// Copyright (c) 2014 High Fidelity, Inc. All rights reserved.
//
#ifndef __hifi__LocalVoxelsList__
#define __hifi__LocalVoxelsList__
#include <QHash>
#include <QString>
#include <QSharedPointer>
#include <QWeakPointer>
#include "VoxelTree.h"
typedef QSharedPointer<VoxelTree> StrongVoxelTreePointer;
typedef QWeakPointer<VoxelTree> WeakVoxelTreePointer;
/// Handles the the storage and cleanup of local named trees used by JS
class LocalVoxelsList {
public:
static LocalVoxelsList* getInstance();
~LocalVoxelsList();
/// Lookup up a tree in the QHash and return a strong pointer to it.
/// \param treeName name of the tree to look up
StrongVoxelTreePointer getTree(QString treeName);
/// Add a that will stay in the list until destruction of the instance and won't be destroyed then either.
/// \param treeName name to give to the tree in the list
/// \param tree standard pointer to the tree
void addPersistantTree(QString treeName, VoxelTree* tree);
/// insert a local tree in the list
/// \param treeName name to give to the tree in the list
/// \param tree strong pointer to the tree
void insert(QString treeName, StrongVoxelTreePointer& tree);
/// remove a tree from the list if it's not being used anymore
/// \param treeName name of the tree to remove
void remove(QString treeName);
private:
static LocalVoxelsList* _instance;
LocalVoxelsList();
QHash<QString, WeakVoxelTreePointer> _trees;
QList<StrongVoxelTreePointer> _persistantTree;
};
#endif /* defined(__hifi__LocalVoxelsList__) */