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(); menu.addSeparator();
for(auto attr: componentInstance->attributes) { 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), auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr.name),
[attr, this]() { [&attr, this]() {
DialogManager::updateAttribute(attr); DialogManager::updateAttribute(&attr);
this->redraw(); this->redraw();
}); });

View File

@ -15,13 +15,13 @@ void Schema::setSchema(domain::Schema* schema)
this->schema = schema; this->schema = schema;
if(schema != nullptr) { if(schema != nullptr) {
for(auto &instance: schema->componentInstances) { for(auto &instance: schema->componentInstances) {
ComponentWrapper *component = ComponentWrapper::ofComponent(&instance); ComponentWrapper *component = ComponentWrapper::ofComponent(instance.get());
component->setPos(instance.position.first, instance.position.second); component->setPos(instance->position.first, instance->position.second);
scene.addItem(component); scene.addItem(component);
} }
for(auto &instance: schema->busInstances) { for(auto &instance: schema->busInstances) {
ComponentWrapper *bus = ComponentWrapper::ofBus(&instance); ComponentWrapper *bus = ComponentWrapper::ofBus(instance.get());
bus->setPos(instance.position.first, instance.position.second); bus->setPos(instance->position.first, instance->position.second);
scene.addItem(bus); scene.addItem(bus);
} }
} }

View File

@ -9,20 +9,20 @@ std::vector<ValidationError> ComdelValidator::validateSchema(Schema &schema, Val
context.attribute = nullptr; context.attribute = nullptr;
for(auto &instance: schema.componentInstances) { for(auto &instance: schema.componentInstances) {
auto result = validateComponent(&instance, context); auto result = validateComponent(instance.get(), context);
errors.insert(errors.end(), result.begin(), result.end()); errors.insert(errors.end(), result.begin(), result.end());
} }
return errors; return errors;
} }
std::vector<ValidationError> ComdelValidator::validateComponent(ComponentInstance *instance, ValidationContext context) { std::vector<ValidationError> ComdelValidator::validateComponent(ComponentInstance* instance, ValidationContext context) {
std::vector<ValidationError> errors; std::vector<ValidationError> errors;
context.instance = instance; context.instance = instance;
context.attributes.clear(); context.attributes.clear();
for(auto &attribute: instance->attributes) { for(auto &attribute: instance->attributes) {
context.attributes[attribute->name] = attribute->value; context.attributes[attribute.name] = attribute.value;
} }
for(auto &rule: instance->component.getRules()) { for(auto &rule: instance->component.getRules()) {
@ -33,7 +33,7 @@ std::vector<ValidationError> ComdelValidator::validateComponent(ComponentInstanc
} }
for(auto &attribute: instance->attributes) { for(auto &attribute: instance->attributes) {
auto result = validateAttribute(attribute, context); auto result = validateAttribute(&attribute, context);
errors.insert(errors.end(), result.begin(), result.end()); errors.insert(errors.end(), result.begin(), result.end());
} }

View File

@ -3,15 +3,15 @@
namespace domain { 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) : 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) : 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) : ConnectionInstance(instance, attributes, connection), secondInstance(secondInstance), bus(bus)
{} {}

View File

@ -17,9 +17,9 @@ public:
virtual ~ConnectionInstance() = default; 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: public:
BusInstance *bus; 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; BusInstance *bus;
ComponentInstance *secondInstance; 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 { 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) : name(name), attributes(attributes), position(position)
{} {}
BusInstance::BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size) 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) : Instance(name, attributes, position), component(component)
{} {}

View File

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

View File

@ -15,20 +15,20 @@ public:
Schema(); Schema();
std::vector<BusInstance> busInstances; std::vector<shared_ptr<BusInstance>> busInstances;
std::vector<ComponentInstance> componentInstances; std::vector<shared_ptr<ComponentInstance>> componentInstances;
std::vector<ConnectionInstance> connections; std::vector<shared_ptr<ConnectionInstance>> connections;
Instance *getInstance(std::string& name) { Instance *getInstance(std::string& name) {
for(auto& instance: busInstances) { for(auto& instance: busInstances) {
if (instance.name == name) { if (instance->name == name) {
return &instance; return instance.get();
} }
} }
for(auto& instance: componentInstances) { for(auto& instance: componentInstances) {
if (instance.name == name) { if (instance->name == name) {
return &instance; return instance.get();
} }
} }
return nullptr; return nullptr;

View File

@ -803,7 +803,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library)
continue; continue;
} }
std::vector<InstanceAttribute*> attributes; std::vector<InstanceAttribute> attributes;
for(auto& attr: conn.attributes) { for(auto& attr: conn.attributes) {
if(connection->hasAttribute(attr.name.value)) { if(connection->hasAttribute(attr.name.value)) {
auto attribute = connection->getAttribute(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"); 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 { } else {
errors.emplace_back(attr.name.span, "unknown attribute"); errors.emplace_back(attr.name.span, "unknown attribute");
} }
} }
if(secondComponent == nullptr) { 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 { } 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; return schema;
} }
ComponentInstance SchemaCreator::loadComponentInstance(InstanceNode instance, Library &library) { shared_ptr<ComponentInstance> SchemaCreator::loadComponentInstance(InstanceNode instance, Library &library) {
auto name = instance.name.value; auto name = instance.name.value;
auto position = std::make_pair(instance.position->first.value, instance.position->second.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); auto component = library.getComponent(instance.component.value);
// validate attributes // validate attributes
std::vector<InstanceAttribute*> attributes; std::vector<InstanceAttribute> attributes;
for(auto& attr: instance.attributes) { for(auto& attr: instance.attributes) {
if(component.hasAttribute(attr.name.value, toType(attr.value.getType()))) { if(component.hasAttribute(attr.name.value, toType(attr.value.getType()))) {
auto attribute = component.getAttribute(attr.name.value); 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 { } else {
errors.emplace_back(SourceError(attr.name.span, "unknown attribute")); 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 name = instance.name.value;
auto position = std::make_pair(instance.position->first.value, instance.position->second.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; 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 } // namespace domain

View File

@ -75,8 +75,8 @@ class SchemaCreator
std::optional<Connection> loadConnection(ConnectionNode node); std::optional<Connection> loadConnection(ConnectionNode node);
std::optional<Bus> loadBus(BusNode node); std::optional<Bus> loadBus(BusNode node);
ComponentInstance loadComponentInstance(InstanceNode instance, Library &library); std::shared_ptr<ComponentInstance> loadComponentInstance(InstanceNode instance, Library &library);
BusInstance loadBusInstance(InstanceNode instance, Library &library); std::shared_ptr<BusInstance> loadBusInstance(InstanceNode instance, Library &library);
std::optional<Bus> getBus(std::string name) { std::optional<Bus> getBus(std::string name) {
for(auto &bus: buses) { for(auto &bus: buses) {
@ -113,7 +113,7 @@ class SchemaCreator
this->context.push_back(context); this->context.push_back(context);
} }
void pushAdditional(std::string name) { void pushAdditional(std::string name) {
if(this->context.size() > 0) { if(!this->context.empty()) {
this->context.push_back(current()); this->context.push_back(current());
current().name = name; current().name = name;
} else { } else {

View File

@ -157,25 +157,25 @@ void MainWindow::onStoreScheme() {
buffer << "@schema {" << std::endl; buffer << "@schema {" << std::endl;
for(auto &componentInstance: schema->componentInstances) { for(auto &componentInstance: schema->componentInstances) {
buffer << "\t" << "@instance " << componentInstance.name << " " << componentInstance.component.getName() << " {" << std::endl; buffer << "\t" << "@instance " << componentInstance->name << " " << componentInstance->component.getName() << " {" << std::endl;
buffer << "\t\t" << "@position (" << componentInstance.position.first << ", " << componentInstance.position.second << ")" << std::endl; buffer << "\t\t" << "@position (" << componentInstance->position.first << ", " << componentInstance->position.second << ")" << std::endl;
for(auto &attribute: componentInstance.attributes) { for(auto &attribute: componentInstance->attributes) {
buffer << "\t\t" << "@attribute " << attribute->name << " " << attribute->value.stringify() << std::endl; buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify() << std::endl;
} }
buffer << "\t}" << std::endl << std::endl; buffer << "\t}" << std::endl << std::endl;
} }
for(auto &busInstance: schema->busInstances) { for(auto &busInstance: schema->busInstances) {
buffer << "\t" << "@instance " << busInstance.name << " " << busInstance.bus.getName() << " {" << 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" << "@position (" << busInstance->position.first << ", " << busInstance->position.second << ")" << std::endl;
buffer << "\t\t" << "@size " << busInstance.size << std::endl; buffer << "\t\t" << "@size " << busInstance->size << std::endl;
buffer << "\t}" << std::endl << std::endl; buffer << "\t}" << std::endl << std::endl;
} }
for(auto &conn: schema->connections) { for(auto &conn: schema->connections) {
auto busConn = dynamic_cast<domain::BusConnectionInstance*>(&conn); auto busConn = dynamic_cast<domain::BusConnectionInstance*>(conn.get());
if(busConn) { if(busConn) {
buffer << "\t" << "@connection (" << busConn->instance->name << "." << busConn->connection.getComponent().pin << ", " << busConn->bus->name << ") {" << std::endl; buffer << "\t" << "@connection (" << busConn->instance->name << "." << busConn->connection.getComponent().pin << ", " << busConn->bus->name << ") {" << std::endl;
buffer << "\t" << "}" << std::endl; buffer << "\t" << "}" << std::endl;