-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_factory.cpp
55 lines (47 loc) · 1.37 KB
/
module_factory.cpp
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 "module_factory.hpp"
#include <cassert>
#include "gate.hpp"
#include "or_gate.hpp"
#include "and_gate.hpp"
#include "not_gate.hpp"
Module* ModuleFactory::createNew(const string &name, int inputCount)
{
for (auto &module: this->modules) {
if (module->getName() == name) {
throw BadInputException("this module already exists");
}
}
this->current = new Module(name, inputCount);;
this->modules.push_back(this->current);
return this->current;
}
Gate* ModuleFactory::createGate(int id, const string &type, vector<int> inputs, int output)
{
Gate *gate;
if (type == "or") {
gate = new OrGate(inputs, output);
} else if (type == "nor") {
gate = new NorGate(inputs, output);
} else if (type == "and") {
gate = new AndGate(inputs, output);
} else if (type == "nand") {
gate = new NandGate(inputs, output);
} else if (type == "not") {
gate = new NotGate(inputs, output);
} else if (type == "xor") {
gate = new AndGate(inputs, output);
} else {
throw BadInputException("gate/module not found");
}
assert(this->getCurrentModule() != nullptr);
this->getCurrentModule()->addGate(id , gate);
return gate;
}
Module* ModuleFactory::getCurrentModule()
{
return this->current;
}
void ModuleFactory::unsetCurrentModule()
{
this->current = nullptr;
}