mirror of
https://github.com/lubosz/overte.git
synced 2025-04-19 17:03:43 +02:00
PR feedback, ensure destruction of shared objects
This commit is contained in:
parent
11733c039f
commit
37a9538f10
3 changed files with 16 additions and 12 deletions
|
@ -47,8 +47,8 @@
|
|||
static const char* const MENU_PROPERTY_NAME = "com.highfidelity.Menu";
|
||||
|
||||
Menu* Menu::getInstance() {
|
||||
static Menu& instance = globalInstace<Menu>(MENU_PROPERTY_NAME);
|
||||
return &instance;
|
||||
static Menu* instance = globalInstace<Menu>(MENU_PROPERTY_NAME);
|
||||
return instance;
|
||||
}
|
||||
|
||||
Menu::Menu() {
|
||||
|
|
|
@ -12,12 +12,13 @@
|
|||
#include "DependencyManager.h"
|
||||
|
||||
#include "SharedUtil.h"
|
||||
#include "Finally.h"
|
||||
|
||||
static const char* const DEPENDENCY_PROPERTY_NAME = "com.highfidelity.DependencyMananger";
|
||||
|
||||
DependencyManager& DependencyManager::manager() {
|
||||
static DependencyManager& instance = globalInstace<DependencyManager>(DEPENDENCY_PROPERTY_NAME);
|
||||
return instance;
|
||||
static DependencyManager* instance = globalInstace<DependencyManager>(DEPENDENCY_PROPERTY_NAME);
|
||||
return *instance;
|
||||
}
|
||||
|
||||
QSharedPointer<Dependency>& DependencyManager::safeGet(size_t hashCode) {
|
||||
|
|
|
@ -28,24 +28,27 @@
|
|||
// in the QApplication by name we can implement the singleton pattern and
|
||||
// have the single instance function across DLL boundaries.
|
||||
template <typename T, typename ...Args>
|
||||
T& globalInstace(const char* propertyName, Args&&... args) {
|
||||
static T *instance { nullptr };
|
||||
T* globalInstace(const char* propertyName, Args&&... args) {
|
||||
static std::shared_ptr<T> instancePtr;
|
||||
static T *resultInstance { nullptr };
|
||||
static std::mutex mutex;
|
||||
if (!instance) {
|
||||
if (!resultInstance) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (!instance) {
|
||||
if (!resultInstance) {
|
||||
auto variant = qApp->property(propertyName);
|
||||
if (variant.isNull()) {
|
||||
auto* typedInstance = new T(args...);
|
||||
void* voidInstance = typedInstance;
|
||||
// Since we're building the object, store it in a shared_ptr so it's
|
||||
// destroyed by the destructor of the static instancePtr
|
||||
instancePtr = std::make_shared<T>(args...);
|
||||
void* voidInstance = &(*instancePtr);
|
||||
variant = QVariant::fromValue(voidInstance);
|
||||
qApp->setProperty(propertyName, variant);
|
||||
}
|
||||
void* returnedVoidInstance = variant.value<void*>();
|
||||
instance = static_cast<T*>(returnedVoidInstance);
|
||||
resultInstance = static_cast<T*>(returnedVoidInstance);
|
||||
}
|
||||
}
|
||||
return *instance;
|
||||
return resultInstance;
|
||||
}
|
||||
|
||||
const int BYTES_PER_COLOR = 3;
|
||||
|
|
Loading…
Reference in a new issue