mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 21:57:00 +02:00
Allow shared libraries to access core global objects
This commit is contained in:
parent
0c60538952
commit
11733c039f
5 changed files with 50 additions and 30 deletions
|
@ -44,22 +44,11 @@
|
||||||
|
|
||||||
#include "Menu.h"
|
#include "Menu.h"
|
||||||
|
|
||||||
Menu* Menu::_instance = NULL;
|
static const char* const MENU_PROPERTY_NAME = "com.highfidelity.Menu";
|
||||||
|
|
||||||
Menu* Menu::getInstance() {
|
Menu* Menu::getInstance() {
|
||||||
static QMutex menuInstanceMutex;
|
static Menu& instance = globalInstace<Menu>(MENU_PROPERTY_NAME);
|
||||||
|
return &instance;
|
||||||
// lock the menu instance mutex to make sure we don't race and create two menus and crash
|
|
||||||
menuInstanceMutex.lock();
|
|
||||||
|
|
||||||
if (!_instance) {
|
|
||||||
qCDebug(interfaceapp, "First call to Menu::getInstance() - initing menu.");
|
|
||||||
_instance = new Menu();
|
|
||||||
}
|
|
||||||
|
|
||||||
menuInstanceMutex.unlock();
|
|
||||||
|
|
||||||
return _instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Menu::Menu() {
|
Menu::Menu() {
|
||||||
|
|
|
@ -57,6 +57,7 @@ private:
|
||||||
class Menu : public QMenuBar {
|
class Menu : public QMenuBar {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
Menu();
|
||||||
static Menu* getInstance();
|
static Menu* getInstance();
|
||||||
|
|
||||||
void loadSettings();
|
void loadSettings();
|
||||||
|
@ -103,9 +104,6 @@ public slots:
|
||||||
void setIsOptionChecked(const QString& menuOption, bool isChecked);
|
void setIsOptionChecked(const QString& menuOption, bool isChecked);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Menu* _instance;
|
|
||||||
Menu();
|
|
||||||
|
|
||||||
typedef void(*settingsAction)(Settings&, QAction&);
|
typedef void(*settingsAction)(Settings&, QAction&);
|
||||||
static void loadAction(Settings& settings, QAction& action);
|
static void loadAction(Settings& settings, QAction& action);
|
||||||
static void saveAction(Settings& settings, QAction& action);
|
static void saveAction(Settings& settings, QAction& action);
|
||||||
|
|
|
@ -11,7 +11,14 @@
|
||||||
|
|
||||||
#include "DependencyManager.h"
|
#include "DependencyManager.h"
|
||||||
|
|
||||||
DependencyManager DependencyManager::_manager;
|
#include "SharedUtil.h"
|
||||||
|
|
||||||
|
static const char* const DEPENDENCY_PROPERTY_NAME = "com.highfidelity.DependencyMananger";
|
||||||
|
|
||||||
|
DependencyManager& DependencyManager::manager() {
|
||||||
|
static DependencyManager& instance = globalInstace<DependencyManager>(DEPENDENCY_PROPERTY_NAME);
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
QSharedPointer<Dependency>& DependencyManager::safeGet(size_t hashCode) {
|
QSharedPointer<Dependency>& DependencyManager::safeGet(size_t hashCode) {
|
||||||
return _instanceHash[hashCode];
|
return _instanceHash[hashCode];
|
||||||
|
|
|
@ -62,8 +62,8 @@ public:
|
||||||
static void registerInheritance();
|
static void registerInheritance();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static DependencyManager _manager;
|
static DependencyManager& manager();
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
size_t getHashCode();
|
size_t getHashCode();
|
||||||
|
|
||||||
|
@ -75,11 +75,11 @@ private:
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
QSharedPointer<T> DependencyManager::get() {
|
QSharedPointer<T> DependencyManager::get() {
|
||||||
static size_t hashCode = _manager.getHashCode<T>();
|
static size_t hashCode = manager().getHashCode<T>();
|
||||||
static QWeakPointer<T> instance;
|
static QWeakPointer<T> instance;
|
||||||
|
|
||||||
if (instance.isNull()) {
|
if (instance.isNull()) {
|
||||||
instance = qSharedPointerCast<T>(_manager.safeGet(hashCode));
|
instance = qSharedPointerCast<T>(manager().safeGet(hashCode));
|
||||||
|
|
||||||
if (instance.isNull()) {
|
if (instance.isNull()) {
|
||||||
qWarning() << "DependencyManager::get(): No instance available for" << typeid(T).name();
|
qWarning() << "DependencyManager::get(): No instance available for" << typeid(T).name();
|
||||||
|
@ -91,9 +91,9 @@ QSharedPointer<T> DependencyManager::get() {
|
||||||
|
|
||||||
template <typename T, typename ...Args>
|
template <typename T, typename ...Args>
|
||||||
QSharedPointer<T> DependencyManager::set(Args&&... args) {
|
QSharedPointer<T> DependencyManager::set(Args&&... args) {
|
||||||
static size_t hashCode = _manager.getHashCode<T>();
|
static size_t hashCode = manager().getHashCode<T>();
|
||||||
|
|
||||||
QSharedPointer<Dependency>& instance = _manager.safeGet(hashCode);
|
QSharedPointer<Dependency>& instance = manager().safeGet(hashCode);
|
||||||
instance.clear(); // Clear instance before creation of new one to avoid edge cases
|
instance.clear(); // Clear instance before creation of new one to avoid edge cases
|
||||||
QSharedPointer<T> newInstance(new T(args...), &T::customDeleter);
|
QSharedPointer<T> newInstance(new T(args...), &T::customDeleter);
|
||||||
QSharedPointer<Dependency> storedInstance = qSharedPointerCast<Dependency>(newInstance);
|
QSharedPointer<Dependency> storedInstance = qSharedPointerCast<Dependency>(newInstance);
|
||||||
|
@ -104,9 +104,9 @@ QSharedPointer<T> DependencyManager::set(Args&&... args) {
|
||||||
|
|
||||||
template <typename T, typename I, typename ...Args>
|
template <typename T, typename I, typename ...Args>
|
||||||
QSharedPointer<T> DependencyManager::set(Args&&... args) {
|
QSharedPointer<T> DependencyManager::set(Args&&... args) {
|
||||||
static size_t hashCode = _manager.getHashCode<T>();
|
static size_t hashCode = manager().getHashCode<T>();
|
||||||
|
|
||||||
QSharedPointer<Dependency>& instance = _manager.safeGet(hashCode);
|
QSharedPointer<Dependency>& instance = manager().safeGet(hashCode);
|
||||||
instance.clear(); // Clear instance before creation of new one to avoid edge cases
|
instance.clear(); // Clear instance before creation of new one to avoid edge cases
|
||||||
QSharedPointer<T> newInstance(new I(args...), &I::customDeleter);
|
QSharedPointer<T> newInstance(new I(args...), &I::customDeleter);
|
||||||
QSharedPointer<Dependency> storedInstance = qSharedPointerCast<Dependency>(newInstance);
|
QSharedPointer<Dependency> storedInstance = qSharedPointerCast<Dependency>(newInstance);
|
||||||
|
@ -117,15 +117,15 @@ QSharedPointer<T> DependencyManager::set(Args&&... args) {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void DependencyManager::destroy() {
|
void DependencyManager::destroy() {
|
||||||
static size_t hashCode = _manager.getHashCode<T>();
|
static size_t hashCode = manager().getHashCode<T>();
|
||||||
_manager.safeGet(hashCode).clear();
|
manager().safeGet(hashCode).clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Base, typename Derived>
|
template<typename Base, typename Derived>
|
||||||
void DependencyManager::registerInheritance() {
|
void DependencyManager::registerInheritance() {
|
||||||
size_t baseHashCode = typeid(Base).hash_code();
|
size_t baseHashCode = typeid(Base).hash_code();
|
||||||
size_t derivedHashCode = typeid(Derived).hash_code();
|
size_t derivedHashCode = typeid(Derived).hash_code();
|
||||||
_manager._inheritanceHash.insert(baseHashCode, derivedHashCode);
|
manager()._inheritanceHash.insert(baseHashCode, derivedHashCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#define hifi_SharedUtil_h
|
#define hifi_SharedUtil_h
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -20,7 +21,32 @@
|
||||||
#include <unistd.h> // not on windows, not needed for mac or windows
|
#include <unistd.h> // not on windows, not needed for mac or windows
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QtCore/QDebug>
|
||||||
|
#include <QtCore/QCoreApplication>
|
||||||
|
|
||||||
|
// Provides efficient access to a named global type. By storing the value
|
||||||
|
// 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 };
|
||||||
|
static std::mutex mutex;
|
||||||
|
if (!instance) {
|
||||||
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
if (!instance) {
|
||||||
|
auto variant = qApp->property(propertyName);
|
||||||
|
if (variant.isNull()) {
|
||||||
|
auto* typedInstance = new T(args...);
|
||||||
|
void* voidInstance = typedInstance;
|
||||||
|
variant = QVariant::fromValue(voidInstance);
|
||||||
|
qApp->setProperty(propertyName, variant);
|
||||||
|
}
|
||||||
|
void* returnedVoidInstance = variant.value<void*>();
|
||||||
|
instance = static_cast<T*>(returnedVoidInstance);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *instance;
|
||||||
|
}
|
||||||
|
|
||||||
const int BYTES_PER_COLOR = 3;
|
const int BYTES_PER_COLOR = 3;
|
||||||
const int BYTES_PER_FLAGS = 1;
|
const int BYTES_PER_FLAGS = 1;
|
||||||
|
|
Loading…
Reference in a new issue