schema_editor/comdel/domain/schema.cpp

121 lines
5.1 KiB
C++
Raw Normal View History

2022-04-05 21:48:07 +00:00
#include "schema.h"
namespace domain {
2022-05-27 06:18:17 +00:00
bool Schema::hasConnection(string &component, string &pin) {
return getConnection(component, pin) != nullptr;
}
2022-04-05 21:48:07 +00:00
2022-05-27 06:18:17 +00:00
ConnectionInstance *Schema::getConnection(string &component, string &pin) {
2022-06-09 21:42:45 +00:00
auto pinConnections = getConnections(component, pin);
if(pinConnections.empty()) {
return nullptr;
} else {
return pinConnections[0];
}
}
std::vector<ConnectionInstance*> Schema::getConnections(string &component, string &pin) {
std::vector<ConnectionInstance*> pinConnections;
2022-05-27 06:18:17 +00:00
for (auto &conn: connections) {
if (conn->instance->name == component && conn->connection.getComponent().pin == pin) {
2022-06-09 21:42:45 +00:00
pinConnections.push_back(conn.get());
2022-05-27 06:18:17 +00:00
}
auto dirConn = dynamic_cast<DirectConnectionInstance *>(conn.get());
if (dirConn != nullptr) {
if (dirConn->secondInstance->name == component && conn->connection.getSecondComponent()->pin == pin) {
2022-06-09 21:42:45 +00:00
pinConnections.push_back(conn.get());
2022-05-27 06:18:17 +00:00
}
}
}
2022-06-09 21:42:45 +00:00
return pinConnections;
2022-05-27 06:18:17 +00:00
}
2022-05-27 06:18:17 +00:00
BusInstance *Schema::getBusInstance(string &name) {
for (auto &instance: busInstances) {
if (instance->name == name) {
return instance.get();
}
}
2022-05-27 06:18:17 +00:00
return nullptr;
}
ComponentInstance *Schema::getComponentInstance(string &name) {
for (auto &instance: componentInstances) {
if (instance->name == name) {
return instance.get();
}
}
2022-05-27 06:18:17 +00:00
return nullptr;
}
std::vector<ConnectionEntry> Schema::availableConnections(std::string instanceName, std::string pinName, bool onlyConnectable) {
std::vector<ConnectionEntry> entries;
auto instance = getComponentInstance(instanceName);
ConnectionComponent connectionComponent{instance->component.getName(), pinName};
for(auto &conn: library.getConnections()) {
if(conn.isConnecting(connectionComponent)) {
// if bus connection
if(library.getBus(conn.getBus()).getType() == Bus::REGULAR) {
for(auto& bus: busInstances) {
if(bus->bus.getName() == conn.getBus()) {
ConnectionEntry entry{ConnectionEntry::BUS, bus.get(), nullopt, nullopt, conn};
entries.emplace_back(entry);
}
}
} else {
for(auto& component: componentInstances) {
for(auto& pin: component->component.getPins()) {
if(conn.isConnecting(connectionComponent, {component->component.getName(), pin.getName()})) {
ConnectionEntry entry{ConnectionEntry::COMPONENT, nullopt, component.get(), pin, conn};
entries.emplace_back(entry);
}
}
}
}
}
}
if(onlyConnectable) {
entries.erase(
std::remove_if(
entries.begin(),
entries.end(),
[this, instance](ConnectionEntry &entry) {
auto& bus = this->library.getBus(entry.connection.getBus());
// we allow duplicates of single automatic connections
if(bus.getType() == Bus::SINGLE_AUTOMATIC) {
return false;
}
for(auto& conn: this->connections) {
if(conn->connection == entry.connection) {
if(bus.getType() == Bus::REGULAR) {
if(entry.busInstance.value()->bus.getName() == conn->connection.getBus()) {
return true;
}
} else {
auto *directInstance = dynamic_cast<DirectConnectionInstance*>(conn.get());
ComponentInstance* secondInstance;
if(directInstance->instance == instance) {
secondInstance = directInstance->secondInstance;
} else {
secondInstance = directInstance->instance;
}
return entry.componentInstance == secondInstance;
}
}
}
return false;
}),
entries.end()
);
}
return entries;
}
2022-04-05 21:48:07 +00:00
} // namespace domain