From 8e0a865dd375baa71357ce817847ea8a9144434c Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:04:35 +0200 Subject: Added design pattern to readme list --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 1703a9f..3ae509d 100644 --- a/readme.md +++ b/readme.md @@ -11,6 +11,7 @@ make - Observer - Low binding factory - Prototype +- Strategy - Dependency injection - vast meer! -- cgit v1.2.3 From f134abd08ccfdb1ece39df4d4a0e422419bd2fed Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:19:46 +0200 Subject: Created In/output nodes --- NodeInput.cpp | 26 ++++++++++++++++++++++++++ NodeInput.h | 43 +++++++++++++++++++++++++++++++++++++++++++ NodeOutput.cpp | 18 ++++++++++++++++++ NodeOutput.h | 16 ++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 NodeInput.cpp create mode 100644 NodeInput.h create mode 100644 NodeOutput.cpp create mode 100644 NodeOutput.h diff --git a/NodeInput.cpp b/NodeInput.cpp new file mode 100644 index 0000000..046cd72 --- /dev/null +++ b/NodeInput.cpp @@ -0,0 +1,26 @@ +#include "NodeInput.h" + +NodeInput::NodeInput(const char * type) : Node(type) { } + +void NodeInput::compare() { + if (this->output == nullptr) return; + + this->output->setLevel(this->level); +} + +NodeInput::NodeInput(const NodeInput * prototype) : Node() { } + +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 +// 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 new file mode 100644 index 0000000..73ef6e1 --- /dev/null +++ b/NodeInput.h @@ -0,0 +1,43 @@ +#pragma once + +#include "Node.h" + +class NodeInput : public Node { +public: + NodeInput() = default; + NodeInput(const NodeInput * prototype); + ~NodeInput() = default; + virtual void compare(); + virtual NodeInput * clone() const; + +private: + NodeInput(const char * type); + + SignalLevel level = UNDEFINED; +}; + +// Input LOW and HIGH unicorns: + +class NodeInputLow : public NodeInput { +public: + // NodeInputLow(const NodeInputLow * prototype); + // ~NodeInputLow() = default; + // virtual void compare(); + // virtual NodeInputLow * clone() const; + +private: + NodeInputLow(const char * type); + constexpr static const char * type = "input_low"; + static NodeInputLow instance; + + SignalLevel level = LOW; +}; + +class NodeInputHigh : public NodeInput { +private: + NodeInputHigh(const char * type); + constexpr static const char * type = "input_high"; + static NodeInputHigh instance; + + SignalLevel level = HIGH; +}; diff --git a/NodeOutput.cpp b/NodeOutput.cpp new file mode 100644 index 0000000..cff66c0 --- /dev/null +++ b/NodeOutput.cpp @@ -0,0 +1,18 @@ +#include "NodeOutput.h" +#include + +NodeOutput NodeOutput::instance(NodeOutput::type); + +NodeOutput::NodeOutput(const char * type) : Node(type) { } + +void NodeOutput::compare() { + if (this->inputs.size() > 0) { + std::cout << this->inputs[0]->getLevel() << std::endl; + } +} + +NodeOutput::NodeOutput(const NodeOutput * prototype) : Node() { } + +NodeOutput * NodeOutput::clone() const { + return new NodeOutput(this); +} \ No newline at end of file diff --git a/NodeOutput.h b/NodeOutput.h new file mode 100644 index 0000000..4e7f742 --- /dev/null +++ b/NodeOutput.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Node.h" + +class NodeOutput : public Node { +public: + NodeOutput(const NodeOutput * prototype); + ~NodeOutput() = default; + virtual void compare(); + virtual NodeOutput * clone() const; + +private: + NodeOutput(const char * type); + constexpr static const char * type = "probe"; + static NodeOutput instance; +}; -- cgit v1.2.3 From 6fd8dcb9e0d33f7660c4e3a602a5c2a2b5091a7c Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:34:39 +0200 Subject: renamed compare function to sim function --- GateAnd.cpp | 2 +- GateAnd.h | 5 +++-- Node.cpp | 2 +- Node.h | 2 +- NodeInput.cpp | 6 ++++-- NodeInput.h | 2 +- NodeOutput.cpp | 6 ++++-- NodeOutput.h | 2 +- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/GateAnd.cpp b/GateAnd.cpp index 6f19d61..93edacb 100644 --- a/GateAnd.cpp +++ b/GateAnd.cpp @@ -4,7 +4,7 @@ GateAnd GateAnd::instance(GateAnd::type); GateAnd::GateAnd(const char * type) : Node(type) { } -void GateAnd::compare() { +void GateAnd::sim() { SignalLevel new_out = HIGH; // TODO: fix segfault somewhere below for (int i = 0; i < this->inputs.size(); i++){ diff --git a/GateAnd.h b/GateAnd.h index 5eedc07..12dad53 100644 --- a/GateAnd.h +++ b/GateAnd.h @@ -4,9 +4,10 @@ class GateAnd : public Node { public: + GateAnd() = default; GateAnd(const GateAnd * prototype); - virtual ~GateAnd() = default; - virtual void compare(); + ~GateAnd() = default; + virtual void sim(); virtual GateAnd * clone() const; private: diff --git a/Node.cpp b/Node.cpp index 1b0b9b8..d3564da 100644 --- a/Node.cpp +++ b/Node.cpp @@ -18,6 +18,6 @@ void Node::setOutput(Net * net){ void Node::update(){ std::cout << "updated" << std::endl; - this->compare(); + this->sim(); } diff --git a/Node.h b/Node.h index e6270a7..36da279 100644 --- a/Node.h +++ b/Node.h @@ -19,7 +19,7 @@ public: void update(); virtual void addInput(Net *); virtual void setOutput(Net *); - virtual void compare() = 0; + virtual void sim() = 0; int gert = 45; protected: diff --git a/NodeInput.cpp b/NodeInput.cpp index 046cd72..17f9ed5 100644 --- a/NodeInput.cpp +++ b/NodeInput.cpp @@ -1,10 +1,12 @@ #include "NodeInput.h" +#include + NodeInput::NodeInput(const char * type) : Node(type) { } -void NodeInput::compare() { +void NodeInput::sim() { if (this->output == nullptr) return; - + std::cout << this->level << " bar\n"; this->output->setLevel(this->level); } diff --git a/NodeInput.h b/NodeInput.h index 73ef6e1..bcca8f4 100644 --- a/NodeInput.h +++ b/NodeInput.h @@ -7,7 +7,7 @@ public: NodeInput() = default; NodeInput(const NodeInput * prototype); ~NodeInput() = default; - virtual void compare(); + virtual void sim(); virtual NodeInput * clone() const; private: diff --git a/NodeOutput.cpp b/NodeOutput.cpp index cff66c0..d47b6a3 100644 --- a/NodeOutput.cpp +++ b/NodeOutput.cpp @@ -5,9 +5,11 @@ NodeOutput NodeOutput::instance(NodeOutput::type); NodeOutput::NodeOutput(const char * type) : Node(type) { } -void NodeOutput::compare() { +void NodeOutput::sim() { if (this->inputs.size() > 0) { - std::cout << this->inputs[0]->getLevel() << std::endl; + std::cout << this->inputs[0]->getLevel() << "foo" << std::endl; + } else { + std::cout << "err: No inputs on probe" << std::endl; } } diff --git a/NodeOutput.h b/NodeOutput.h index 4e7f742..2e92bf0 100644 --- a/NodeOutput.h +++ b/NodeOutput.h @@ -6,7 +6,7 @@ class NodeOutput : public Node { public: NodeOutput(const NodeOutput * prototype); ~NodeOutput() = default; - virtual void compare(); + virtual void sim(); virtual NodeOutput * clone() const; private: -- cgit v1.2.3 From 00c11d618d46067ed8656e5e9c834773c189cbc0 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:49:40 +0200 Subject: removed gert :< --- Node.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Node.h b/Node.h index 36da279..b7f6681 100644 --- a/Node.h +++ b/Node.h @@ -20,7 +20,6 @@ public: virtual void addInput(Net *); virtual void setOutput(Net *); virtual void sim() = 0; - int gert = 45; protected: Node(const char * type); -- cgit v1.2.3 From b93c2d642d7c9f94f36f408f3614741b29161c14 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:50:08 +0200 Subject: de big exception --- Exception.cpp | 23 +++++++++++++++++++++++ Exception.h | 16 ++++++++++++++++ Parser.cpp | 19 ------------------- Parser.h | 11 ++--------- 4 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 Exception.cpp create mode 100644 Exception.h diff --git a/Exception.cpp b/Exception.cpp new file mode 100644 index 0000000..52e30dc --- /dev/null +++ b/Exception.cpp @@ -0,0 +1,23 @@ +#include "Exception.h" + +#include +#include + +Exception::Exception(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + size_t sz = vsnprintf(NULL, 0, fmt, args) + 1; + if (error != NULL) free(error); + error = (char *) malloc(sz); + vsnprintf(error, sz, fmt, args); + va_end(args); +} + +Exception::~Exception() { + if (error != NULL) + free(error); +} + +const char * Exception::what() { + return error; +} \ No newline at end of file diff --git a/Exception.h b/Exception.h new file mode 100644 index 0000000..818f3f0 --- /dev/null +++ b/Exception.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +class Exception : public std::exception { +public: + Exception(const char * fmt, ...); + virtual ~Exception(); + virtual const char * what(); + +private: + char * error = NULL; +}; + +class ParserException : public Exception { using Exception::Exception; }; +class CircuitException : public Exception { using Exception::Exception; }; diff --git a/Parser.cpp b/Parser.cpp index 84c3217..c8a90b4 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -1,29 +1,10 @@ #include #include -#include #include "Parser.h" using std::getline; -ParserException::ParserException(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - size_t sz = vsnprintf(NULL, 0, fmt, args) + 1; - if (error != NULL) free(error); - error = (char *) malloc(sz); - vsnprintf(error, sz, fmt, args); - va_end(args); -} - -ParserException::~ParserException() { - if (error != NULL) - free(error); -} - -const char * ParserException::what() { - return error; -} size_t Parser::filter(char * input) { size_t diff --git a/Parser.h b/Parser.h index 3a86eec..b6d7bd5 100644 --- a/Parser.h +++ b/Parser.h @@ -2,22 +2,15 @@ #include #include -#include + #include "Circuit.h" +#include "Exception.h" using std::istream; using std::string; -class ParserException : public std::exception { -public: - ParserException(const char * fmt, ...); - virtual ~ParserException(); - virtual const char * what(); -private: - char * error = NULL; -}; class Parser { public: -- cgit v1.2.3 From 064512cb02465ec598e46ae4bc7947395ed66dc2 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:58:02 +0200 Subject: now trowing error on no probe input --- NodeOutput.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/NodeOutput.cpp b/NodeOutput.cpp index d47b6a3..00ca006 100644 --- a/NodeOutput.cpp +++ b/NodeOutput.cpp @@ -1,4 +1,6 @@ #include "NodeOutput.h" +#include "Exception.h" + #include NodeOutput NodeOutput::instance(NodeOutput::type); @@ -6,11 +8,10 @@ NodeOutput NodeOutput::instance(NodeOutput::type); NodeOutput::NodeOutput(const char * type) : Node(type) { } void NodeOutput::sim() { - if (this->inputs.size() > 0) { - std::cout << this->inputs[0]->getLevel() << "foo" << std::endl; - } else { - std::cout << "err: No inputs on probe" << std::endl; - } + if (this->inputs.size() == 0) + throw CircuitException("No inputs on probe"); + + std::cout << this->inputs[0]->getLevel() << "foo" << std::endl; } NodeOutput::NodeOutput(const NodeOutput * prototype) : Node() { } -- cgit v1.2.3 From a7eb10e81568b65c39d6e1d042309916a9180a2b Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:00:51 +0200 Subject: WIP simulate starter --- Circuit.cpp | 6 ++++++ Circuit.h | 7 ++++--- main.cpp | 12 ++++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Circuit.cpp b/Circuit.cpp index 7da9384..23f49a6 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -35,6 +35,12 @@ void Circuit::new_net(string src, vector dests) { node->setOutput(net); } +void Circuit::sim() { + for (auto & node : nodes) { + node.second->sim(); + } +} + Node * Circuit::find_node(string label) { auto map_index = this->nodes.find(label); if (map_index == nodes.end()) return nullptr; diff --git a/Circuit.h b/Circuit.h index 1806293..0cc08c6 100644 --- a/Circuit.h +++ b/Circuit.h @@ -16,9 +16,10 @@ public: virtual ~Circuit(); public: - void create(string label, vector nodes); - void new_node(string label, string type); - void new_net(string src, vector dests); + virtual void create(string label, vector nodes); + virtual void new_node(string label, string type); + virtual void new_net(string src, vector dests); + virtual void sim(); private: std::map nodes = {}; diff --git a/main.cpp b/main.cpp index 01ddeb7..728155f 100644 --- a/main.cpp +++ b/main.cpp @@ -24,12 +24,12 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - // try { - // circuit.run(); - // } catch (exception& e) { - // cout << "Circuit error: " << e.what() << endl; - // return EXIT_FAILURE; - // } + try { + circuit.sim(); + } catch (CircuitException& e) { + cout << "Circuit error: " << e.what() << endl; + return EXIT_FAILURE; + } // cout << "Circuit output: " << circuit.getOutput() << endl; -- cgit v1.2.3