diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-12 12:30:48 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-12 12:30:48 +0200 |
commit | c9c4b6caa2a14b7b5a338a6981a616506da7e78f (patch) | |
tree | 1b6c30d6561a342578bf7b197d94cda755ba4aa2 | |
parent | 2b941bc1b28e0a60da50baa0875d7cb05c0632c1 (diff) |
connect the nodes (no segfault edition)
-rw-r--r-- | Circuit.cpp | 10 | ||||
-rw-r--r-- | Circuit.h | 2 | ||||
-rw-r--r-- | Node.h | 1 | ||||
-rw-r--r-- | Observer.cpp | 9 |
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; @@ -23,5 +23,7 @@ public: private: std::map<string, Node *> nodes = {}; vector<Net *> nets = {}; + + virtual Node * find_node(string label); }; @@ -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(); |