Skip to content

Commit

Permalink
refctor: 1637 remove modules singleton (#1749)
Browse files Browse the repository at this point in the history
  • Loading branch information
rprospero committed Mar 11, 2024
1 parent 75fe729 commit c34b46e
Show file tree
Hide file tree
Showing 41 changed files with 243 additions and 217 deletions.
43 changes: 43 additions & 0 deletions src/classes/coreData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,46 @@ void CoreData::removeReferencesTo(Species *data)
cfg->empty();
}
void CoreData::removeReferencesTo(SpeciesSite *data) { KeywordStore::objectNoLongerValid(data); }

/*
* Modules
*/

// Return vector of all existing Modules
const std::vector<Module *> CoreData::moduleInstances() const
{
std::vector<Module *> result{};
for (auto &layer : processingLayers())
std::transform(layer->modules().begin(), layer->modules().end(), std::back_inserter(result),
[](auto &source) { return source.get(); });

return result;
}

// Search for any instance of any module with the specified unique name
Module *CoreData::findModule(std::string_view uniqueName) const
{
auto instances = moduleInstances();
auto it = std::find_if(instances.begin(), instances.end(),
[uniqueName](const auto *m) { return DissolveSys::sameString(m->name(), uniqueName); });
if (it != instances.end())
return *it;

return nullptr;
}

// Search for and return any instance(s) of the specified Module type
std::vector<Module *> CoreData::allOfType(ModuleTypes::ModuleType type) const
{
return allOfType(std::vector<ModuleTypes::ModuleType>{type});
}

// Search for and return any instance(s) of the specified Module type
std::vector<Module *> CoreData::allOfType(std::vector<ModuleTypes::ModuleType> types) const
{
std::vector<Module *> modules;
auto instances = moduleInstances();
std::copy_if(instances.begin(), instances.end(), std::back_inserter(modules),
[&types](const auto *m) { return std::find(types.begin(), types.end(), m->type()) != types.end(); });
return modules;
}
24 changes: 23 additions & 1 deletion src/classes/coreData.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "classes/configuration.h"
#include "classes/species.h"
#include "data/elements.h"
#include "module/types.h"
#include "templates/optionalRef.h"
#include <list>
#include <memory>
Expand Down Expand Up @@ -189,13 +190,34 @@ class CoreData
Configuration *findConfigurationByNiceName(std::string_view name) const;

/*
* Layers
* Layers and Modules
*/
private:
// List of defined processing layers
std::vector<std::unique_ptr<ModuleLayer>> processingLayers_;

public:
// Return vector of all existing Modules
const std::vector<Module *> moduleInstances() const;
// Search for any instance of any module with the specified unique name
Module *findModule(std::string_view uniqueName) const;
// Search for and return any instance(s) of the specified Module type
std::vector<Module *> allOfType(ModuleTypes::ModuleType type) const;
std::vector<Module *> allOfType(std::vector<ModuleTypes::ModuleType> types) const;
template <class M> std::vector<M *> allOfType() const
{
std::vector<M *> results;

for (auto *module : moduleInstances())
{
M *castModule = dynamic_cast<M *>(module);
if (castModule)
results.push_back(castModule);
}

return results;
}

// Add new processing layer
ModuleLayer *addProcessingLayer();
// Remove processing layer
Expand Down
4 changes: 2 additions & 2 deletions src/gui/keywordWidgets/dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class KeywordsDialog : public QDialog
Q_OBJECT

public:
KeywordsDialog(QWidget *parent, const KeywordStore &keywordStore, const CoreData &coreData);
KeywordsDialog(QWidget *parent, const KeywordStore &keywordStore, CoreData &coreData);
~KeywordsDialog() = default;

private:
// Main form declaration
Ui::KeywordsDialog ui_;
// CoreData reference
const CoreData &coreData_;
CoreData &coreData_;
// Whether any keywords have been modified in the current 'show'
bool keywordsModified_{false};
// Combined signal mask for all keyword changes
Expand Down
3 changes: 1 addition & 2 deletions src/gui/keywordWidgets/dialogFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#include "gui/keywordWidgets/dialog.h"
#include "keywords/organiser.h"

KeywordsDialog::KeywordsDialog(QWidget *parent, const KeywordStore &keywordStore, const CoreData &coreData)
: coreData_(coreData)
KeywordsDialog::KeywordsDialog(QWidget *parent, const KeywordStore &keywordStore, CoreData &coreData) : coreData_(coreData)
{
ui_.setupUi(this);

Expand Down
6 changes: 5 additions & 1 deletion src/gui/keywordWidgets/fileAndFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FileAndFormatKeywordWidget : public QWidget, public KeywordWidgetBase
Q_OBJECT

public:
FileAndFormatKeywordWidget(QWidget *parent, FileAndFormatKeyword *keyword, const CoreData &coreData);
FileAndFormatKeywordWidget(QWidget *parent, FileAndFormatKeyword *keyword, CoreData &coreData);

/*
* Keyword
Expand All @@ -24,6 +24,10 @@ class FileAndFormatKeywordWidget : public QWidget, public KeywordWidgetBase
// Associated keyword
FileAndFormatKeyword *keyword_;

protected:
// mutable coredata source
CoreData &coreData_;

/*
* Widgets
*/
Expand Down
4 changes: 2 additions & 2 deletions src/gui/keywordWidgets/fileAndFormatFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <QFileDialog>
#include <QFileInfo>

FileAndFormatKeywordWidget::FileAndFormatKeywordWidget(QWidget *parent, FileAndFormatKeyword *keyword, const CoreData &coreData)
: QWidget(parent), KeywordWidgetBase(coreData), keyword_(keyword)
FileAndFormatKeywordWidget::FileAndFormatKeywordWidget(QWidget *parent, FileAndFormatKeyword *keyword, CoreData &coreData)
: QWidget(parent), KeywordWidgetBase(coreData), keyword_(keyword), coreData_(coreData)
{
ui_.setupUi(this);

Expand Down
6 changes: 5 additions & 1 deletion src/gui/keywordWidgets/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LayerKeywordWidget : public QWidget, public KeywordWidgetBase
Q_OBJECT

public:
LayerKeywordWidget(QWidget *parent, LayerKeyword *keyword, const CoreData &coreData);
LayerKeywordWidget(QWidget *parent, LayerKeyword *keyword, CoreData &coreData);

/*
* Keyword
Expand All @@ -24,6 +24,10 @@ class LayerKeywordWidget : public QWidget, public KeywordWidgetBase
// Associated keyword
LayerKeyword *keyword_;

protected:
// mutable coredata source
CoreData &coreData_;

/*
* Widgets
*/
Expand Down
4 changes: 2 additions & 2 deletions src/gui/keywordWidgets/layerFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "gui/helpers/mouseWheelAdjustmentGuard.h"
#include "gui/keywordWidgets/layer.h"

LayerKeywordWidget::LayerKeywordWidget(QWidget *parent, LayerKeyword *keyword, const CoreData &coreData)
: QWidget(parent), KeywordWidgetBase(coreData), keyword_(keyword)
LayerKeywordWidget::LayerKeywordWidget(QWidget *parent, LayerKeyword *keyword, CoreData &coreData)
: QWidget(parent), KeywordWidgetBase(coreData), keyword_(keyword), coreData_(coreData)
{
// Setup our UI
ui_.setupUi(this);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/keywordWidgets/moduleFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void ModuleKeywordWidget::updateAllowedModules()
refreshing_ = true;

// Update allowed modules
allowedModules_ = Module::allOfType(keyword_->moduleType());
allowedModules_ = coreData_.allOfType(keyword_->moduleType());
moduleModel_.setData(allowedModules_);

// Set current index based on keyword data
Expand Down
2 changes: 1 addition & 1 deletion src/gui/keywordWidgets/moduleVectorFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void ModuleVectorKeywordWidget::resetModelData()
refreshing_ = true;

// Update allowed modules
allowedModules_ = Module::allOfType(keyword_->moduleTypes());
allowedModules_ = coreData_.allOfType(keyword_->moduleTypes());
moduleModel_.setData(allowedModules_);

updateSummaryText();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/keywordWidgets/producers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ KeywordWidgetProducer::KeywordWidgetProducer()
*/

// Produce object of specified type
std::pair<QWidget *, KeywordWidgetBase *> KeywordWidgetProducer::produce(KeywordBase *keyword, const CoreData &coreData) const
std::pair<QWidget *, KeywordWidgetBase *> KeywordWidgetProducer::produce(KeywordBase *keyword, CoreData &coreData) const
{
auto it = producers_.find(keyword->typeIndex());
if (it == producers_.end())
Expand Down
8 changes: 4 additions & 4 deletions src/gui/keywordWidgets/producers.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class KeywordWidgetProducer
// Return type
using WidgetKeywordProduct = std::pair<QWidget *, KeywordWidgetBase *>;
// Producer function type
using ProducerFunction = std::function<WidgetKeywordProduct(KeywordBase *keyword, const CoreData &coreData)>;
using ProducerFunction = std::function<WidgetKeywordProduct(KeywordBase *keyword, CoreData &coreData)>;
// Producers for all data types
std::unordered_map<std::type_index, ProducerFunction> producers_;

Expand All @@ -45,7 +45,7 @@ class KeywordWidgetProducer
template <class K> void registerProducer(ProducerFunction function) { producers_[typeid(K)] = std::move(function); }
template <class K, class W> void registerProducer()
{
producers_[typeid(K *)] = [&](KeywordBase *keyword, const CoreData &coreData)
producers_[typeid(K *)] = [&](KeywordBase *keyword, CoreData &coreData)
{
// Cast the base up to the full keyword
auto *k = dynamic_cast<K *>(keyword);
Expand All @@ -61,7 +61,7 @@ class KeywordWidgetProducer
};
}
// Produce object of specified type
std::pair<QWidget *, KeywordWidgetBase *> produce(KeywordBase *keyword, const CoreData &coreData) const;
std::pair<QWidget *, KeywordWidgetBase *> produce(KeywordBase *keyword, CoreData &coreData) const;

/*
* Instance
Expand All @@ -75,7 +75,7 @@ class KeywordWidgetProducer
*/
public:
// Create new item via template
static std::pair<QWidget *, KeywordWidgetBase *> create(KeywordBase *keyword, const CoreData &coreData)
static std::pair<QWidget *, KeywordWidgetBase *> create(KeywordBase *keyword, CoreData &coreData)
{
return instance().produce(keyword, coreData);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/keywordWidgets/weightedModuleVectorFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void WeightedModuleVectorKeywordWidget::resetModelData()
refreshing_ = true;

// Update allowed modules
allowedModules_ = Module::allOfType(keyword_->moduleTypes());
allowedModules_ = coreData_.allOfType(keyword_->moduleTypes());
weightedModuleModel_.setData(allowedModules_);

updateSummaryText();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/keywordWidgets/widget.hui
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class KeywordsWidget : public QScrollArea

public:
// Set up controls for specified keywords
void setUp(const KeywordStoreSection &keywordSection, const CoreData &coreData);
void setUp(const KeywordStoreSection &keywordSection, CoreData &coreData);
// Create a suitable button for the named group
static std::pair<QPushButton *, bool> buttonForGroup(std::string_view groupName);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/keywordWidgets/widgetFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ KeywordsWidget::KeywordsWidget(QWidget *parent) : QScrollArea(parent)
*/

// Set up controls for specified keywords
void KeywordsWidget::setUp(const KeywordStoreSection &keywordSection, const CoreData &coreData)
void KeywordsWidget::setUp(const KeywordStoreSection &keywordSection, CoreData &coreData)
{
keywordWidgets_.clear();

Expand Down

0 comments on commit c34b46e

Please sign in to comment.