7
7
Field::Field (std::string name, std::shared_ptr<Type> type)
8
8
: TypeAndName(std::move(name), std::move(type)) {}
9
9
10
- std::string Field::generateSetter (int fieldIndex) {
11
- std::string setter = handleReservedWords (getName (), " _=" );
12
- std::string parameterType = type->str ();
13
- std::string value = " value" ;
14
- if (isAliasForType<ArrayType>(type.get ()) ||
15
- isAliasForType<Struct>(type.get ())) {
16
- parameterType = " native.Ptr[" + parameterType + " ]" ;
17
- value = " !" + value;
18
- }
19
- std::stringstream s;
20
- s << " def " << setter << " (value: " + parameterType + " ): Unit = !p._"
21
- << std::to_string (fieldIndex + 1 ) << " = " << value;
22
- return s.str ();
23
- }
24
-
25
- std::string Field::generateGetter (int fieldIndex) {
26
- std::string getter = handleReservedWords (getName ());
27
- std::string returnType = type->str ();
28
- std::string methodBody;
29
- if (isAliasForType<ArrayType>(type.get ()) ||
30
- isAliasForType<Struct>(type.get ())) {
31
- returnType = " native.Ptr[" + returnType + " ]" ;
32
- methodBody = " p._" + std::to_string (fieldIndex + 1 );
33
- } else {
34
- methodBody = " !p._" + std::to_string (fieldIndex + 1 );
35
- }
36
- std::stringstream s;
37
- s << " def " << getter << " : " << returnType << " = " << methodBody;
38
- return s.str ();
39
- }
40
-
41
- StructOrUnion::StructOrUnion (std::string name, std::vector<Field *> fields,
10
+ StructOrUnion::StructOrUnion (std::string name,
11
+ std::vector<std::shared_ptr<Field>> fields,
42
12
std::shared_ptr<Location> location)
43
13
: name(std::move(name)), fields(std::move(fields)),
44
14
location(std::move(location)) {}
45
15
46
16
std::string StructOrUnion::getName () const { return name; }
47
17
48
- StructOrUnion::~StructOrUnion () {
49
- for (const auto &field : fields) {
50
- delete field;
51
- }
52
- }
53
-
54
18
bool StructOrUnion::equals (const StructOrUnion &other) const {
55
19
if (this == &other) {
56
20
return true ;
@@ -77,8 +41,8 @@ std::shared_ptr<Location> StructOrUnion::getLocation() const {
77
41
return location;
78
42
}
79
43
80
- Struct::Struct (std::string name, std::vector<Field *> fields, uint64_t typeSize ,
81
- std::shared_ptr<Location> location)
44
+ Struct::Struct (std::string name, std::vector<std::shared_ptr< Field>> fields,
45
+ uint64_t typeSize, std::shared_ptr<Location> location)
82
46
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
83
47
typeSize(typeSize) {}
84
48
@@ -106,11 +70,11 @@ std::string Struct::generateHelperClass() const {
106
70
s << " implicit class " << type << " _ops(val p: native.Ptr[" << type
107
71
<< " ])"
108
72
<< " extends AnyVal {\n " ;
109
- int fieldIndex = 0 ;
73
+ unsigned fieldIndex = 0 ;
110
74
for (const auto &field : fields) {
111
75
if (!field->getName ().empty ()) {
112
- s << field-> generateGetter (fieldIndex) << " \n " ;
113
- s << field-> generateSetter (fieldIndex) << " \n " ;
76
+ s << generateGetter (fieldIndex) << " \n " ;
77
+ s << generateSetter (fieldIndex) << " \n " ;
114
78
}
115
79
fieldIndex++;
116
80
}
@@ -163,8 +127,41 @@ bool Struct::operator==(const Type &other) const {
163
127
return false ;
164
128
}
165
129
166
- Union::Union (std::string name, std::vector<Field *> fields, uint64_t maxSize,
167
- std::shared_ptr<Location> location)
130
+ std::string Struct::generateSetter (unsigned fieldIndex) const {
131
+ std::shared_ptr<Field> field = fields[fieldIndex];
132
+ std::string setter = handleReservedWords (field->getName (), " _=" );
133
+ std::string parameterType = field->getType ()->str ();
134
+ std::string value = " value" ;
135
+ if (isAliasForType<ArrayType>(field->getType ().get ()) ||
136
+ isAliasForType<Struct>(field->getType ().get ())) {
137
+ parameterType = " native.Ptr[" + parameterType + " ]" ;
138
+ value = " !" + value;
139
+ }
140
+ std::stringstream s;
141
+ s << " def " << setter << " (value: " + parameterType + " ): Unit = !p._"
142
+ << std::to_string (fieldIndex + 1 ) << " = " << value;
143
+ return s.str ();
144
+ }
145
+
146
+ std::string Struct::generateGetter (unsigned fieldIndex) const {
147
+ std::shared_ptr<Field> field = fields[fieldIndex];
148
+ std::string getter = handleReservedWords (field->getName ());
149
+ std::string returnType = field->getType ()->str ();
150
+ std::string methodBody;
151
+ if (isAliasForType<ArrayType>(field->getType ().get ()) ||
152
+ isAliasForType<Struct>(field->getType ().get ())) {
153
+ returnType = " native.Ptr[" + returnType + " ]" ;
154
+ methodBody = " p._" + std::to_string (fieldIndex + 1 );
155
+ } else {
156
+ methodBody = " !p._" + std::to_string (fieldIndex + 1 );
157
+ }
158
+ std::stringstream s;
159
+ s << " def " << getter << " : " << returnType << " = " << methodBody;
160
+ return s.str ();
161
+ }
162
+
163
+ Union::Union (std::string name, std::vector<std::shared_ptr<Field>> fields,
164
+ uint64_t maxSize, std::shared_ptr<Location> location)
168
165
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
169
166
ArrayType(std::make_shared<PrimitiveType>(" Byte" ), maxSize) {}
170
167
0 commit comments