aboutsummaryrefslogtreecommitdiff
path: root/Circuit.cpp
blob: 22ea98c23802064995d828ce8e7957f637f74258 (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
51
52
53
54
55
#include "Circuit.h"
#include "NodeFactory.h"

void Circuit::create(string label, vector<string> nodes) {
	if (nodes.size() == 1 && NodeFactory::has_type(nodes[0]))
		return new_node(label, nodes[0]);

	new_net(label, nodes);
}

void Circuit::new_node(string label, string type) {
	if (nodes.find(label) != nodes.end()) return; // TODO: exception!

	Node * node = NodeFactory::create(type);
	if (node == nullptr) return; // TODO: exception?

	nodes[label] = node;

	// printf("[%s] (%s)\n", label.c_str(), type.c_str());
}

void Circuit::new_net(string src, vector<string> dests) {
	Net * net = new Net();
	nets.push_back(net);

	for (auto dest : dests) {
		Node * node = find_node(dest);
		if (node == nullptr) continue; // TODO: exception!
		node->addInput(net);
	}

	Node * node = find_node(src);
	if (node == nullptr) return; // TODO: exception!
	node->setOutput(net);
}

void Circuit::sim() {
	for (auto & node : nodes) {
		node.second->sim();
	}
}

Node * Circuit::find_node(string label) {
	auto map_index = this->nodes.find(label);
	if (map_index == nodes.end()) return nullptr;
	return map_index->second;
}

Circuit::~Circuit() {
	for (auto & n : nodes)
		delete n.second;
	for (auto & n : nets)
		delete n;
}