|
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 | 10 | StructOrUnion::StructOrUnion(std::string name,
|
42 | 11 | std::vector<std::shared_ptr<Field>> fields,
|
43 | 12 | std::shared_ptr<Location> location)
|
@@ -101,11 +70,11 @@ std::string Struct::generateHelperClass() const {
|
101 | 70 | s << " implicit class " << type << "_ops(val p: native.Ptr[" << type
|
102 | 71 | << "])"
|
103 | 72 | << " extends AnyVal {\n";
|
104 |
| - int fieldIndex = 0; |
| 73 | + unsigned fieldIndex = 0; |
105 | 74 | for (const auto &field : fields) {
|
106 | 75 | if (!field->getName().empty()) {
|
107 |
| - s << field->generateGetter(fieldIndex) << "\n"; |
108 |
| - s << field->generateSetter(fieldIndex) << "\n"; |
| 76 | + s << generateGetter(fieldIndex) << "\n"; |
| 77 | + s << generateSetter(fieldIndex) << "\n"; |
109 | 78 | }
|
110 | 79 | fieldIndex++;
|
111 | 80 | }
|
@@ -158,6 +127,39 @@ bool Struct::operator==(const Type &other) const {
|
158 | 127 | return false;
|
159 | 128 | }
|
160 | 129 |
|
| 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 | + |
161 | 163 | Union::Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
|
162 | 164 | uint64_t maxSize, std::shared_ptr<Location> location)
|
163 | 165 | : StructOrUnion(std::move(name), std::move(fields), std::move(location)),
|
|
0 commit comments