From 5ccd9b2fa4426359e7261aaefaa434c52a5a76b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Sat, 7 May 2022 14:19:43 +0200 Subject: [PATCH] Cleaned up connections --- comdel/display/component_display.cpp | 8 ++++---- comdel/display/schema_display.cpp | 8 ++++---- comdel/domain/comdelvalidator.cpp | 8 ++++---- comdel/domain/connectioninstance.cpp | 6 +++--- comdel/domain/connectioninstance.h | 8 ++++---- comdel/domain/instance.cpp | 6 +++--- comdel/domain/instance.h | 6 +++--- comdel/domain/schema.h | 14 +++++++------- comdel/domain/schemacreator.cpp | 20 ++++++++++---------- comdel/domain/schemacreator.h | 6 +++--- mainwindow.cpp | 16 ++++++++-------- 11 files changed, 53 insertions(+), 53 deletions(-) diff --git a/comdel/display/component_display.cpp b/comdel/display/component_display.cpp index 5a3b4fe..5cd65d9 100644 --- a/comdel/display/component_display.cpp +++ b/comdel/display/component_display.cpp @@ -97,11 +97,11 @@ void ComponentItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { }); menu.addSeparator(); for(auto attr: componentInstance->attributes) { - bool enabled = attr->attribute.getPopup().has_value(); + bool enabled = attr.attribute.getPopup().has_value(); - auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr->name), - [attr, this]() { - DialogManager::updateAttribute(attr); + auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr.name), + [&attr, this]() { + DialogManager::updateAttribute(&attr); this->redraw(); }); diff --git a/comdel/display/schema_display.cpp b/comdel/display/schema_display.cpp index 7bd27de..20c6515 100644 --- a/comdel/display/schema_display.cpp +++ b/comdel/display/schema_display.cpp @@ -15,13 +15,13 @@ void Schema::setSchema(domain::Schema* schema) this->schema = schema; if(schema != nullptr) { for(auto &instance: schema->componentInstances) { - ComponentWrapper *component = ComponentWrapper::ofComponent(&instance); - component->setPos(instance.position.first, instance.position.second); + ComponentWrapper *component = ComponentWrapper::ofComponent(instance.get()); + component->setPos(instance->position.first, instance->position.second); scene.addItem(component); } for(auto &instance: schema->busInstances) { - ComponentWrapper *bus = ComponentWrapper::ofBus(&instance); - bus->setPos(instance.position.first, instance.position.second); + ComponentWrapper *bus = ComponentWrapper::ofBus(instance.get()); + bus->setPos(instance->position.first, instance->position.second); scene.addItem(bus); } } diff --git a/comdel/domain/comdelvalidator.cpp b/comdel/domain/comdelvalidator.cpp index 9fd9295..f2fad6e 100644 --- a/comdel/domain/comdelvalidator.cpp +++ b/comdel/domain/comdelvalidator.cpp @@ -9,20 +9,20 @@ std::vector ComdelValidator::validateSchema(Schema &schema, Val context.attribute = nullptr; for(auto &instance: schema.componentInstances) { - auto result = validateComponent(&instance, context); + auto result = validateComponent(instance.get(), context); errors.insert(errors.end(), result.begin(), result.end()); } return errors; } -std::vector ComdelValidator::validateComponent(ComponentInstance *instance, ValidationContext context) { +std::vector ComdelValidator::validateComponent(ComponentInstance* instance, ValidationContext context) { std::vector errors; context.instance = instance; context.attributes.clear(); for(auto &attribute: instance->attributes) { - context.attributes[attribute->name] = attribute->value; + context.attributes[attribute.name] = attribute.value; } for(auto &rule: instance->component.getRules()) { @@ -33,7 +33,7 @@ std::vector ComdelValidator::validateComponent(ComponentInstanc } for(auto &attribute: instance->attributes) { - auto result = validateAttribute(attribute, context); + auto result = validateAttribute(&attribute, context); errors.insert(errors.end(), result.begin(), result.end()); } diff --git a/comdel/domain/connectioninstance.cpp b/comdel/domain/connectioninstance.cpp index 2739065..963b787 100644 --- a/comdel/domain/connectioninstance.cpp +++ b/comdel/domain/connectioninstance.cpp @@ -3,15 +3,15 @@ namespace domain { -ConnectionInstance::ConnectionInstance(ComponentInstance *instance, std::vector attributes, Connection connection) +ConnectionInstance::ConnectionInstance(ComponentInstance *instance, std::vector attributes, Connection connection) : instance(instance), attributes(attributes), connection(connection) {} -BusConnectionInstance::BusConnectionInstance(ComponentInstance *instance, std::vector attributes, BusInstance *bus, Connection connection) +BusConnectionInstance::BusConnectionInstance(ComponentInstance *instance, std::vector attributes, BusInstance *bus, Connection connection) : ConnectionInstance(instance, attributes, connection), bus(bus) {} -DirectConnectionInstance::DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondInstance, std::vector attributes, BusInstance *bus, Connection connection) +DirectConnectionInstance::DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondInstance, std::vector attributes, BusInstance *bus, Connection connection) : ConnectionInstance(instance, attributes, connection), secondInstance(secondInstance), bus(bus) {} diff --git a/comdel/domain/connectioninstance.h b/comdel/domain/connectioninstance.h index 4c5101c..a04354c 100644 --- a/comdel/domain/connectioninstance.h +++ b/comdel/domain/connectioninstance.h @@ -17,9 +17,9 @@ public: virtual ~ConnectionInstance() = default; - std::vector attributes; + std::vector attributes; - ConnectionInstance(ComponentInstance *instance, std::vector attributes, Connection connection); + ConnectionInstance(ComponentInstance *instance, std::vector attributes, Connection connection); }; @@ -28,7 +28,7 @@ class BusConnectionInstance: public ConnectionInstance public: BusInstance *bus; - BusConnectionInstance(ComponentInstance *instance, std::vector attributes, BusInstance *bus, Connection connection); + BusConnectionInstance(ComponentInstance *instance, std::vector attributes, BusInstance *bus, Connection connection); }; @@ -38,7 +38,7 @@ public: BusInstance *bus; ComponentInstance *secondInstance; - DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondsInstance, std::vector attributes, BusInstance *bus, Connection connection); + DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondsInstance, std::vector attributes, BusInstance *bus, Connection connection); }; diff --git a/comdel/domain/instance.cpp b/comdel/domain/instance.cpp index 016cb15..ebc97ff 100644 --- a/comdel/domain/instance.cpp +++ b/comdel/domain/instance.cpp @@ -3,13 +3,13 @@ namespace domain { -Instance::Instance(std::string name, std::vector attributes, std::pair position) +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) + : Instance(name, vector(), position), bus(bus), size(size) {} -ComponentInstance::ComponentInstance(std::string name, std::vector attributes, std::pair position, Component component) +ComponentInstance::ComponentInstance(std::string name, std::vector attributes, std::pair position, Component component) : Instance(name, attributes, position), component(component) {} diff --git a/comdel/domain/instance.h b/comdel/domain/instance.h index b0dd129..8f45e29 100644 --- a/comdel/domain/instance.h +++ b/comdel/domain/instance.h @@ -16,12 +16,12 @@ class Instance { public: std::string name; - std::vector attributes; + std::vector attributes; std::pair position; virtual ~Instance() {}; - Instance(std::string name, std::vector attributes, std::pair position); + Instance(std::string name, std::vector attributes, std::pair position); }; class BusInstance: public Instance @@ -42,7 +42,7 @@ class ComponentInstance: public Instance public: Component component; - ComponentInstance(std::string name, std::vector attributes, std::pair position, Component component); + ComponentInstance(std::string name, std::vector attributes, std::pair position, Component component); virtual ~ComponentInstance() { Instance::~Instance(); diff --git a/comdel/domain/schema.h b/comdel/domain/schema.h index aa7732f..70ff7d5 100644 --- a/comdel/domain/schema.h +++ b/comdel/domain/schema.h @@ -15,20 +15,20 @@ public: Schema(); - std::vector busInstances; - std::vector componentInstances; + std::vector> busInstances; + std::vector> componentInstances; - std::vector connections; + std::vector> connections; Instance *getInstance(std::string& name) { for(auto& instance: busInstances) { - if (instance.name == name) { - return &instance; + if (instance->name == name) { + return instance.get(); } } for(auto& instance: componentInstances) { - if (instance.name == name) { - return &instance; + if (instance->name == name) { + return instance.get(); } } return nullptr; diff --git a/comdel/domain/schemacreator.cpp b/comdel/domain/schemacreator.cpp index 7a4ef66..0cffec4 100644 --- a/comdel/domain/schemacreator.cpp +++ b/comdel/domain/schemacreator.cpp @@ -803,7 +803,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library) continue; } - std::vector attributes; + std::vector attributes; for(auto& attr: conn.attributes) { if(connection->hasAttribute(attr.name.value)) { auto attribute = connection->getAttribute(attr.name.value); @@ -818,23 +818,23 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library) errors.emplace_back(attr.span, "invalid value"); } - attributes.push_back(new InstanceAttribute(attribute.getName(), toType(attr.value), attribute)); + attributes.emplace_back(attribute.getName(), toType(attr.value), attribute); } else { errors.emplace_back(attr.name.span, "unknown attribute"); } } if(secondComponent == nullptr) { - schema->connections.push_back(BusConnectionInstance(firstComponent, attributes, bus, *connection)); + schema->connections.push_back(std::make_shared(firstComponent, attributes, bus, *connection)); } else { - schema->connections.push_back(DirectConnectionInstance(firstComponent, secondComponent, attributes, bus, *connection)); + schema->connections.push_back(std::make_shared(firstComponent, secondComponent, attributes, bus, *connection)); } } return schema; } -ComponentInstance SchemaCreator::loadComponentInstance(InstanceNode instance, Library &library) { +shared_ptr SchemaCreator::loadComponentInstance(InstanceNode instance, Library &library) { auto name = instance.name.value; auto position = std::make_pair(instance.position->first.value, instance.position->second.value); @@ -842,19 +842,19 @@ ComponentInstance SchemaCreator::loadComponentInstance(InstanceNode instance, Li auto component = library.getComponent(instance.component.value); // validate attributes - std::vector attributes; + std::vector attributes; for(auto& attr: instance.attributes) { if(component.hasAttribute(attr.name.value, toType(attr.value.getType()))) { auto attribute = component.getAttribute(attr.name.value); - attributes.emplace_back(new InstanceAttribute(attribute.getName(), toType(attr.value), attribute)); + attributes.emplace_back(attribute.getName(), toType(attr.value), attribute); } else { errors.emplace_back(SourceError(attr.name.span, "unknown attribute")); } } - return {name, attributes, position, component}; + return std::make_shared(name, attributes, position, component); } -BusInstance SchemaCreator::loadBusInstance(InstanceNode instance, Library &library) { +std::shared_ptr SchemaCreator::loadBusInstance(InstanceNode instance, Library &library) { auto name = instance.name.value; auto position = std::make_pair(instance.position->first.value, instance.position->second.value); @@ -866,7 +866,7 @@ BusInstance SchemaCreator::loadBusInstance(InstanceNode instance, Library &libra size = instance.size->value; } - return {name, position, bus, static_cast(size)}; + return std::make_shared(name, position, bus, static_cast(size)); } } // namespace domain diff --git a/comdel/domain/schemacreator.h b/comdel/domain/schemacreator.h index d29bf9d..d0360b1 100644 --- a/comdel/domain/schemacreator.h +++ b/comdel/domain/schemacreator.h @@ -75,8 +75,8 @@ class SchemaCreator std::optional loadConnection(ConnectionNode node); std::optional loadBus(BusNode node); - ComponentInstance loadComponentInstance(InstanceNode instance, Library &library); - BusInstance loadBusInstance(InstanceNode instance, Library &library); + std::shared_ptr loadComponentInstance(InstanceNode instance, Library &library); + std::shared_ptr loadBusInstance(InstanceNode instance, Library &library); std::optional getBus(std::string name) { for(auto &bus: buses) { @@ -113,7 +113,7 @@ class SchemaCreator this->context.push_back(context); } void pushAdditional(std::string name) { - if(this->context.size() > 0) { + if(!this->context.empty()) { this->context.push_back(current()); current().name = name; } else { diff --git a/mainwindow.cpp b/mainwindow.cpp index 50d3776..ae94e16 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -157,25 +157,25 @@ void MainWindow::onStoreScheme() { buffer << "@schema {" << std::endl; for(auto &componentInstance: schema->componentInstances) { - buffer << "\t" << "@instance " << componentInstance.name << " " << componentInstance.component.getName() << " {" << std::endl; - buffer << "\t\t" << "@position (" << componentInstance.position.first << ", " << componentInstance.position.second << ")" << std::endl; + buffer << "\t" << "@instance " << componentInstance->name << " " << componentInstance->component.getName() << " {" << std::endl; + buffer << "\t\t" << "@position (" << componentInstance->position.first << ", " << componentInstance->position.second << ")" << std::endl; - for(auto &attribute: componentInstance.attributes) { - buffer << "\t\t" << "@attribute " << attribute->name << " " << attribute->value.stringify() << std::endl; + for(auto &attribute: componentInstance->attributes) { + buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify() << std::endl; } buffer << "\t}" << std::endl << std::endl; } for(auto &busInstance: schema->busInstances) { - buffer << "\t" << "@instance " << busInstance.name << " " << busInstance.bus.getName() << " {" << std::endl; - buffer << "\t\t" << "@position (" << busInstance.position.first << ", " << busInstance.position.second << ")" << std::endl; - buffer << "\t\t" << "@size " << busInstance.size << std::endl; + buffer << "\t" << "@instance " << busInstance->name << " " << busInstance->bus.getName() << " {" << std::endl; + buffer << "\t\t" << "@position (" << busInstance->position.first << ", " << busInstance->position.second << ")" << std::endl; + buffer << "\t\t" << "@size " << busInstance->size << std::endl; buffer << "\t}" << std::endl << std::endl; } for(auto &conn: schema->connections) { - auto busConn = dynamic_cast(&conn); + auto busConn = dynamic_cast(conn.get()); if(busConn) { buffer << "\t" << "@connection (" << busConn->instance->name << "." << busConn->connection.getComponent().pin << ", " << busConn->bus->name << ") {" << std::endl; buffer << "\t" << "}" << std::endl;