diff options
-rw-r--r-- | Circuit.cpp | 19 | ||||
-rw-r--r-- | Circuit.h | 1 | ||||
-rw-r--r-- | Node.cpp | 1 | ||||
-rw-r--r-- | Node.h | 6 | ||||
-rw-r--r-- | NodeInput.cpp | 10 | ||||
-rw-r--r-- | NodeInput.h | 2 | ||||
-rw-r--r-- | NodeOutput.h | 4 | ||||
-rw-r--r-- | NodeOutputVisitor.cpp | 8 | ||||
-rw-r--r-- | NodeOutputVisitor.h | 17 | ||||
-rw-r--r-- | NodeVisitor.h | 11 | ||||
-rw-r--r-- | main.cpp | 4 | ||||
-rw-r--r-- | prut.h | 9 |
12 files changed, 78 insertions, 14 deletions
diff --git a/Circuit.cpp b/Circuit.cpp index 62dfae4..e89fcb1 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -1,9 +1,14 @@ +#include <format> + #include "Circuit.h" #include "Exception.h" #include "NodeFactory.h" +#include "NodeOutputVisitor.h" #include "prut.h" +using std::format; + void Circuit::create(string label, vector<string> nodes) { if (nodes.size() == 1 && NodeFactory::has_type(nodes[0])) return new_node(label, nodes[0]); @@ -60,3 +65,17 @@ Circuit::~Circuit() { delete n; } +string Circuit::result() { + string output; + + for (auto & n : nodes) { + NodeOutputVisitor visitor; + n.second->accept(visitor); + if (!visitor.output_node) continue; + + output += std::format("{}: {}\n", n.first, std::to_string(visitor.level)); + } + + return output; +} + @@ -20,6 +20,7 @@ public: virtual void new_node(string label, string type); virtual void new_net(string src, vector<string> dests); virtual void sim(); + virtual string result(); private: std::map<string, Node *> nodes = {}; @@ -3,6 +3,7 @@ #include "Node.h" #include "NodeFactory.h" #include "Net.h" +#include "Exception.h" #include "prut.h" @@ -5,7 +5,7 @@ #include "Observer.h" #include "Net.h" -#include "Exception.h" +#include "NodeVisitor.h" using std::string; using std::vector; @@ -32,5 +32,9 @@ protected: protected: int min_inputs = -1; int max_inputs = -1; + +public: + virtual void accept(NodeVisitor & visitor) { visitor.visit(*this); } + friend NodeVisitor; }; diff --git a/NodeInput.cpp b/NodeInput.cpp index 6ee0db4..41d5352 100644 --- a/NodeInput.cpp +++ b/NodeInput.cpp @@ -12,16 +12,10 @@ NodeInput::NodeInput() { NodeInputLow * NodeInputLow::clone() const { return new NodeInputLow(this); } -SignalLevel NodeInputLow::level() { - prutprint("LOW"); - return LOW; -} +SignalLevel NodeInputLow::level() { return LOW; } NodeInputHigh * NodeInputHigh::clone() const { return new NodeInputHigh(this); } -SignalLevel NodeInputHigh::level() { - prutprint("HIGH"); - return HIGH; -} +SignalLevel NodeInputHigh::level() { return HIGH; } diff --git a/NodeInput.h b/NodeInput.h index 4bd7b59..9b87f3b 100644 --- a/NodeInput.h +++ b/NodeInput.h @@ -20,7 +20,6 @@ public: protected: SignalLevel level(); using NodeInput::NodeInput; - // NodeInputLow(const char * type) : NodeInput(type) {} private: NodeInputLow(const NodeInputLow *) : NodeInput() {} @@ -35,7 +34,6 @@ public: protected: SignalLevel level(); using NodeInput::NodeInput; - // NodeInputHigh(const char * type) : NodeInput(type) {} private: NodeInputHigh(const NodeInputHigh *) : NodeInput() {} diff --git a/NodeOutput.h b/NodeOutput.h index 3b60335..cd25055 100644 --- a/NodeOutput.h +++ b/NodeOutput.h @@ -14,4 +14,8 @@ private: NodeOutput(const char * type); constexpr static const char * type = "probe"; static NodeOutput instance; + +public: + virtual void accept(NodeVisitor & visitor) { visitor.visit(*this); } }; + diff --git a/NodeOutputVisitor.cpp b/NodeOutputVisitor.cpp new file mode 100644 index 0000000..ca00fd4 --- /dev/null +++ b/NodeOutputVisitor.cpp @@ -0,0 +1,8 @@ +#include "NodeOutputVisitor.h" + +void NodeOutputVisitor::visit(Node & node) { } +void NodeOutputVisitor::visit(NodeOutput & node) { + level = node.level(); // TODO: dit klopt niet + output_node = true; +} + diff --git a/NodeOutputVisitor.h b/NodeOutputVisitor.h new file mode 100644 index 0000000..710711c --- /dev/null +++ b/NodeOutputVisitor.h @@ -0,0 +1,17 @@ +#pragma once + +#include "NodeVisitor.h" +#include "NodeOutput.h" + +class NodeOutputVisitor : public NodeVisitor { +public: + NodeOutputVisitor() = default; + virtual ~NodeOutputVisitor() = default; + + virtual void visit(Node & node); + virtual void visit(NodeOutput & node); + + bool output_node = false; + SignalLevel level = UNDEFINED; +}; + diff --git a/NodeVisitor.h b/NodeVisitor.h new file mode 100644 index 0000000..f98a8ba --- /dev/null +++ b/NodeVisitor.h @@ -0,0 +1,11 @@ +#pragma once + +class Node; +class NodeOutput; + +class NodeVisitor { +public: + virtual void visit(NodeOutput & node) = 0; + virtual void visit(Node & node) = 0; +}; + @@ -35,8 +35,8 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - // cout << "Circuit output: " << circuit.getOutput() << endl; - // cout << circuit.result(); + // print results + cout << circuit.result(); return EXIT_SUCCESS; } @@ -1,6 +1,13 @@ #pragma once -#define prutprintf(fmt, ...) printf("\x1b[37m%s (%s:%d):\x1b[0m " fmt "\n", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__, __VA_ARGS__) +// Disable prutprint +#define prutprintf(fmt, ...) + +// Short version "file:line: message" +// #define prutprintf(fmt, ...) printf("\x1b[37m%s:%d:\x1b[0m " fmt "\n", __FILE_NAME__, __LINE__, __VA_ARGS__) + +// Long version "function (file:line): message" +// #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) |