aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Gate.cpp45
-rw-r--r--Gate.h17
-rw-r--r--Net.cpp3
-rw-r--r--Net.h2
-rw-r--r--Observer.cpp12
-rw-r--r--Observer.h1
-rw-r--r--main.cpp5
7 files changed, 70 insertions, 15 deletions
diff --git a/Gate.cpp b/Gate.cpp
index 22d57e2..0724b44 100644
--- a/Gate.cpp
+++ b/Gate.cpp
@@ -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);
+ // }
}
diff --git a/Gate.h b/Gate.h
index 14c35ac..3f34b7a 100644
--- a/Gate.h
+++ b/Gate.h
@@ -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();
+};
diff --git a/Net.cpp b/Net.cpp
index 9ee86e7..be01152 100644
--- a/Net.cpp
+++ b/Net.cpp
@@ -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;
diff --git a/Net.h b/Net.h
index bf93377..81c8034 100644
--- a/Net.h
+++ b/Net.h
@@ -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();
}
diff --git a/Observer.h b/Observer.h
index e6d4f68..6ff8f3b 100644
--- a/Observer.h
+++ b/Observer.h
@@ -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();
diff --git a/main.cpp b/main.cpp
index d7709e2..05ec033 100644
--- a/main.cpp
+++ b/main.cpp
@@ -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);