From 14cda00ebc46ba02f68bbbd3ae111059d1ccac9c Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 12 Dec 2014 20:52:41 -0800 Subject: [PATCH] First implementation of the DependencyManager --- libraries/shared/src/DependencyManager.cpp | 33 +++++++++++++ libraries/shared/src/DependencyManager.h | 55 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 libraries/shared/src/DependencyManager.cpp create mode 100644 libraries/shared/src/DependencyManager.h diff --git a/libraries/shared/src/DependencyManager.cpp b/libraries/shared/src/DependencyManager.cpp new file mode 100644 index 0000000000..b4b3061317 --- /dev/null +++ b/libraries/shared/src/DependencyManager.cpp @@ -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(); +} \ No newline at end of file diff --git a/libraries/shared/src/DependencyManager.h b/libraries/shared/src/DependencyManager.h new file mode 100644 index 0000000000..690058cab4 --- /dev/null +++ b/libraries/shared/src/DependencyManager.h @@ -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 +#include + +#include + +class DependencyManager { +public: + template + static T* get(); + + class Dependency { + virtual void deleteInstance() = 0; + friend DependencyManager; + }; +private: + static DependencyManager& getInstance(); + DependencyManager(); + ~DependencyManager(); + + typedef QHash InstanceHash; + static InstanceHash& getInstanceHash() { return getInstance()._instanceHash; } + InstanceHash _instanceHash; +}; + +template +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(instance); + } + + // Found no instance in hash so we create one. + T* newInstance = new T(); + getInstanceHash().insert(typeId, dynamic_cast(newInstance)); + return newInstance; +} + +#endif // hifi_DependencyManager_h \ No newline at end of file