diff options
-rw-r--r-- | Node.cpp | 3 | ||||
-rw-r--r-- | NodeFactory.cpp | 28 | ||||
-rw-r--r-- | NodeFactory.h | 7 |
3 files changed, 28 insertions, 10 deletions
@@ -1,9 +1,10 @@ #include "Node.h" +#include "NodeFactory.h" #include <iostream> Node::Node(const char * type) { - + NodeFactory::assign(type, this); } void Node::addInput(Net* net) { diff --git a/NodeFactory.cpp b/NodeFactory.cpp index da4cc72..4824734 100644 --- a/NodeFactory.cpp +++ b/NodeFactory.cpp @@ -11,14 +11,24 @@ bool NodeFactory::has_type(const char * type) { bool NodeFactory::has_type(string type) { std::ranges::transform(type, type.begin(), [] (unsigned char c) { return std::tolower(c); }); - // TODO: query the map instead - if (type == "and") return true; - if (type == "not") return true; - if (type == "or") return true; - if (type == "input_high") return true; - if (type == "input_low") return true; - if (type == "probe") return true; - - return false; + static NodeFactoryMap & map = get_map(); + + return map.find(type) != map.end(); +} + +void NodeFactory::assign(const char * _type, const Node * node) { + static NodeFactoryMap & map = get_map(); + string type = _type; + + std::ranges::transform(type, type.begin(), [] (unsigned char c) { return std::tolower(c); }); + + if (has_type(type)) return; + + map[type] = node; +} + +NodeFactoryMap & NodeFactory::get_map() { + static NodeFactoryMap map; + return map; } diff --git a/NodeFactory.h b/NodeFactory.h index b5757d4..7790b4a 100644 --- a/NodeFactory.h +++ b/NodeFactory.h @@ -1,11 +1,14 @@ #pragma once #include <string> +#include <map> #include "Node.h" using std::string; +using NodeFactoryMap = std::map<string, const Node *>; + class NodeFactory { public: NodeFactory() = default; @@ -16,6 +19,10 @@ public: static bool has_type(string type); private: + static void assign(const char * type, const Node * node); + static NodeFactoryMap & get_map(); + +private: friend Node; }; |