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
{
Instance *instance;
ComponentInstance *instance;
InstanceAttribute *attribute;
Action::ActionType type;
std::string message;
};
struct ValidationContext {
Instance *instance;
ComponentInstance *instance;
InstanceAttribute *attribute;
std::map<std::string, AddressSpace> addressSpaces;
std::map<std::string, Value> attributes;

View File

@ -2,15 +2,11 @@
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)
: 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)
: Instance(name, attributes, position), component(component)
: name(name), attributes(std::move(attributes)), position(position), component(component)
{}

View File

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

View File

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

View File

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

View File

@ -787,7 +787,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library)
}
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) {
errors.emplace_back(conn.first.instance.span, "unknown component");
continue;
@ -800,7 +800,7 @@ Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library)
ComponentInstance *secondComponent = nullptr;
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) {
errors.emplace_back(conn.second->instance.span, "unknown component");
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) {
errors.emplace_back(conn.bus.span, "unknown bus");
continue;

View File

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