-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
OperationFactory.h
73 lines (55 loc) · 1.7 KB
/
OperationFactory.h
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include "Operation.h"
class FactoryPlant
{
protected:
virtual Operation * CreateInstanceSub(std::queue<std::wstring> & oArgList, const std::wstring & sCommand) = 0;
static std::map<std::wstring, FactoryPlant *> & GetCommands()
{
static std::map<std::wstring, FactoryPlant *> vCommands;
return vCommands;
}
public:
virtual ~FactoryPlant() = default;
static Operation * CreateInstance(std::queue<std::wstring> & oArgList)
{
// get the first element off the list
std::wstring sCommand = oArgList.front(); oArgList.pop();
// error if the string is least one character long
if (sCommand.empty()) return nullptr;
// error if the string does not start with "/" or "-"
if (sCommand.at(0) != '/' && sCommand.at(0) != '-')
{
wprintf(L"ERROR: Unrecognized parameter '%s'\n", sCommand.c_str());
std::exit(-1);
}
// convert to uppercase for map matching
ConvertToUpper(sCommand);
// remove the first character
sCommand.erase(0, 1);
// see if there's a class that matches this
const auto oCommand = GetCommands().find(sCommand);
// error if there is no matching command
if (oCommand == GetCommands().end())
{
wprintf(L"ERROR: Unrecognized parameter '%s'\n", sCommand.c_str());
std::exit(-1);
}
// create the the new class
return GetCommands()[sCommand]->CreateInstanceSub(oArgList, sCommand);
}
};
template <class SubType> class ClassFactory final : public FactoryPlant
{
private:
Operation * CreateInstanceSub(std::queue<std::wstring> & oArgList, const std::wstring & sCommand) override
{
return new SubType(oArgList, sCommand);
}
public:
ClassFactory(std::wstring sCommand)
{
ConvertToUpper(sCommand);
GetCommands()[sCommand] = this;
}
};