aboutsummaryrefslogtreecommitdiff
path: root/NodeFactory.cpp
diff options
context:
space:
mode:
authorUnavailableDev <69792062+UnavailableDev@users.noreply.github.com>2024-06-12 12:05:45 +0200
committerUnavailableDev <69792062+UnavailableDev@users.noreply.github.com>2024-06-12 12:05:45 +0200
commit126a3c79516a6417181c3fe924084032d653b596 (patch)
tree1f1d356eccdf7d316cf7e991af036f0c0a6a53bd /NodeFactory.cpp
parentc084bee21f66e6322d4d55b8700f0779f2c58d0d (diff)
parent8e0a865dd375baa71357ce817847ea8a9144434c (diff)
Merge branch 'master' into node
Diffstat (limited to 'NodeFactory.cpp')
-rw-r--r--NodeFactory.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/NodeFactory.cpp b/NodeFactory.cpp
new file mode 100644
index 0000000..c9d4f20
--- /dev/null
+++ b/NodeFactory.cpp
@@ -0,0 +1,48 @@
+#include <locale>
+#include <ranges>
+#include <algorithm>
+
+#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) {
+ 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);
+
+ if (has_type(type)) return; // TODO: exception?
+
+ // printf("map[\"%s\"] = %p\n", type.c_str(), node);
+ map[type] = node;
+}
+
+NodeFactoryMap & NodeFactory::get_map() {
+ static NodeFactoryMap 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();
+}
+