diff options
Diffstat (limited to 'NodeFactory.cpp')
-rw-r--r-- | NodeFactory.cpp | 30 |
1 files changed, 22 insertions, 8 deletions
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(); +} + |