@@ -8,6 +8,19 @@ Field::Field(std::string name, std::string type)
8
8
StructOrUnion::StructOrUnion (std::string name, std::vector<Field> fields)
9
9
: name(std::move(name)), fields(std::move(fields)) {}
10
10
11
+ std::string StructOrUnion::getName () const {
12
+ return name;
13
+ }
14
+
15
+ bool StructOrUnion::usesType (const std::string &type) const {
16
+ for (const auto &field : fields) {
17
+ if (typeUsesOtherType (field.getType (), type)) {
18
+ return true ;
19
+ }
20
+ }
21
+ return false ;
22
+ }
23
+
11
24
Struct::Struct (std::string name, std::vector<Field> fields, uint64_t typeSize)
12
25
: StructOrUnion(std::move(name), std::move(fields)), typeSize(typeSize) {}
13
26
@@ -38,8 +51,8 @@ std::string Struct::generateHelperClass() const {
38
51
return " " ;
39
52
}
40
53
std::stringstream s;
41
- std::string newName = " struct_ " + name ;
42
- s << " implicit class " << newName << " _ops(val p: native.Ptr[struct_ " << name << " ])"
54
+ std::string type = getType () ;
55
+ s << " implicit class " << type << " _ops(val p: native.Ptr[" << type << " ])"
43
56
<< " extends AnyVal {\n " ;
44
57
int fieldIndex = 0 ;
45
58
for (const auto &field : fields) {
@@ -56,8 +69,8 @@ std::string Struct::generateHelperClass() const {
56
69
s << " }\n\n " ;
57
70
58
71
/* makes struct instantiation easier */
59
- s << " def " << newName + " ()(implicit z: native.Zone): native.Ptr[" + newName + " ]"
60
- << " = native.alloc[" + newName + " ]\n " ;
72
+ s << " def " << type + " ()(implicit z: native.Zone): native.Ptr[" + type + " ]"
73
+ << " = native.alloc[" + type + " ]\n " ;
61
74
62
75
return s.str ();
63
76
}
@@ -66,18 +79,23 @@ bool Struct::hasHelperMethods() const {
66
79
return !fields.empty () && fields.size () < SCALA_NATIVE_MAX_STRUCT_FIELDS;
67
80
}
68
81
82
+ std::string Struct::getType () const {
83
+ return " struct_" + name;
84
+ }
85
+
69
86
Union::Union (std::string name,
70
87
std::vector<Field> members, uint64_t maxSize)
71
88
: StructOrUnion(std::move(name), std::move(members)), maxSize(maxSize) {}
72
89
73
90
TypeDef Union::generateTypeDef () const {
74
- return TypeDef (" union_ " + name , " native.CArray[Byte, " + uint64ToScalaNat (maxSize) + " ]" );
91
+ return TypeDef (getType () , " native.CArray[Byte, " + uint64ToScalaNat (maxSize) + " ]" );
75
92
}
76
93
77
94
std::string Union::generateHelperClass () const {
78
95
std::stringstream s;
79
- s << " implicit class union_" << name << " _pos"
80
- << " (val p: native.Ptr[union_" << name << " ]) extends AnyVal {\n " ;
96
+ std::string type = getType ();
97
+ s << " implicit class " << type << " _pos"
98
+ << " (val p: native.Ptr[" << type << " ]) extends AnyVal {\n " ;
81
99
for (const auto &field : fields) {
82
100
if (!field.getName ().empty ()) {
83
101
std::string getter = handleReservedWords (field.getName ());
@@ -93,3 +111,7 @@ std::string Union::generateHelperClass() const {
93
111
s << " }\n " ;
94
112
return s.str ();
95
113
}
114
+
115
+ std::string Union::getType () const {
116
+ return " union_" + name;
117
+ }
0 commit comments