From 74a7ef3e7a1da53973b1cbadef8a6db9d03d9e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Sun, 8 May 2022 15:47:47 +0200 Subject: [PATCH] Refactored instances --- comdel/domain/comdelvalidator.h | 4 +-- comdel/domain/instance.cpp | 8 ++---- comdel/domain/instance.h | 41 ++++++++++++------------------- comdel/domain/instanceattribute.h | 3 ++- comdel/domain/schema.h | 6 +++-- comdel/domain/schemacreator.cpp | 6 ++--- mainwindow.cpp | 10 ++++++-- 7 files changed, 37 insertions(+), 41 deletions(-) diff --git a/comdel/domain/comdelvalidator.h b/comdel/domain/comdelvalidator.h index 9c0dec7..e801062 100644 --- a/comdel/domain/comdelvalidator.h +++ b/comdel/domain/comdelvalidator.h @@ -8,14 +8,14 @@ namespace domain { struct ValidationError { - Instance *instance; + ComponentInstance *instance; InstanceAttribute *attribute; Action::ActionType type; std::string message; }; struct ValidationContext { - Instance *instance; + ComponentInstance *instance; InstanceAttribute *attribute; std::map addressSpaces; std::map attributes; diff --git a/comdel/domain/instance.cpp b/comdel/domain/instance.cpp index ebc97ff..fde02d5 100644 --- a/comdel/domain/instance.cpp +++ b/comdel/domain/instance.cpp @@ -2,15 +2,11 @@ namespace domain { - -Instance::Instance(std::string name, std::vector attributes, std::pair position) - : name(name), attributes(attributes), position(position) -{} BusInstance::BusInstance(std::string name, std::pair position, Bus bus, int size) - : Instance(name, vector(), position), bus(bus), size(size) + : name(name), position(position), bus(bus), size(size) {} ComponentInstance::ComponentInstance(std::string name, std::vector attributes, std::pair position, Component component) - : Instance(name, attributes, position), component(component) + : name(name), attributes(std::move(attributes)), position(position), component(component) {} diff --git a/comdel/domain/instance.h b/comdel/domain/instance.h index 8f45e29..1e8412a 100644 --- a/comdel/domain/instance.h +++ b/comdel/domain/instance.h @@ -12,41 +12,32 @@ namespace domain { -class Instance +class BusInstance +{ +public: + std::string name; + std::pair position; + + Bus bus; + int size; + + BusInstance(std::string name, std::pair position, Bus bus, int size); + + virtual ~BusInstance() = default; +}; + +class ComponentInstance { public: std::string name; std::vector attributes; std::pair position; - virtual ~Instance() {}; - - Instance(std::string name, std::vector attributes, std::pair position); -}; - -class BusInstance: public Instance -{ -public: - Bus bus; - int size; - - BusInstance(std::string name, std::pair position, Bus bus, int size); - - virtual ~BusInstance() { - Instance::~Instance(); - } -}; - -class ComponentInstance: public Instance -{ -public: Component component; ComponentInstance(std::string name, std::vector attributes, std::pair position, Component component); - virtual ~ComponentInstance() { - Instance::~Instance(); - } + virtual ~ComponentInstance() = default; }; } // namespace domain diff --git a/comdel/domain/instanceattribute.h b/comdel/domain/instanceattribute.h index b32b686..d66bdc4 100644 --- a/comdel/domain/instanceattribute.h +++ b/comdel/domain/instanceattribute.h @@ -15,10 +15,11 @@ class InstanceAttribute public: InstanceAttribute(std::string name, Value value, Attribute attribute); - std::string name; + std::string name = ""; Value value; Attribute attribute; + ~InstanceAttribute() = default; }; } // namespace domain diff --git a/comdel/domain/schema.h b/comdel/domain/schema.h index 70ff7d5..f8d4e61 100644 --- a/comdel/domain/schema.h +++ b/comdel/domain/schema.h @@ -14,18 +14,20 @@ class Schema public: Schema(); - std::vector> busInstances; std::vector> componentInstances; std::vector> connections; - Instance *getInstance(std::string& name) { + BusInstance *getBusInstance(std::string& name) { for(auto& instance: busInstances) { if (instance->name == name) { return instance.get(); } } + return nullptr; + } + ComponentInstance *getComponentInstance(std::string& name) { for(auto& instance: componentInstances) { if (instance->name == name) { return instance.get(); diff --git a/comdel/domain/schemacreator.cpp b/comdel/domain/schemacreator.cpp index 271501d..ce9c0c8 100644 --- a/comdel/domain/schemacreator.cpp +++ b/comdel/domain/schemacreator.cpp @@ -787,7 +787,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library) } for(auto &conn: node.connections) { - auto firstComponent = dynamic_cast(schema->getInstance(conn.first.instance.value)); + auto firstComponent = schema->getComponentInstance(conn.first.instance.value); if(firstComponent == nullptr) { errors.emplace_back(conn.first.instance.span, "unknown component"); continue; @@ -800,7 +800,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library) ComponentInstance *secondComponent = nullptr; if(conn.second.has_value()) { - secondComponent = dynamic_cast(schema->getInstance(conn.second->instance.value)); + secondComponent = schema->getComponentInstance(conn.second->instance.value); if(secondComponent == nullptr) { errors.emplace_back(conn.second->instance.span, "unknown component"); continue; @@ -811,7 +811,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library) } } - auto bus = dynamic_cast(schema->getInstance(conn.bus.value)); + auto bus = schema->getBusInstance(conn.bus.value); if(bus == nullptr) { errors.emplace_back(conn.bus.span, "unknown bus"); continue; diff --git a/mainwindow.cpp b/mainwindow.cpp index ae94e16..47e8186 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -129,7 +129,11 @@ void MainWindow::onLoadSchema() { for (auto& error : generator.getErrors()) { parseContext.formatError(error, buffer, "ERROR: "); + delete schema; + schema = nullptr; } + } else { + library = std::nullopt; } if(generator.getErrors().empty()) { @@ -196,7 +200,6 @@ void MainWindow::onValidateSchema(bool /*toggled*/) { return; } - this->validationErrors.clear(); std::map callbacks; @@ -246,7 +249,10 @@ void MainWindow::onValidateSchema(bool /*toggled*/) { void MainWindow::clear() { validationErrors.clear(); - schema = nullptr; + if(schema != nullptr) { + delete schema; + schema = nullptr; + } library = std::nullopt; libraryDisplay->setLibrary(library);