77Field::Field (std::string name, std::shared_ptr<Type> type)
88 : TypeAndName(std::move(name), std::move(type)) {}
99
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,
4212 std::shared_ptr<Location> location)
4313 : name(std::move(name)), fields(std::move(fields)),
4414 location(std::move(location)) {}
4515
4616std::string StructOrUnion::getName () const { return name; }
4717
48- StructOrUnion::~StructOrUnion () {
49- for (const auto &field : fields) {
50- delete field;
51- }
52- }
53-
5418bool StructOrUnion::equals (const StructOrUnion &other) const {
5519 if (this == &other) {
5620 return true ;
@@ -77,8 +41,8 @@ std::shared_ptr<Location> StructOrUnion::getLocation() const {
7741 return location;
7842}
7943
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)
8246 : StructOrUnion(std::move(name), std::move(fields), std::move(location)),
8347 typeSize(typeSize) {}
8448
@@ -106,11 +70,11 @@ std::string Struct::generateHelperClass() const {
10670 s << " implicit class " << type << " _ops(val p: native.Ptr[" << type
10771 << " ])"
10872 << " extends AnyVal {\n " ;
109- int fieldIndex = 0 ;
73+ unsigned fieldIndex = 0 ;
11074 for (const auto &field : fields) {
11175 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 " ;
11478 }
11579 fieldIndex++;
11680 }
@@ -163,8 +127,41 @@ bool Struct::operator==(const Type &other) const {
163127 return false ;
164128}
165129
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)
168165 : StructOrUnion(std::move(name), std::move(fields), std::move(location)),
169166 ArrayType(std::make_shared<PrimitiveType>(" Byte" ), maxSize) {}
170167
0 commit comments