From 0238828afa4f470ba1626471f2e29aece5e9816b Mon Sep 17 00:00:00 2001 From: mfreiholz Date: Tue, 23 Feb 2016 20:51:19 +0100 Subject: [PATCH] Uses static lazy loaded QHash objects instead of static linker initialized class members. This should fix multi initialization when linking static into DLL and EXE at the same time. Hopefully.. --- .../include/ads/SectionContent.h | 9 +++-- .../include/ads/SectionWidget.h | 10 ++++-- AdvancedDockingSystem/src/ContainerWidget.cpp | 18 +++++----- AdvancedDockingSystem/src/SectionContent.cpp | 34 ++++++++++++------ AdvancedDockingSystem/src/SectionWidget.cpp | 35 ++++++++++++++----- 5 files changed, 73 insertions(+), 33 deletions(-) diff --git a/AdvancedDockingSystem/include/ads/SectionContent.h b/AdvancedDockingSystem/include/ads/SectionContent.h index 10a989e..2419c77 100644 --- a/AdvancedDockingSystem/include/ads/SectionContent.h +++ b/AdvancedDockingSystem/include/ads/SectionContent.h @@ -50,9 +50,12 @@ private: QPointer _title; QPointer _content; - static int NextUid; - static QHash LookupMap; - static QHash LookupMapByName; + static int GetNextUid(); + static QHash& GetLookupMap(); + static QHash& GetLookupMapByName(); + + //static QHash LookupMap; + //static QHash LookupMapByName; }; ADS_NAMESPACE_END diff --git a/AdvancedDockingSystem/include/ads/SectionWidget.h b/AdvancedDockingSystem/include/ads/SectionWidget.h index d75e502..389562f 100644 --- a/AdvancedDockingSystem/include/ads/SectionWidget.h +++ b/AdvancedDockingSystem/include/ads/SectionWidget.h @@ -68,9 +68,13 @@ private: SectionContent::RefPtr _mousePressContent; SectionTitleWidget* _mousePressTitleWidget; - static int NextUid; - static QHash LookupMap; - static QHash > LookupMapByContainer; + static int GetNextUid(); + static QHash& GetLookupMap(); + static QHash >& GetLookupMapByContainer(); + +// static int NextUid; +// static QHash LookupMap; +// static QHash > LookupMapByContainer; }; ADS_NAMESPACE_END diff --git a/AdvancedDockingSystem/src/ContainerWidget.cpp b/AdvancedDockingSystem/src/ContainerWidget.cpp index 55e44d1..2c6591a 100644 --- a/AdvancedDockingSystem/src/ContainerWidget.cpp +++ b/AdvancedDockingSystem/src/ContainerWidget.cpp @@ -94,7 +94,7 @@ bool ContainerWidget::showSectionContent(const SectionContent::RefPtr& sc) hsi.data.titleWidget->setVisible(true); hsi.data.contentWidget->setVisible(true); SectionWidget* sw = NULL; - if (hsi.preferredSectionId > 0 && (sw = SectionWidget::LookupMap.value(hsi.preferredSectionId)) != NULL) + if (hsi.preferredSectionId > 0 && (sw = SectionWidget::GetLookupMap().value(hsi.preferredSectionId)) != NULL) { sw->addContent(hsi.data, true); return true; @@ -302,7 +302,7 @@ QByteArray ContainerWidget::saveState() const while (iter.hasNext()) { iter.next(); - if (iter.value().preferredSectionId <= 0 || !SectionWidget::LookupMap.contains(iter.value().preferredSectionId)) + if (iter.value().preferredSectionId <= 0 || !SectionWidget::GetLookupMap().contains(iter.value().preferredSectionId)) cnt++; } out << cnt; @@ -310,7 +310,7 @@ QByteArray ContainerWidget::saveState() const while (iter.hasNext()) { iter.next(); - if (iter.value().preferredSectionId <= 0 || !SectionWidget::LookupMap.contains(iter.value().preferredSectionId)) + if (iter.value().preferredSectionId <= 0 || !SectionWidget::GetLookupMap().contains(iter.value().preferredSectionId)) out << iter.value().data.content->uniqueName(); } } @@ -376,7 +376,7 @@ bool ContainerWidget::restoreState(const QByteArray& data) QString uname; in >> uname; - const SectionContent::RefPtr sc = SectionContent::LookupMapByName.value(uname); + const SectionContent::RefPtr sc = SectionContent::GetLookupMapByName().value(uname); if (!sc) continue; @@ -401,7 +401,7 @@ bool ContainerWidget::restoreState(const QByteArray& data) { QString uname; in >> uname; - const SectionContent::RefPtr sc = SectionContent::LookupMapByName.value(uname); + const SectionContent::RefPtr sc = SectionContent::GetLookupMapByName().value(uname); if (!sc) continue; @@ -437,7 +437,7 @@ bool ContainerWidget::restoreState(const QByteArray& data) contents.append(contentsToHide.at(i)); // Compare restored contents with available contents - const QList allContents = SectionContent::LookupMap.values(); + const QList allContents = SectionContent::GetLookupMap().values(); for (int i = 0; i < allContents.count(); ++i) { const SectionContent::RefPtr sc = allContents.at(i).toStrongRef(); @@ -844,7 +844,7 @@ bool ContainerWidget::restoreFloatingWidgets(QDataStream& in, int version, QList in >> visible; qDebug() << "Restore FloatingWidget" << uname << geom << visible; - const SectionContent::RefPtr sc = SectionContent::LookupMapByName.value(uname).toStrongRef(); + const SectionContent::RefPtr sc = SectionContent::GetLookupMapByName().value(uname).toStrongRef(); if (!sc) { qWarning() << "Can not find SectionContent:" << uname; @@ -922,7 +922,7 @@ bool ContainerWidget::restoreSectionWidgets(QDataStream& in, int version, QSplit int preferredIndex = -1; in >> preferredIndex; - const SectionContent::RefPtr sc = SectionContent::LookupMapByName.value(uname).toStrongRef(); + const SectionContent::RefPtr sc = SectionContent::GetLookupMapByName().value(uname).toStrongRef(); if (!sc) { qWarning() << "Can not find SectionContent:" << uname; @@ -988,7 +988,7 @@ void ContainerWidget::onActionToggleSectionContentVisibility(bool visible) if (!a) return; const int uid = a->property("uid").toInt(); - const SectionContent::RefPtr sc = SectionContent::LookupMap.value(uid).toStrongRef(); + const SectionContent::RefPtr sc = SectionContent::GetLookupMap().value(uid).toStrongRef(); if (sc.isNull()) { qCritical() << "Can not find content by ID" << uid; diff --git a/AdvancedDockingSystem/src/SectionContent.cpp b/AdvancedDockingSystem/src/SectionContent.cpp index 1a62ed9..fc39acf 100644 --- a/AdvancedDockingSystem/src/SectionContent.cpp +++ b/AdvancedDockingSystem/src/SectionContent.cpp @@ -5,12 +5,8 @@ ADS_NAMESPACE_BEGIN -int SectionContent::NextUid = 1; -QHash SectionContent::LookupMap; -QHash SectionContent::LookupMapByName; - SectionContent::SectionContent() : - _uid(NextUid++) + _uid(GetNextUid()) { } @@ -21,7 +17,7 @@ SectionContent::RefPtr SectionContent::newSectionContent(const QString& uniqueNa qFatal("Can not create SectionContent with empty uniqueName"); return RefPtr(); } - else if (LookupMapByName.contains(uniqueName)) + else if (GetLookupMapByName().contains(uniqueName)) { qFatal("Can not create SectionContent with already used uniqueName"); return RefPtr(); @@ -38,17 +34,17 @@ SectionContent::RefPtr SectionContent::newSectionContent(const QString& uniqueNa sc->_title = title; sc->_content = content; - LookupMap.insert(sc->uid(), sc); + GetLookupMap().insert(sc->uid(), sc); if (!sc->uniqueName().isEmpty()) - LookupMapByName.insert(sc->uniqueName(), sc); + GetLookupMapByName().insert(sc->uniqueName(), sc); return sc; } SectionContent::~SectionContent() { - LookupMap.remove(_uid); - LookupMapByName.remove(_uniqueName); + GetLookupMap().remove(_uid); + GetLookupMapByName().remove(_uniqueName); delete _title; delete _content; } @@ -78,4 +74,22 @@ QWidget* SectionContent::contentWidget() const return _content; } +int SectionContent::GetNextUid() +{ + static int NextUid = 0; + return ++NextUid; +} + +QHash& SectionContent::GetLookupMap() +{ + static QHash LookupMap; + return LookupMap; +} + +QHash& SectionContent::GetLookupMapByName() +{ + static QHash LookupMapByName; + return LookupMapByName; +} + ADS_NAMESPACE_END diff --git a/AdvancedDockingSystem/src/SectionWidget.cpp b/AdvancedDockingSystem/src/SectionWidget.cpp index 1d94e16..d038afe 100644 --- a/AdvancedDockingSystem/src/SectionWidget.cpp +++ b/AdvancedDockingSystem/src/SectionWidget.cpp @@ -25,13 +25,13 @@ ADS_NAMESPACE_BEGIN -int SectionWidget::NextUid = 1; -QHash SectionWidget::LookupMap; -QHash > SectionWidget::LookupMapByContainer; +//int SectionWidget::NextUid = 1; +//QHash SectionWidget::LookupMap; +//QHash > SectionWidget::LookupMapByContainer; SectionWidget::SectionWidget(ContainerWidget* parent) : QFrame(parent), - _uid(NextUid++), + _uid(GetNextUid()), _container(parent), _tabsLayout(NULL), _contentsLayout(NULL), @@ -73,15 +73,15 @@ SectionWidget::SectionWidget(ContainerWidget* parent) : setGraphicsEffect(shadow); #endif - LookupMap.insert(_uid, this); - LookupMapByContainer[_container].insert(_uid, this); + GetLookupMap().insert(_uid, this); + GetLookupMapByContainer()[_container].insert(_uid, this); } SectionWidget::~SectionWidget() { qDebug() << Q_FUNC_INFO; - LookupMap.remove(_uid); - LookupMapByContainer[_container].remove(_uid); + GetLookupMap().remove(_uid); + GetLookupMapByContainer()[_container].remove(_uid); _container->_sections.removeAll(this); // Note: I don't like this here, but we have to remove it from list... // Delete empty QSplitter. @@ -318,4 +318,23 @@ void SectionWidget::onCloseButtonClicked() _container->hideSectionContent(sc); } +int SectionWidget::GetNextUid() +{ + static int NextUid = 0; + return ++NextUid; +} + +QHash& SectionWidget::GetLookupMap() +{ + static QHash LookupMap; + return LookupMap; + +} + +QHash >& SectionWidget::GetLookupMapByContainer() +{ + static QHash > LookupMapByContainer; + return LookupMapByContainer; +} + ADS_NAMESPACE_END