diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-08 14:10:50 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-08 14:10:50 +0200 |
commit | 2acd9a0097bb89e4b10ff77848d314e60f27be54 (patch) | |
tree | 70f83de0b7ed4da8bc31f5a5ba0c68249c1d8217 | |
parent | 9413d81efb2279fc136deee874a8560dd4bb4a7e (diff) |
instance Nodes using copy constructor in NodeFactory
-rw-r--r-- | Circuit.cpp | 16 | ||||
-rw-r--r-- | Circuit.h | 10 | ||||
-rw-r--r-- | GateAnd.cpp | 16 | ||||
-rw-r--r-- | GateAnd.h | 4 | ||||
-rw-r--r-- | Node.h | 7 | ||||
-rw-r--r-- | NodeFactory.cpp | 30 | ||||
-rw-r--r-- | NodeFactory.h | 3 |
7 files changed, 67 insertions, 19 deletions
diff --git a/Circuit.cpp b/Circuit.cpp index f6229f1..cf6312e 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -10,10 +10,26 @@ void Circuit::create(string label, vector<string> nodes) { } void Circuit::new_node(string label, string type) { + if (nodes.find(label) != nodes.end()) return; // TODO: exception! + + Node * node = NodeFactory::create(type); + if (node == nullptr) return; // TODO: exception? + + nodes[label] = node; + printf("[%s] (%s)\n", label.c_str(), type.c_str()); } void Circuit::new_net(string label, string connection) { + // TODO: instance Net + // TODO: connect Net to connection printf("[%s] -> %s\n", label.c_str(), connection.c_str()); } +Circuit::~Circuit() { + for (auto const & n : nodes) + delete n.second; + for (auto const & n : nets) + delete n.second; +} + @@ -2,6 +2,10 @@ #include <string> #include <vector> +#include <map> + +#include "Node.h" +#include "Net.h" using std::string; using std::vector; @@ -9,11 +13,15 @@ using std::vector; class Circuit { public: Circuit() = default; - virtual ~Circuit() = default; + virtual ~Circuit(); public: void create(string label, vector<string> nodes); void new_node(string label, string type); void new_net(string label, string node); + +private: + std::map<string, Node *> nodes = {}; + std::map<string, Net *> nets = {}; }; diff --git a/GateAnd.cpp b/GateAnd.cpp index e48b21c..dc65353 100644 --- a/GateAnd.cpp +++ b/GateAnd.cpp @@ -1,15 +1,8 @@ -#include <iostream> - #include "GateAnd.h" -using std::cout; -using std::endl; - GateAnd GateAnd::instance(GateAnd::type); -GateAnd::GateAnd(const char * type) : Node(type) { - cout << __PRETTY_FUNCTION__ << endl; -} +GateAnd::GateAnd(const char * type) : Node(type) { } // Concrete Nodes: void GateAnd::compare() { @@ -37,3 +30,10 @@ void GateAnd::compare() { // this->output->setLevel(new_out); // } } + +GateAnd::GateAnd(const GateAnd * prototype) : Node() { } + +GateAnd * GateAnd::clone() const { + return new GateAnd(this); +} + @@ -4,11 +4,13 @@ class GateAnd : public Node { public: - GateAnd(const char * type); + GateAnd(const GateAnd * prototype); virtual ~GateAnd() = default; virtual void compare(); + virtual GateAnd * clone() const; private: + GateAnd(const char * type); constexpr static const char * type = "and"; static GateAnd instance; }; @@ -11,14 +11,19 @@ using std::vector; class Node : Observer { public: - Node(const char * type); + Node() = default; virtual ~Node() = default; + virtual Node * clone() const = 0; + +public: void update(); virtual void addInput(Net *); virtual void setOutput(Net *); virtual void compare() = 0; protected: + Node(const char * type); + string label; vector<Net *> inputs; diff --git a/NodeFactory.cpp b/NodeFactory.cpp index 4824734..c9d4f20 100644 --- a/NodeFactory.cpp +++ b/NodeFactory.cpp @@ -4,26 +4,27 @@ #include "NodeFactory.h" +string NodeFactory::normalize_type(string type) { + std::ranges::transform(type, type.begin(), [] (unsigned char c) { return std::tolower(c); }); + return type; +} + bool NodeFactory::has_type(const char * type) { return has_type(string(type)); } bool NodeFactory::has_type(string type) { - std::ranges::transform(type, type.begin(), [] (unsigned char c) { return std::tolower(c); }); - - static NodeFactoryMap & map = get_map(); - - return map.find(type) != map.end(); + return find_type(type) != nullptr; } void NodeFactory::assign(const char * _type, const Node * node) { static NodeFactoryMap & map = get_map(); string type = _type; + type = normalize_type(type); - std::ranges::transform(type, type.begin(), [] (unsigned char c) { return std::tolower(c); }); - - if (has_type(type)) return; + if (has_type(type)) return; // TODO: exception? + // printf("map[\"%s\"] = %p\n", type.c_str(), node); map[type] = node; } @@ -32,3 +33,16 @@ NodeFactoryMap & NodeFactory::get_map() { return map; } +const Node * NodeFactory::find_type(string type) { + static NodeFactoryMap & map = get_map(); + type = normalize_type(type); + if (!map.contains(type)) return nullptr; + return map.find(type)->second; +} + +Node * NodeFactory::create(string type) { + const Node * prototype = find_type(type); + if (prototype == nullptr) return nullptr; + return prototype->clone(); +} + diff --git a/NodeFactory.h b/NodeFactory.h index 7790b4a..3c8c4f4 100644 --- a/NodeFactory.h +++ b/NodeFactory.h @@ -17,10 +17,13 @@ public: public: static bool has_type(const char * type); static bool has_type(string type); + static Node * create(string type); private: static void assign(const char * type, const Node * node); static NodeFactoryMap & get_map(); + static string normalize_type(string type); + static const Node * find_type(string type); private: friend Node; |