diff options
-rw-r--r-- | Circuit.cpp | 5 | ||||
-rw-r--r-- | GateAnd.cpp | 2 | ||||
-rw-r--r-- | GateAnd.h | 2 | ||||
-rw-r--r-- | Net.cpp | 3 | ||||
-rw-r--r-- | Node.cpp | 2 | ||||
-rw-r--r-- | NodeInput.cpp | 31 | ||||
-rw-r--r-- | NodeInput.h | 27 | ||||
-rw-r--r-- | NodeOutput.cpp | 4 | ||||
-rw-r--r-- | Observer.cpp | 7 | ||||
-rw-r--r-- | Observer.h | 8 | ||||
-rw-r--r-- | docs/class-diag.puml | 4 | ||||
-rw-r--r-- | main.cpp | 29 | ||||
-rw-r--r-- | makefile | 2 | ||||
-rw-r--r-- | prut.h | 6 |
14 files changed, 54 insertions, 78 deletions
diff --git a/Circuit.cpp b/Circuit.cpp index 8137939..d6a5375 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -2,6 +2,8 @@ #include "Exception.h" #include "NodeFactory.h" +#include "prut.h" + void Circuit::create(string label, vector<string> nodes) { if (nodes.size() == 1 && NodeFactory::has_type(nodes[0])) return new_node(label, nodes[0]); @@ -19,7 +21,7 @@ void Circuit::new_node(string label, string type) { nodes[label] = node; - printf("[%s] (%s)\n", label.c_str(), type.c_str()); + prutprintf("[%s] (%s)", label.c_str(), type.c_str()); } void Circuit::new_net(string src, vector<string> dests) { @@ -36,6 +38,7 @@ void Circuit::new_net(string src, vector<string> dests) { if (node == nullptr) throw CircuitException("unknown destination node \"%s\"", dest.c_str()); node->addInput(net); + prutprintf("%s -> %s", src.c_str(), dest.c_str()); } } diff --git a/GateAnd.cpp b/GateAnd.cpp index c48d19c..e51df30 100644 --- a/GateAnd.cpp +++ b/GateAnd.cpp @@ -2,8 +2,6 @@ GateAnd GateAnd::instance(GateAnd::type); -GateAnd::GateAnd(const char * type) : Node(type) { } - SignalLevel GateAnd::level() { for (int i = 0; i < this->inputs.size(); i++){ SignalLevel l = this->inputs[i]->getLevel(); @@ -14,7 +14,7 @@ protected: int min_inputs = 0; int max_inputs = -1; - GateAnd(const char * type); + using Node::Node; private: constexpr static const char * type = "and"; @@ -1,9 +1,10 @@ #include "Net.h" #include <iostream> +#include "prut.h" + void Net::setLevel(SignalLevel level){ this->level = level; - std::cout << this->size() << std::endl; this->notify(); } @@ -4,6 +4,8 @@ #include "NodeFactory.h" #include "Net.h" +#include "prut.h" + Node::Node(const char * type) { NodeFactory::assign(type, this); } diff --git a/NodeInput.cpp b/NodeInput.cpp index 0fc8110..2d1d517 100644 --- a/NodeInput.cpp +++ b/NodeInput.cpp @@ -1,14 +1,10 @@ #include "NodeInput.h" +#include "prut.h" #include <iostream> -NodeInput::NodeInput(const char * type) : Node(type) { } - -void NodeInput::sim() { - if (this->output == nullptr) return; - - this->output->setLevel(this->input); -} +NodeInputLow NodeInputLow::instance(NodeInputLow::type); +NodeInputHigh NodeInputHigh::instance(NodeInputHigh::type); NodeInput::NodeInput(const NodeInput * prototype) : Node() { } @@ -16,13 +12,16 @@ 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() { } +SignalLevel NodeInput::level() { + prutprint(""); + return UNDEFINED; +} +SignalLevel NodeInputLow::level() { + prutprint(""); + return LOW; +} +SignalLevel NodeInputHigh::level() { + prutprint(""); + return HIGH; +} -// // INPUT_HIGH -// NodeInputHigh NodeInputHigh::instance(NodeInputHigh::type); -// NodeInputHigh::NodeInputHigh(const char * type) : NodeInput(type) { } -// NodeInputHigh::NodeInputHigh(const NodeInputHigh * prototype) : NodeInput() { } diff --git a/NodeInput.h b/NodeInput.h index 82dfd4c..e176fa7 100644 --- a/NodeInput.h +++ b/NodeInput.h @@ -2,13 +2,13 @@ #include "Node.h" +#include "prut.h" + class NodeInput : public Node { public: - NodeInput() = default; + using Node::Node; NodeInput(const NodeInput * prototype); - ~NodeInput() = default; - virtual void sim(); - SignalLevel level() { return UNDEFINED; }; + virtual NodeInput * clone() const; private: @@ -17,28 +17,23 @@ private: SignalLevel input = UNDEFINED; }; -// Input LOW and HIGH unicorns: - class NodeInputLow : public NodeInput { public: - // NodeInputLow(const NodeInputLow * prototype); - // ~NodeInputLow() = default; - // virtual void compare(); - // virtual NodeInputLow * clone() const; + using NodeInput::NodeInput; + virtual SignalLevel level(); private: - NodeInputLow(const char * type); constexpr static const char * type = "input_low"; static NodeInputLow instance; - - SignalLevel input = LOW; }; class NodeInputHigh : public NodeInput { +public: + using NodeInput::NodeInput; + virtual SignalLevel level(); + private: - NodeInputHigh(const char * type); constexpr static const char * type = "input_high"; static NodeInputHigh instance; - - SignalLevel input = HIGH; }; + diff --git a/NodeOutput.cpp b/NodeOutput.cpp index 5138805..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,7 +11,7 @@ void NodeOutput::sim() { if (this->inputs.size() == 0) throw CircuitException("No inputs on probe"); - std::cout << "Probe signal level: " << this->inputs[0]->getLevel() << std::endl; + prutprintf("level: %u", this->inputs[0]->getLevel()); } NodeOutput::NodeOutput(const NodeOutput * prototype) : Node() { } diff --git a/Observer.cpp b/Observer.cpp index 7d5dc22..39a8245 100644 --- a/Observer.cpp +++ b/Observer.cpp @@ -3,6 +3,7 @@ #include "Observer.h" void Subject::attach(Observer * obs){ + // std::cout << "added" << std::endl; this->observers.push_back(obs); } @@ -11,12 +12,12 @@ void Subject::detach(Observer *){ } int Subject::size() { + // 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(); } @@ -2,11 +2,8 @@ #include <vector> class Observer { -private: - public: 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/docs/class-diag.puml b/docs/class-diag.puml index 4d9f489..971de54 100644 --- a/docs/class-diag.puml +++ b/docs/class-diag.puml @@ -61,8 +61,8 @@ Net -- SignalLevel Node -- SignalLevel Node <|-[dashed]-- GateAnd -Node <|-[dashed]-- GateNand -Node <|-[dashed]-- GateNor +GateAnd <|-- GateNand +GateOr <|-- GateNor Node <|-[dashed]-- GateNot Node <|-[dashed]-- GateOr Node <|-[dashed]-- GateXor @@ -4,35 +4,12 @@ #include "Parser.h" #include "Circuit.h" +#include "prut.h" + using std::cout; using std::endl; using std::ifstream; -#include "Node.h" -#include "GateAnd.h" - -int main() { - // Observer ob(); - Net n, n1, o; - Node *g = new GateAnd; - try { - g->addInput(&n); - g->addInput(&n1); - g->setOutput(&o); - - // o.setLevel(UNDEFINED); - n.setLevel(HIGH); - n1.setLevel(HIGH); - int level = 22; - level = o.getLevel(); - printf("hello world! %d\n", level); - } catch(Exception& e) { - std::cerr << e.what() << '\n'; - } - return 0; -} - -/* int main(int argc, char** argv) { Parser main_parser; Circuit circuit; @@ -48,6 +25,7 @@ int main(int argc, char** argv) { cout << "Parser error: " << e.what() << endl; return EXIT_FAILURE; } + prutprint("parsing done!"); try { circuit.sim(); @@ -61,4 +39,3 @@ int main(int argc, char** argv) { return EXIT_SUCCESS; } -*/
\ No newline at end of file @@ -20,3 +20,5 @@ clean: compile_commands.json: compiledb make -Bn + +_Bj: ; $(MAKE) -C . -Bj @@ -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) + |