aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 12:30:48 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 12:30:48 +0200
commitc9c4b6caa2a14b7b5a338a6981a616506da7e78f (patch)
tree1b6c30d6561a342578bf7b197d94cda755ba4aa2
parent2b941bc1b28e0a60da50baa0875d7cb05c0632c1 (diff)
connect the nodes (no segfault edition)
-rw-r--r--Circuit.cpp10
-rw-r--r--Circuit.h2
-rw-r--r--Node.h1
-rw-r--r--Observer.cpp9
4 files changed, 16 insertions, 6 deletions
diff --git a/Circuit.cpp b/Circuit.cpp
index 393060f..7da9384 100644
--- a/Circuit.cpp
+++ b/Circuit.cpp
@@ -25,16 +25,22 @@ void Circuit::new_net(string src, vector<string> dests) {
nets.push_back(net);
for (auto dest : dests) {
- Node * node = nodes.find(dest)->second;
+ Node * node = find_node(dest);
if (node == nullptr) continue; // TODO: exception!
node->addInput(net);
}
- Node * node = nodes.find(src)->second;
+ Node * node = find_node(src);
if (node == nullptr) return; // TODO: exception!
node->setOutput(net);
}
+Node * Circuit::find_node(string label) {
+ auto map_index = this->nodes.find(label);
+ if (map_index == nodes.end()) return nullptr;
+ return map_index->second;
+}
+
Circuit::~Circuit() {
for (auto & n : nodes)
delete n.second;
diff --git a/Circuit.h b/Circuit.h
index e681742..1806293 100644
--- a/Circuit.h
+++ b/Circuit.h
@@ -23,5 +23,7 @@ public:
private:
std::map<string, Node *> nodes = {};
vector<Net *> nets = {};
+
+ virtual Node * find_node(string label);
};
diff --git a/Node.h b/Node.h
index 43cfc6c..e6270a7 100644
--- a/Node.h
+++ b/Node.h
@@ -20,6 +20,7 @@ public:
virtual void addInput(Net *);
virtual void setOutput(Net *);
virtual void compare() = 0;
+ int gert = 45;
protected:
Node(const char * type);
diff --git a/Observer.cpp b/Observer.cpp
index 1d96ed4..8d9823b 100644
--- a/Observer.cpp
+++ b/Observer.cpp
@@ -1,20 +1,21 @@
-#include "Observer.h"
#include <iostream>
+#include "Observer.h"
+
void Observer::update(){
std::cout << 'a' << std::endl;
}
-void Subject::attach(Observer* obs){
+void Subject::attach(Observer * obs){
std::cout << "added" << std::endl;
this->observers.push_back(obs);
}
-void Subject::detach(Observer*){
+void Subject::detach(Observer *){
}
-// TODO possibly add foo input as update value?
+// TODO: possibly add foo input as update value?
void Subject::notify() {
for (int i = 0; i < this->observers.size(); i++)
this->observers[i]->update();