From 9413d81efb2279fc136deee874a8560dd4bb4a7e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 8 Jun 2024 13:10:37 +0200 Subject: use factory map to look up node types --- Node.cpp | 3 ++- NodeFactory.cpp | 28 +++++++++++++++++++--------- NodeFactory.h | 7 +++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Node.cpp b/Node.cpp index 5deaf03..a0fc27b 100644 --- a/Node.cpp +++ b/Node.cpp @@ -1,9 +1,10 @@ #include "Node.h" +#include "NodeFactory.h" #include 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 +#include #include "Node.h" using std::string; +using NodeFactoryMap = std::map; + class NodeFactory { public: NodeFactory() = default; @@ -15,6 +18,10 @@ public: static bool has_type(const char * type); static bool has_type(string type); +private: + static void assign(const char * type, const Node * node); + static NodeFactoryMap & get_map(); + private: friend Node; }; -- cgit v1.2.3