Refactored instances

This commit is contained in:
Borna Rajković 2022-05-08 15:47:47 +02:00
parent fb8aafb2a9
commit 74a7ef3e7a
7 changed files with 37 additions and 41 deletions

View File

@ -8,14 +8,14 @@ namespace domain {
struct ValidationError struct ValidationError
{ {
Instance *instance; ComponentInstance *instance;
InstanceAttribute *attribute; InstanceAttribute *attribute;
Action::ActionType type; Action::ActionType type;
std::string message; std::string message;
}; };
struct ValidationContext { struct ValidationContext {
Instance *instance; ComponentInstance *instance;
InstanceAttribute *attribute; InstanceAttribute *attribute;
std::map<std::string, AddressSpace> addressSpaces; std::map<std::string, AddressSpace> addressSpaces;
std::map<std::string, Value> attributes; std::map<std::string, Value> attributes;

View File

@ -2,15 +2,11 @@
namespace domain { namespace domain {
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) BusInstance::BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size)
: Instance(name, vector<InstanceAttribute>(), position), bus(bus), size(size) : name(name), position(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) : name(name), attributes(std::move(attributes)), position(position), component(component)
{} {}

View File

@ -12,41 +12,32 @@
namespace domain { namespace domain {
class Instance class BusInstance
{
public:
std::string name;
std::pair<int, int> position;
Bus bus;
int size;
BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size);
virtual ~BusInstance() = default;
};
class ComponentInstance
{ {
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() {};
Instance(std::string name, std::vector<InstanceAttribute> attributes, std::pair<int, int> position);
};
class BusInstance: public Instance
{
public:
Bus bus;
int size;
BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size);
virtual ~BusInstance() {
Instance::~Instance();
}
};
class ComponentInstance: public Instance
{
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() = default;
Instance::~Instance();
}
}; };
} // namespace domain } // namespace domain

View File

@ -15,10 +15,11 @@ class InstanceAttribute
public: public:
InstanceAttribute(std::string name, Value value, Attribute attribute); InstanceAttribute(std::string name, Value value, Attribute attribute);
std::string name; std::string name = "";
Value value; Value value;
Attribute attribute; Attribute attribute;
~InstanceAttribute() = default;
}; };
} // namespace domain } // namespace domain

View File

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

View File

@ -787,7 +787,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library)
} }
for(auto &conn: node.connections) { for(auto &conn: node.connections) {
auto firstComponent = dynamic_cast<ComponentInstance*>(schema->getInstance(conn.first.instance.value)); auto firstComponent = schema->getComponentInstance(conn.first.instance.value);
if(firstComponent == nullptr) { if(firstComponent == nullptr) {
errors.emplace_back(conn.first.instance.span, "unknown component"); errors.emplace_back(conn.first.instance.span, "unknown component");
continue; continue;
@ -800,7 +800,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library)
ComponentInstance *secondComponent = nullptr; ComponentInstance *secondComponent = nullptr;
if(conn.second.has_value()) { if(conn.second.has_value()) {
secondComponent = dynamic_cast<ComponentInstance*>(schema->getInstance(conn.second->instance.value)); secondComponent = schema->getComponentInstance(conn.second->instance.value);
if(secondComponent == nullptr) { if(secondComponent == nullptr) {
errors.emplace_back(conn.second->instance.span, "unknown component"); errors.emplace_back(conn.second->instance.span, "unknown component");
continue; continue;
@ -811,7 +811,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library)
} }
} }
auto bus = dynamic_cast<BusInstance*>(schema->getInstance(conn.bus.value)); auto bus = schema->getBusInstance(conn.bus.value);
if(bus == nullptr) { if(bus == nullptr) {
errors.emplace_back(conn.bus.span, "unknown bus"); errors.emplace_back(conn.bus.span, "unknown bus");
continue; continue;

View File

@ -129,7 +129,11 @@ void MainWindow::onLoadSchema() {
for (auto& error : generator.getErrors()) { for (auto& error : generator.getErrors()) {
parseContext.formatError(error, buffer, "ERROR: "); parseContext.formatError(error, buffer, "ERROR: ");
delete schema;
schema = nullptr;
} }
} else {
library = std::nullopt;
} }
if(generator.getErrors().empty()) { if(generator.getErrors().empty()) {
@ -196,7 +200,6 @@ void MainWindow::onValidateSchema(bool /*toggled*/) {
return; return;
} }
this->validationErrors.clear(); this->validationErrors.clear();
std::map<std::string, domain::FunctionCallback> callbacks; std::map<std::string, domain::FunctionCallback> callbacks;
@ -246,7 +249,10 @@ void MainWindow::onValidateSchema(bool /*toggled*/) {
void MainWindow::clear() { void MainWindow::clear() {
validationErrors.clear(); validationErrors.clear();
if(schema != nullptr) {
delete schema;
schema = nullptr; schema = nullptr;
}
library = std::nullopt; library = std::nullopt;
libraryDisplay->setLibrary(library); libraryDisplay->setLibrary(library);