aboutsummaryrefslogtreecommitdiff
path: root/NodeFactory.cpp
blob: 6105c2d35186155760d42f5e0e766b69d2c1a2b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cassert>
#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);

	// ensure there is only one class that registers a type
	assert(!has_type(type));

	// 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();
}