Couple improvements to the dependency manager

This commit is contained in:
Atlante45 2014-12-12 21:10:45 -08:00
parent 14cda00ebc
commit 50fd52377f
2 changed files with 12 additions and 11 deletions

View file

@ -16,17 +16,10 @@ DependencyManager& DependencyManager::getInstance() {
return 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() { DependencyManager::~DependencyManager() {
foreach (Dependency* instance, _instanceHash) { foreach (Dependency* instance, _instanceHash) {
if (instance) { if (instance) {
instance->deleteInstance(); delete instance;
} }
} }
_instanceHash.clear(); _instanceHash.clear();

View file

@ -19,16 +19,24 @@
class DependencyManager { class DependencyManager {
public: public:
// Only accessible method.
// usage: T* instance = DependencyManager::get<T>();
template<typename T> template<typename T>
static T* get(); static T* get();
// Any class T in the DependencyManager needs to subclass Dependency
// They also need to have protected constructor(s) and virtual destructor
// As well as declare DependencyManager a friend class
class Dependency { class Dependency {
virtual void deleteInstance() = 0; protected:
Dependency() {}
virtual ~Dependency() {} // Ensure the proper destruction of the object
friend DependencyManager; friend DependencyManager;
}; };
private: private:
static DependencyManager& getInstance(); static DependencyManager& getInstance();
DependencyManager(); DependencyManager() {}
~DependencyManager(); ~DependencyManager();
typedef QHash<QString, Dependency*> InstanceHash; typedef QHash<QString, Dependency*> InstanceHash;
@ -52,4 +60,4 @@ T* DependencyManager::get() {
return newInstance; return newInstance;
} }
#endif // hifi_DependencyManager_h #endif // hifi_DependencyManager_h