diff options
-rw-r--r-- | Gate.cpp | 45 | ||||
-rw-r--r-- | Gate.h | 17 | ||||
-rw-r--r-- | Net.cpp | 3 | ||||
-rw-r--r-- | Net.h | 2 | ||||
-rw-r--r-- | Observer.cpp | 12 | ||||
-rw-r--r-- | Observer.h | 1 | ||||
-rw-r--r-- | main.cpp | 5 |
7 files changed, 70 insertions, 15 deletions
@@ -1,10 +1,47 @@ #include "Gate.h" -Gate::Gate(/* args */){} +#include <iostream> + +Gate::Gate(){} Gate::~Gate(){} -void Gate::addInput(Net*){ - +void Gate::addInput(Net* net){ + net->attach(this); +} +void Gate::setOutput(Net* net){ + this->output = net; +} + +void Gate::update(){ + std::cout << "updated" << std::endl; + this->compare(); } -void Gate::addOutput(Net*){ + +/*/ Concrete Gates: /*/ + +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); + // } } @@ -8,18 +8,25 @@ class Gate: Observer { - private: + protected: std::string label; std::string type; std::vector<Net*> inputs; - std::vector<Net*> outputs; + Net* output; public: Gate(/* args */); - ~Gate(); + virtual ~Gate(); + void update(); virtual void addInput(Net*); - virtual void addOutput(Net*); + virtual void setOutput(Net*); + virtual void compare() = 0; }; -class GateAnd +class GateAnd: public Gate { + public: + GateAnd(){}; + ~GateAnd(){}; + void compare(); +}; @@ -1,4 +1,5 @@ #include "Net.h" +#include <iostream> Net::Net(){} @@ -6,6 +7,8 @@ Net::~Net(){} void Net::setLevel(SignalLevel level){ this->level = level; + std::cout << this->size() << std::endl; + this->notify(); } SignalLevel Net::getLevel(){ return this->level; @@ -4,7 +4,7 @@ enum SignalLevel {LOW, HIGH, UNDEFINED}; -class Net: Subject { +class Net: public Subject { private: SignalLevel level = UNDEFINED; public: diff --git a/Observer.cpp b/Observer.cpp index adc2f8d..1e9c9bb 100644 --- a/Observer.cpp +++ b/Observer.cpp @@ -5,15 +5,19 @@ void Observer::update(){ std::cout << 'a' << std::endl; } + + void Subject::attach(Observer* obs){ - observers.push_back(obs); + std::cout << "added" << std::endl; + this->observers.push_back(obs); } + void Subject::detach(Observer*){ } - + // TODO possibly add foo input as update value? void Subject::notify() { - for (int i = 0; i < observers.size(); i++) - observers.at(i)->update(); + for (int i = 0; i < this->observers.size(); i++) + this->observers[i]->update(); } @@ -17,6 +17,7 @@ class Subject { // virtual void attach(Observer* obs) { observers.push_back(obs);} virtual void attach(Observer* obs); virtual void detach(Observer*); + virtual int size() { return this->observers.size(); } // TODO possibly add foo input as update value? virtual void notify(); @@ -1,11 +1,14 @@ #include <cstdio> // #include "Observer.h" #include "Net.h" +#include "Gate.h" int main(int argc, char** argv) { // Observer ob(); Net n; - // n.setLevel(HIGH); + Gate *g = new GateAnd; + g->addInput(&n); + n.setLevel(HIGH); int level = 22; level = n.getLevel(); printf("hello world! %d\n", level); |