From c9c4b6caa2a14b7b5a338a6981a616506da7e78f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 12 Jun 2024 12:30:48 +0200 Subject: connect the nodes (no segfault edition) --- Circuit.cpp | 10 ++++++++-- Circuit.h | 2 ++ Node.h | 1 + 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 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 nodes = {}; vector 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 +#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(); -- cgit v1.2.3