aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 16:56:19 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 16:56:19 +0200
commite689d7947050face22fa1533d9a1cba6d43ea6b8 (patch)
tree7209bf2a04a4254913de6b0ef26742c85479c2eb
parentc0c6828e0652b177d36d15ded9801e419c07a64c (diff)
add prut
-rw-r--r--GateAnd.cpp2
-rw-r--r--GateAnd.h2
-rw-r--r--Net.cpp7
-rw-r--r--Node.cpp4
-rw-r--r--NodeInput.cpp13
-rw-r--r--NodeInput.h18
-rw-r--r--NodeOutput.cpp6
-rw-r--r--Observer.cpp13
-rw-r--r--Observer.h10
-rw-r--r--prut.h6
10 files changed, 38 insertions, 43 deletions
diff --git a/GateAnd.cpp b/GateAnd.cpp
index 487f8bc..987d5e4 100644
--- a/GateAnd.cpp
+++ b/GateAnd.cpp
@@ -3,8 +3,6 @@
GateAnd GateAnd::instance(GateAnd::type);
-GateAnd::GateAnd(const char * type) : Node(type) { }
-
SignalLevel GateAnd::level() {
if (this->inputs.size() < 1) throw CircuitException("AndGate input size error");
diff --git a/GateAnd.h b/GateAnd.h
index 142c628..f56ca47 100644
--- a/GateAnd.h
+++ b/GateAnd.h
@@ -13,7 +13,7 @@ public:
private:
SignalLevel level();
- GateAnd(const char * type);
+ using Node::Node;
constexpr static const char * type = "and";
static GateAnd instance;
};
diff --git a/Net.cpp b/Net.cpp
index 249b6bb..801210b 100644
--- a/Net.cpp
+++ b/Net.cpp
@@ -1,9 +1,14 @@
#include "Net.h"
#include <iostream>
+#include "prut.h"
+
void Net::setLevel(SignalLevel level){
this->level = level;
- std::cout << this->size() << std::endl;
+ // std::cout << this->size() << std::endl;
+ prutprintf("%u", this->size());
+
+
this->notify();
}
diff --git a/Node.cpp b/Node.cpp
index 75f94e5..3fe911f 100644
--- a/Node.cpp
+++ b/Node.cpp
@@ -4,6 +4,8 @@
#include "NodeFactory.h"
#include "Net.h"
+#include "prut.h"
+
Node::Node(const char * type) {
NodeFactory::assign(type, this);
}
@@ -18,7 +20,7 @@ void Node::setOutput(Net * net){
}
void Node::update(){
- std::cout << "updated" << std::endl;
+ prutprint("updated");
this->sim();
}
diff --git a/NodeInput.cpp b/NodeInput.cpp
index 17f9ed5..618ed31 100644
--- a/NodeInput.cpp
+++ b/NodeInput.cpp
@@ -2,12 +2,13 @@
#include <iostream>
-NodeInput::NodeInput(const char * type) : Node(type) { }
+NodeInputLow NodeInputLow::instance(NodeInputLow::type);
+NodeInputHigh NodeInputHigh::instance(NodeInputHigh::type);
void NodeInput::sim() {
- if (this->output == nullptr) return;
- std::cout << this->level << " bar\n";
- this->output->setLevel(this->level);
+ if (this->output == nullptr) return;
+ // std::cout << this->level << " bar\n";
+ this->output->setLevel(this->level);
}
NodeInput::NodeInput(const NodeInput * prototype) : Node() { }
@@ -16,10 +17,6 @@ NodeInput * NodeInput::clone() const {
return new NodeInput(this);
}
-// INPUT_LOW
-NodeInputLow NodeInputLow::instance(NodeInputLow::type);
-NodeInputLow::NodeInputLow(const char * type) : NodeInput() { }
-
// NodeInputLow::NodeInputLow(const NodeInputLow * prototype) : NodeInput() { }
// // INPUT_HIGH
diff --git a/NodeInput.h b/NodeInput.h
index bcca8f4..e6206b9 100644
--- a/NodeInput.h
+++ b/NodeInput.h
@@ -4,21 +4,22 @@
class NodeInput : public Node {
public:
- NodeInput() = default;
+ NodeInput() = default;
NodeInput(const NodeInput * prototype);
~NodeInput() = default;
virtual void sim();
virtual NodeInput * clone() const;
-private:
- NodeInput(const char * type);
-
- SignalLevel level = UNDEFINED;
+protected:
+ using Node::Node;
+ SignalLevel level = UNDEFINED;
};
// Input LOW and HIGH unicorns:
class NodeInputLow : public NodeInput {
+ using NodeInput::NodeInput;
+
public:
// NodeInputLow(const NodeInputLow * prototype);
// ~NodeInputLow() = default;
@@ -26,18 +27,17 @@ public:
// virtual NodeInputLow * clone() const;
private:
- NodeInputLow(const char * type);
constexpr static const char * type = "input_low";
static NodeInputLow instance;
- SignalLevel level = LOW;
+ SignalLevel level = LOW;
};
class NodeInputHigh : public NodeInput {
private:
- NodeInputHigh(const char * type);
+ using NodeInput::NodeInput;
constexpr static const char * type = "input_high";
static NodeInputHigh instance;
- SignalLevel level = HIGH;
+ SignalLevel level = HIGH;
};
diff --git a/NodeOutput.cpp b/NodeOutput.cpp
index 00ca006..a02ba1f 100644
--- a/NodeOutput.cpp
+++ b/NodeOutput.cpp
@@ -1,7 +1,7 @@
#include "NodeOutput.h"
#include "Exception.h"
-#include <iostream>
+#include "prut.h"
NodeOutput NodeOutput::instance(NodeOutput::type);
@@ -11,11 +11,11 @@ void NodeOutput::sim() {
if (this->inputs.size() == 0)
throw CircuitException("No inputs on probe");
- std::cout << this->inputs[0]->getLevel() << "foo" << std::endl;
+ prutprintf("level: %u", this->inputs[0]->getLevel());
}
NodeOutput::NodeOutput(const NodeOutput * prototype) : Node() { }
NodeOutput * NodeOutput::clone() const {
return new NodeOutput(this);
-} \ No newline at end of file
+}
diff --git a/Observer.cpp b/Observer.cpp
index ff523b8..39a8245 100644
--- a/Observer.cpp
+++ b/Observer.cpp
@@ -2,12 +2,8 @@
#include "Observer.h"
-void Observer::update(){
- std::cout << 'a' << std::endl;
-}
-
void Subject::attach(Observer * obs){
- std::cout << "added" << std::endl;
+ // std::cout << "added" << std::endl;
this->observers.push_back(obs);
}
@@ -16,13 +12,12 @@ void Subject::detach(Observer *){
}
int Subject::size() {
- std::cout << "subject list size " << this->observers.size() << std::endl;
+ // std::cout << "subject list size " << this->observers.size() << std::endl;
return this->observers.size();
}
-// TODO possibly add foo input as update value?
void Subject::notify() {
- for (int i = 0; i < this->observers.size(); i++)
- this->observers[i]->update();
+ for (auto observer : this->observers)
+ observer->update();
}
diff --git a/Observer.h b/Observer.h
index 580d539..20ca887 100644
--- a/Observer.h
+++ b/Observer.h
@@ -2,11 +2,8 @@
#include <vector>
class Observer {
-private:
-
public:
- virtual void update();
-
+ virtual void update() = 0;
};
class Subject {
@@ -18,11 +15,6 @@ public:
virtual void detach(Observer*);
virtual int size();
- // TODO possibly add foo input as update value?
virtual void notify();
- // virtual void notify() {
- // for (int i = 0; i < observers.size(); i++)
- // observers.at(i)->update();
- // }
};
diff --git a/prut.h b/prut.h
new file mode 100644
index 0000000..a41c01c
--- /dev/null
+++ b/prut.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#define prutprintf(fmt, ...) printf("\x1b[37m%s (%s:%d):\x1b[0m " fmt "\n", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__, __VA_ARGS__)
+
+#define prutprint(s) prutprintf("%s", s)
+