-
Notifications
You must be signed in to change notification settings - Fork 0
/
Substitution.h
92 lines (73 loc) · 1.74 KB
/
Substitution.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef SUBSTITUTION_H
#define SUBSTITUTION_H
#include <iostream>
#include "picojson/picojson.h"
class Substitution
{
std::map<std::wstring, picojson::value> subs;
std::map<std::wstring, picojson::value> condSubs;
std::map<std::wstring, std::wstring> mappings;
std::map<std::wstring, std::wstring> symbolTypes;
class NoSubstitute {};
class NotDefined {};
public:
Substitution()
{
}
std::shared_ptr<Substitution> Clone() const
{
auto result = std::make_shared<Substitution>();
result->subs = subs;
result->condSubs = condSubs;
result->mappings = mappings;
result->symbolTypes = symbolTypes;
return result;
}
void Add(std::wstring str, picojson::value json, picojson::value jsonForCondition)
{
subs[str] = json;
condSubs[str] = jsonForCondition;
}
picojson::value Substitute(std::wstring str, bool forCondition) const
{
if (HasSubstitute(str))
{
if (forCondition)
{
return condSubs.find(str)->second;
}
return subs.find(str)->second;
}
throw NoSubstitute();
}
bool HasSubstitute(std::wstring str) const
{
return subs.find(str) != subs.end();
}
void AddMapping(std::wstring str)
{
mappings[str] = str;
}
bool HasMapping(std::wstring str) const
{
return mappings.find(str) != mappings.end();
}
void AddTypeMapping(std::wstring str, std::wstring type)
{
//std::wcerr << "typed " << str << " as " << type << std::endl;
symbolTypes[str] = type;
}
bool HasTypeMapping(std::wstring str) const
{
return symbolTypes.find(str) != symbolTypes.end();
}
std::wstring GetType(std::wstring str) const
{
if (HasTypeMapping(str))
{
return symbolTypes.find(str)->second;
}
throw NotDefined();
}
};
#endif // SUBSTITUTION_H