aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-08 13:10:37 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-08 13:10:37 +0200
commit9413d81efb2279fc136deee874a8560dd4bb4a7e (patch)
tree68dae06d8ceb71831f4a4157c1fe7f31448f33d4
parent9e1b22e12bb0915081c94e6266b9725251500dcd (diff)
use factory map to look up node types
-rw-r--r--Node.cpp3
-rw-r--r--NodeFactory.cpp28
-rw-r--r--NodeFactory.h7
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 <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;
};