Cleaned up connections

This commit is contained in:
Borna Rajković 2022-05-07 14:19:43 +02:00
parent 344fe7cfa6
commit 5ccd9b2fa4
11 changed files with 53 additions and 53 deletions

View File

@ -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();
});

View File

@ -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);
}
}

View File

@ -9,20 +9,20 @@ std::vector<ValidationError> 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<ValidationError> ComdelValidator::validateComponent(ComponentInstance *instance, ValidationContext context) {
std::vector<ValidationError> ComdelValidator::validateComponent(ComponentInstance* instance, ValidationContext context) {
std::vector<ValidationError> 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<ValidationError> 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());
}

View File

@ -3,15 +3,15 @@
namespace domain {
ConnectionInstance::ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute*> attributes, Connection connection)
ConnectionInstance::ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes, Connection connection)
: instance(instance), attributes(attributes), connection(connection)
{}
BusConnectionInstance::BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute*> attributes, BusInstance *bus, Connection connection)
BusConnectionInstance::BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes, BusInstance *bus, Connection connection)
: ConnectionInstance(instance, attributes, connection), bus(bus)
{}
DirectConnectionInstance::DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondInstance, std::vector<InstanceAttribute*> attributes, BusInstance *bus, Connection connection)
DirectConnectionInstance::DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondInstance, std::vector<InstanceAttribute> attributes, BusInstance *bus, Connection connection)
: ConnectionInstance(instance, attributes, connection), secondInstance(secondInstance), bus(bus)
{}

View File

@ -17,9 +17,9 @@ public:
virtual ~ConnectionInstance() = default;
std::vector<InstanceAttribute*> attributes;
std::vector<InstanceAttribute> attributes;
ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute*> attributes, Connection connection);
ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes, Connection connection);
};
@ -28,7 +28,7 @@ class BusConnectionInstance: public ConnectionInstance
public:
BusInstance *bus;
BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute*> attributes, BusInstance *bus, Connection connection);
BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes, BusInstance *bus, Connection connection);
};
@ -38,7 +38,7 @@ public:
BusInstance *bus;
ComponentInstance *secondInstance;
DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondsInstance, std::vector<InstanceAttribute*> attributes, BusInstance *bus, Connection connection);
DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondsInstance, std::vector<InstanceAttribute> attributes, BusInstance *bus, Connection connection);
};

View File

@ -3,13 +3,13 @@
namespace domain {
Instance::Instance(std::string name, std::vector<InstanceAttribute*> attributes, std::pair<int, int> position)
Instance::Instance(std::string name, std::vector<InstanceAttribute> attributes, std::pair<int, int> position)
: name(name), attributes(attributes), position(position)
{}
BusInstance::BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size)
: Instance(name, vector<InstanceAttribute*>(), position), bus(bus), size(size)
: Instance(name, vector<InstanceAttribute>(), position), bus(bus), size(size)
{}
ComponentInstance::ComponentInstance(std::string name, std::vector<InstanceAttribute*> attributes, std::pair<int, int> position, Component component)
ComponentInstance::ComponentInstance(std::string name, std::vector<InstanceAttribute> attributes, std::pair<int, int> position, Component component)
: Instance(name, attributes, position), component(component)
{}

View File

@ -16,12 +16,12 @@ class Instance
{
public:
std::string name;
std::vector<InstanceAttribute*> attributes;
std::vector<InstanceAttribute> attributes;
std::pair<int, int> position;
virtual ~Instance() {};
Instance(std::string name, std::vector<InstanceAttribute*> attributes, std::pair<int, int> position);
Instance(std::string name, std::vector<InstanceAttribute> attributes, std::pair<int, int> position);
};
class BusInstance: public Instance
@ -42,7 +42,7 @@ class ComponentInstance: public Instance
public:
Component component;
ComponentInstance(std::string name, std::vector<InstanceAttribute*> attributes, std::pair<int, int> position, Component component);
ComponentInstance(std::string name, std::vector<InstanceAttribute> attributes, std::pair<int, int> position, Component component);
virtual ~ComponentInstance() {
Instance::~Instance();

View File

@ -15,20 +15,20 @@ public:
Schema();
std::vector<BusInstance> busInstances;
std::vector<ComponentInstance> componentInstances;
std::vector<shared_ptr<BusInstance>> busInstances;
std::vector<shared_ptr<ComponentInstance>> componentInstances;
std::vector<ConnectionInstance> connections;
std::vector<shared_ptr<ConnectionInstance>> 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;

View File

@ -803,7 +803,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library)
continue;
}
std::vector<InstanceAttribute*> attributes;
std::vector<InstanceAttribute> 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<BusConnectionInstance>(firstComponent, attributes, bus, *connection));
} else {
schema->connections.push_back(DirectConnectionInstance(firstComponent, secondComponent, attributes, bus, *connection));
schema->connections.push_back(std::make_shared<DirectConnectionInstance>(firstComponent, secondComponent, attributes, bus, *connection));
}
}
return schema;
}
ComponentInstance SchemaCreator::loadComponentInstance(InstanceNode instance, Library &library) {
shared_ptr<ComponentInstance> 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<InstanceAttribute*> attributes;
std::vector<InstanceAttribute> 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<ComponentInstance>(name, attributes, position, component);
}
BusInstance SchemaCreator::loadBusInstance(InstanceNode instance, Library &library) {
std::shared_ptr<BusInstance> 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<int>(size)};
return std::make_shared<BusInstance>(name, position, bus, static_cast<int>(size));
}
} // namespace domain

View File

@ -75,8 +75,8 @@ class SchemaCreator
std::optional<Connection> loadConnection(ConnectionNode node);
std::optional<Bus> loadBus(BusNode node);
ComponentInstance loadComponentInstance(InstanceNode instance, Library &library);
BusInstance loadBusInstance(InstanceNode instance, Library &library);
std::shared_ptr<ComponentInstance> loadComponentInstance(InstanceNode instance, Library &library);
std::shared_ptr<BusInstance> loadBusInstance(InstanceNode instance, Library &library);
std::optional<Bus> 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 {

View File

@ -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<domain::BusConnectionInstance*>(&conn);
auto busConn = dynamic_cast<domain::BusConnectionInstance*>(conn.get());
if(busConn) {
buffer << "\t" << "@connection (" << busConn->instance->name << "." << busConn->connection.getComponent().pin << ", " << busConn->bus->name << ") {" << std::endl;
buffer << "\t" << "}" << std::endl;