-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathCodeGenerator.cpp
166 lines (135 loc) · 3 KB
/
CodeGenerator.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// CodeGenerator.cpp
//
// Copyright (c) 2020, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "CodeGenerator.h"
#include "Poco/StringTokenizer.h"
#include <set>
using namespace std::string_literals;
namespace Poco {
namespace ActiveRecord {
namespace Compiler {
CodeGenerator::CodeGenerator(const std::string& source, std::ostream& stream):
_source(source),
_stream(stream)
{
}
void CodeGenerator::writeBeginNameSpace(const std::string& nameSpace) const
{
if (!nameSpace.empty())
{
auto ns = splitNameSpace(nameSpace);
for (const auto& s: ns)
{
_stream << "namespace " << s << " {\n";
}
}
}
void CodeGenerator::writeEndNameSpace(const std::string& nameSpace) const
{
if (!nameSpace.empty())
{
auto ns = splitNameSpace(nameSpace);
for (const auto& s: ns)
{
_stream << "} ";
}
_stream << "// namespace " << nameSpace << "\n";
}
}
void CodeGenerator::writeHeaderComment(const std::string& fileName) const
{
_stream
<< "//\n"
<< "// " << fileName << "\n"
<< "//\n"
<< "// This file has been generated from " << _source << ". Do not edit.\n"
<< "//\n\n\n";
}
void CodeGenerator::writeInclude(const std::string& nameSpace, const std::string& name) const
{
_stream << "#include <";
auto ns = splitNameSpace(nameSpace);
for (const auto& s: ns)
{
_stream << s << '/';
}
_stream << name << ".h>\n";
}
std::vector<std::string> CodeGenerator::splitNameSpace(const std::string& nameSpace)
{
std::vector<std::string> result;
Poco::StringTokenizer tok(nameSpace, ":"s, Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY);
result.insert(result.end(), tok.begin(), tok.end());
return result;
}
std::string CodeGenerator::propertyType(const Property& prop) const
{
std::string type;
if (prop.nullable) type += "Poco::Nullable<";
type += prop.type;
if (prop.nullable) type += ">";
return type;
}
std::string CodeGenerator::paramType(const Property& prop) const
{
std::string type;
if (!prop.nullable && isSimpleType(prop.type))
{
type = propertyType(prop);
}
else
{
type += "const ";
type += propertyType(prop);
type += "&";
}
return type;
}
std::string CodeGenerator::keyType(const Class& clazz) const
{
for (const auto& p: clazz.properties)
{
if (p.name == clazz.key)
{
return propertyType(p);
}
}
return ""s;
}
bool CodeGenerator::isSimpleType(const std::string& type)
{
static const std::set<std::string> simpleTypes =
{
"bool"s,
"char"s,
"Poco::UInt8"s,
"Poco::Int8"s,
"Poco::UInt16"s,
"Poco::Int16"s,
"Poco::UInt32"s,
"Poco::Int32"s,
"Poco::UInt64"s,
"Poco::Int64"s,
"float"s,
"double"s
};
return simpleTypes.find(type) != simpleTypes.end();
}
std::string CodeGenerator::fullClassName(const Class& clazz) const
{
std::string fullName;
auto ns = splitNameSpace(clazz.nameSpace);
for (const auto& n: ns)
{
fullName += n;
fullName += "::";
}
fullName += clazz.name;
return fullName;
}
} } } // namespace Poco::ActiveRecord::Compiler