diff options
-rw-r--r-- | Circuit.cpp | 33 | ||||
-rw-r--r-- | Circuit.h | 6 | ||||
-rw-r--r-- | GateAnd.cpp | 43 | ||||
-rw-r--r-- | Net.cpp | 4 | ||||
-rw-r--r-- | Net.h | 12 | ||||
-rw-r--r-- | Node.cpp | 9 | ||||
-rw-r--r-- | Node.h | 1 | ||||
-rw-r--r-- | Observer.cpp | 9 | ||||
-rw-r--r-- | docs/class-diag.puml | 2 |
9 files changed, 68 insertions, 51 deletions
diff --git a/Circuit.cpp b/Circuit.cpp index cf6312e..7da9384 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -5,8 +5,7 @@ void Circuit::create(string label, vector<string> nodes) { if (nodes.size() == 1 && NodeFactory::has_type(nodes[0])) return new_node(label, nodes[0]); - for (string node : nodes) - new_net(label, node); + new_net(label, nodes); } void Circuit::new_node(string label, string type) { @@ -20,16 +19,32 @@ void Circuit::new_node(string label, string type) { printf("[%s] (%s)\n", label.c_str(), type.c_str()); } -void Circuit::new_net(string label, string connection) { - // TODO: instance Net - // TODO: connect Net to connection - printf("[%s] -> %s\n", label.c_str(), connection.c_str()); +void Circuit::new_net(string src, vector<string> dests) { + printf("%s\n", src.c_str()); + Net * net = new Net(); + nets.push_back(net); + + for (auto dest : dests) { + Node * node = find_node(dest); + if (node == nullptr) continue; // TODO: exception! + node->addInput(net); + } + + 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 const & n : nodes) - delete n.second; - for (auto const & n : nets) + for (auto & n : nodes) delete n.second; + for (auto & n : nets) + delete n; } @@ -18,10 +18,12 @@ public: public: void create(string label, vector<string> nodes); void new_node(string label, string type); - void new_net(string label, string node); + void new_net(string src, vector<string> dests); private: std::map<string, Node *> nodes = {}; - std::map<string, Net *> nets = {}; + vector<Net *> nets = {}; + + virtual Node * find_node(string label); }; diff --git a/GateAnd.cpp b/GateAnd.cpp index dc65353..6f19d61 100644 --- a/GateAnd.cpp +++ b/GateAnd.cpp @@ -4,31 +4,28 @@ GateAnd GateAnd::instance(GateAnd::type); GateAnd::GateAnd(const char * type) : Node(type) { } -// Concrete Nodes: void GateAnd::compare() { SignalLevel new_out = HIGH; -// TODO fix segfault somewhere below -// for (int i = 0; i < this->inputs.size(); i++){ -// switch (this->inputs[i]->getLevel()){ -// case LOW: -// new_out = LOW; -// break; -// case HIGH: -// continue; -// break; -// case UNDEFINED: -// default: -// new_out = UNDEFINED; -// exit; -// break; -// } -// } - -// if (this->output->getLevel() == new_out){ -// /* do nothing */ -// } else { -// this->output->setLevel(new_out); -// } + // TODO: fix segfault somewhere below + for (int i = 0; i < this->inputs.size(); i++){ + switch (this->inputs[i]->getLevel()){ + case LOW: + new_out = LOW; + break; + case HIGH: + continue; + break; + case UNDEFINED: + default: + new_out = UNDEFINED; + // TODO: exception!! + break; + } + } + + if (this->output->getLevel() == new_out) return; + + this->output->setLevel(new_out); } GateAnd::GateAnd(const GateAnd * prototype) : Node() { } @@ -1,10 +1,6 @@ #include "Net.h" #include <iostream> -Net::Net(){} - -Net::~Net(){} - void Net::setLevel(SignalLevel level){ this->level = level; std::cout << this->size() << std::endl; @@ -2,14 +2,18 @@ #include "Observer.h" -enum SignalLevel {LOW, HIGH, UNDEFINED}; +enum SignalLevel { + LOW, + HIGH, + UNDEFINED +}; -class Net: public Subject { +class Net : public Subject { private: SignalLevel level = UNDEFINED; public: - Net(/* args */); - ~Net(); + Net() = default; + virtual ~Net() = default; virtual void setLevel(SignalLevel); virtual SignalLevel getLevel(); }; @@ -1,17 +1,18 @@ +#include <iostream> + #include "Node.h" #include "NodeFactory.h" - -#include <iostream> +#include "Net.h" Node::Node(const char * type) { NodeFactory::assign(type, this); } -void Node::addInput(Net* net) { +void Node::addInput(Net * net) { net->attach(this); } -void Node::setOutput(Net* net){ +void Node::setOutput(Net * net){ this->output = net; } @@ -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(); diff --git a/docs/class-diag.puml b/docs/class-diag.puml index 5efda1b..3c572bd 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -67,7 +67,7 @@ class Circuit { + createNode(string type, string label) + createLink(string labelA, string labelB) - - nets: Map<string label, Net*> + - nets: vector<Net*> - nodes: Map<string label, Node*> } |