mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-06-24 22:50:10 +02:00
First implementation of the DependencyManager
This commit is contained in:
parent
3e7f9ee1fe
commit
14cda00ebc
2 changed files with 88 additions and 0 deletions
33
libraries/shared/src/DependencyManager.cpp
Normal file
33
libraries/shared/src/DependencyManager.cpp
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
//
|
||||||
|
// DependencyManager.cpp
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Clément Brisset on 12/10/14.
|
||||||
|
// Copyright 2014 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "DependencyManager.h"
|
||||||
|
|
||||||
|
DependencyManager& DependencyManager::getInstance() {
|
||||||
|
static DependencyManager instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
DependencyManager::DependencyManager() {
|
||||||
|
// Guard against request of ourself
|
||||||
|
// We could set the value to this, but since it doesn't make sense to access
|
||||||
|
// the DependencyManager instance from the outside let's set it to NULL
|
||||||
|
_instanceHash.insert(typeid(DependencyManager).name(), NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
DependencyManager::~DependencyManager() {
|
||||||
|
foreach (Dependency* instance, _instanceHash) {
|
||||||
|
if (instance) {
|
||||||
|
instance->deleteInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_instanceHash.clear();
|
||||||
|
}
|
55
libraries/shared/src/DependencyManager.h
Normal file
55
libraries/shared/src/DependencyManager.h
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
//
|
||||||
|
// DependencyManager.h
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Clément Brisset on 12/10/14.
|
||||||
|
// Copyright 2014 High Fidelity, Inc.
|
||||||
|
//
|
||||||
|
// Distributed under the Apache License, Version 2.0.
|
||||||
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef hifi_DependencyManager_h
|
||||||
|
#define hifi_DependencyManager_h
|
||||||
|
|
||||||
|
#include <QHash>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
#include <typeinfo>
|
||||||
|
|
||||||
|
class DependencyManager {
|
||||||
|
public:
|
||||||
|
template<typename T>
|
||||||
|
static T* get();
|
||||||
|
|
||||||
|
class Dependency {
|
||||||
|
virtual void deleteInstance() = 0;
|
||||||
|
friend DependencyManager;
|
||||||
|
};
|
||||||
|
private:
|
||||||
|
static DependencyManager& getInstance();
|
||||||
|
DependencyManager();
|
||||||
|
~DependencyManager();
|
||||||
|
|
||||||
|
typedef QHash<QString, Dependency*> InstanceHash;
|
||||||
|
static InstanceHash& getInstanceHash() { return getInstance()._instanceHash; }
|
||||||
|
InstanceHash _instanceHash;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T* DependencyManager::get() {
|
||||||
|
const QString& typeId = typeid(T).name();
|
||||||
|
|
||||||
|
// Search the hash for global instance
|
||||||
|
Dependency* instance = getInstanceHash().value(typeId, NULL);
|
||||||
|
if (instance) {
|
||||||
|
return dynamic_cast<T*>(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Found no instance in hash so we create one.
|
||||||
|
T* newInstance = new T();
|
||||||
|
getInstanceHash().insert(typeId, dynamic_cast<Dependency*>(newInstance));
|
||||||
|
return newInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // hifi_DependencyManager_h
|
Loading…
Reference in a new issue