From 3cf454ef959b36f90803fb63fcb2fe26b85335a0 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 16 Jun 2024 09:21:18 +0200 Subject: better errors + fix min/max input shit --- Circuit.cpp | 19 ++++++++++++++++--- Circuit.h | 2 +- Exception.cpp | 8 +++++++- Exception.h | 11 +++++++++++ Node.cpp | 8 ++++---- Node.h | 2 +- NodeInput.cpp | 8 ++++++-- NodeInput.h | 2 ++ NodeOutput.cpp | 2 +- readme.md | 34 ++-------------------------------- 10 files changed, 51 insertions(+), 45 deletions(-) diff --git a/Circuit.cpp b/Circuit.cpp index dfbb178..57fbb02 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -13,7 +13,7 @@ void Circuit::create(string label, vector nodes) { return new_node(label, nodes[0]); new_net(label, nodes); - ld->add_connection(label, nodes); + loops.add_connection(label, nodes); } void Circuit::new_node(string label, string type) { @@ -45,8 +45,21 @@ void Circuit::new_net(string src, vector dests) { } void Circuit::sim() { - for (auto & node : nodes) - node.second->sim(); + for (auto & pair : nodes) { + try { + Node * node = pair.second; + node->sim(); + } catch (CircuitException & e) { + const char * label = "???"; + Node * node = e.node; + if (node != nullptr) { + auto it = std::find_if(nodes.begin(), nodes.end(), + [node](auto && n) { return n.second == node; }); + if (it != nodes.end()) label = it->first.c_str(); + } + throw CircuitException("node %s: %s", label, e.what()); + } + } } Node * Circuit::find_node(string label) { diff --git a/Circuit.h b/Circuit.h index 05e4626..6819a6f 100644 --- a/Circuit.h +++ b/Circuit.h @@ -33,7 +33,7 @@ private: std::map nodes = {}; vector nets = {}; - LoopDetection * ld = new LoopDetection(); + LoopDetection loops = LoopDetection(); virtual Node * find_node(string label); }; diff --git a/Exception.cpp b/Exception.cpp index 46f420e..5cb7094 100644 --- a/Exception.cpp +++ b/Exception.cpp @@ -43,4 +43,10 @@ ParserException::ParserException(const char * fmt, ...) { va_format(args, fmt); va_end(args); } - +NodeException::NodeException(Node * node, const char * fmt, ...) { + this->node = node; + va_list args; + va_start(args, fmt); + va_format(args, fmt); + va_end(args); +} diff --git a/Exception.h b/Exception.h index 8f4ba4f..ab9cfce 100644 --- a/Exception.h +++ b/Exception.h @@ -3,6 +3,8 @@ #include #include +#include "Node.h" + class Exception : public std::exception { public: Exception(const char * fmt, ...); @@ -17,11 +19,20 @@ protected: class ParserException : public Exception { public: + using Exception::Exception; ParserException(const char * fmt, ...); }; class CircuitException : public Exception { public: + using Exception::Exception; CircuitException(const char * fmt, ...); + Node * node = nullptr; +}; + +class NodeException : public CircuitException { +public: + using CircuitException::CircuitException; + NodeException(Node * node, const char * fmt, ...); }; diff --git a/Node.cpp b/Node.cpp index eb3428e..ad0a1ff 100644 --- a/Node.cpp +++ b/Node.cpp @@ -14,7 +14,7 @@ void Node::addInput(Net * net) { void Node::setOutput(Net * net){ if (this->output != nullptr) - throw CircuitException("net already assigned"); + throw NodeException(this, "net already assigned"); this->output = net; } @@ -22,11 +22,11 @@ void Node::setOutput(Net * net){ void Node::sim() { size_t input_size = this->inputs.size(); if (this->min_inputs >= 0 && input_size < min_inputs) - throw CircuitException("too few inputs"); + throw NodeException(this, "too few inputs (expected >= %d, got %d)", min_inputs, input_size); if (this->max_inputs >= 0 && input_size > max_inputs) - throw CircuitException("too many inputs"); + throw NodeException(this, "too many inputs (expected <= %d, got %d)", max_inputs, input_size); - // NodeOutput does not have an output itself + // output may be unconnected if (this->output == nullptr) return; SignalLevel new_out = this->level(); diff --git a/Node.h b/Node.h index 64c5b2b..9685141 100644 --- a/Node.h +++ b/Node.h @@ -33,7 +33,7 @@ protected: Net * output = nullptr; protected: - int min_inputs = 1; + int min_inputs = 2; // at least 2 inputs for most gates int max_inputs = -1; // unlimited public: diff --git a/NodeInput.cpp b/NodeInput.cpp index 591c2ca..e4ddbb9 100644 --- a/NodeInput.cpp +++ b/NodeInput.cpp @@ -1,12 +1,16 @@ #include "NodeInput.h" +#include "Exception.h" NodeInputLow NodeInputLow::instance(NodeInputLow::type); NodeInputHigh NodeInputHigh::instance(NodeInputHigh::type); -NodeInput::NodeInput() { - this->min_inputs = -1; +NodeInput::NodeInput() : Node() { + this->min_inputs = 0; this->max_inputs = 0; } +void NodeInput::addInput(Net *) { + throw NodeException(this, "NodeInput cannot have inputs"); +} NodeInputLow * NodeInputLow::clone() const { return new NodeInputLow(this); diff --git a/NodeInput.h b/NodeInput.h index 29ef091..ebacf4f 100644 --- a/NodeInput.h +++ b/NodeInput.h @@ -7,6 +7,8 @@ public: NodeInput(); ~NodeInput() = default; + virtual void addInput(Net *); + protected: using Node::Node; }; diff --git a/NodeOutput.cpp b/NodeOutput.cpp index 883868d..bd30afe 100644 --- a/NodeOutput.cpp +++ b/NodeOutput.cpp @@ -18,7 +18,7 @@ NodeOutput * NodeOutput::clone() const { } void NodeOutput::setOutput(Net *) { - throw CircuitException("NodeOutput cannot have an output"); + throw NodeException(this, "NodeOutput cannot have an output"); } SignalLevel NodeOutput::level() { diff --git a/readme.md b/readme.md index 6165d5b..f65826d 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,3 @@ -# design patterns - ## Building ``` @@ -17,35 +15,7 @@ make ## TODO -Functions: - -- [x] loop detection (JOSHUA) -- [x] final output format (LOEK) - -Schoonheid: - -- [ ] worden members van klassen in hun declaratie 'overschreven' (dit kan - niet, moet in een constructor o.i.d. gedaan worden) -- [ ] is de scope (public/private/protected) van alle members lekker +- is de scope (public/private/protected) van alle members lekker consistent? -- [x] stomme doxygen (`//! ` boven functies/variabelen, - `//!< wat doet dit` bij enum constantes) - -Classes: - -- [x] class Node -- [x] class CircuitFactory -- [x] class GateAnd -- [x] class GateNand -- [X] class GateNor -- [x] class GateNot -- [x] class GateOr -- [x] class GateXor -- [x] class Net -- [x] class NodeInput -- [x] class NodeOutput -- [x] class Parser -- [x] class Subject -- [x] enum SignalLevel -- [x] interface Observer +- klassendiagram -- cgit v1.2.3