Skip to content

Commit b26ec93

Browse files
committed
Generate setters for fields of big structs
1 parent 56e9d9b commit b26ec93

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

bindgen/ir/Struct.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,30 @@ bool Struct::isRepresentedAsStruct() const {
199199

200200
std::string
201201
Struct::generateSetterForArrayRepresentation(unsigned int fieldIndex) const {
202-
return std::string();
202+
std::shared_ptr<Field> field = fields[fieldIndex];
203+
std::string setter = handleReservedWords(field->getName(), "_=");
204+
std::string parameterType;
205+
std::string value = "value";
206+
std::string castedField = "p._1";
207+
208+
PointerType pointerToFieldType = PointerType(field->getType());
209+
if (field->getOffset() > 0) {
210+
castedField = "(" + castedField + " + " +
211+
std::to_string(field->getOffset()) + ")";
212+
}
213+
castedField = "!" + castedField + ".cast[" + pointerToFieldType.str() + "]";
214+
if (isAliasForType<ArrayType>(field->getType().get()) ||
215+
isAliasForType<Struct>(field->getType().get())) {
216+
parameterType = pointerToFieldType.str();
217+
value = "!" + value;
218+
} else {
219+
parameterType = field->getType()->str();
220+
}
221+
std::stringstream s;
222+
s << " def " << setter
223+
<< "(value: " + parameterType + "): Unit = " << castedField << " = "
224+
<< value << "\n";
225+
return s.str();
203226
}
204227

205228
std::string
@@ -210,7 +233,7 @@ Struct::generateGetterForArrayRepresentation(unsigned fieldIndex) const {
210233
std::string methodBody;
211234

212235
PointerType pointerToFieldType = PointerType(field->getType());
213-
if (field->getOffset() != 0) {
236+
if (field->getOffset() > 0) {
214237
methodBody = "(p._1 + " + std::to_string(field->getOffset()) + ")";
215238
} else {
216239
methodBody = "p._1";

tests/samples/Struct.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,51 @@ object StructHelpers {
5353

5454
implicit class struct_bigStruct_ops(val p: native.Ptr[struct_bigStruct]) extends AnyVal {
5555
def one: native.CLong = !p._1.cast[native.Ptr[native.CLong]]
56+
def one_=(value: native.CLong): Unit = !p._1.cast[native.Ptr[native.CLong]] = value
5657
def two: native.CChar = !(p._1 + 8).cast[native.Ptr[native.CChar]]
58+
def two_=(value: native.CChar): Unit = !(p._1 + 8).cast[native.Ptr[native.CChar]] = value
5759
def three: native.CInt = !(p._1 + 12).cast[native.Ptr[native.CInt]]
60+
def three_=(value: native.CInt): Unit = !(p._1 + 12).cast[native.Ptr[native.CInt]] = value
5861
def four: native.CFloat = !(p._1 + 16).cast[native.Ptr[native.CFloat]]
62+
def four_=(value: native.CFloat): Unit = !(p._1 + 16).cast[native.Ptr[native.CFloat]] = value
5963
def five: native.CDouble = !(p._1 + 24).cast[native.Ptr[native.CDouble]]
64+
def five_=(value: native.CDouble): Unit = !(p._1 + 24).cast[native.Ptr[native.CDouble]] = value
6065
def six: native.Ptr[struct_point] = !(p._1 + 32).cast[native.Ptr[native.Ptr[struct_point]]]
66+
def six_=(value: native.Ptr[struct_point]): Unit = !(p._1 + 32).cast[native.Ptr[native.Ptr[struct_point]]] = value
6167
def seven: native.CInt = !(p._1 + 40).cast[native.Ptr[native.CInt]]
68+
def seven_=(value: native.CInt): Unit = !(p._1 + 40).cast[native.Ptr[native.CInt]] = value
6269
def eight: native.CInt = !(p._1 + 44).cast[native.Ptr[native.CInt]]
70+
def eight_=(value: native.CInt): Unit = !(p._1 + 44).cast[native.Ptr[native.CInt]] = value
6371
def nine: native.CInt = !(p._1 + 48).cast[native.Ptr[native.CInt]]
72+
def nine_=(value: native.CInt): Unit = !(p._1 + 48).cast[native.Ptr[native.CInt]] = value
6473
def ten: native.CInt = !(p._1 + 52).cast[native.Ptr[native.CInt]]
74+
def ten_=(value: native.CInt): Unit = !(p._1 + 52).cast[native.Ptr[native.CInt]] = value
6575
def eleven: native.CInt = !(p._1 + 56).cast[native.Ptr[native.CInt]]
76+
def eleven_=(value: native.CInt): Unit = !(p._1 + 56).cast[native.Ptr[native.CInt]] = value
6677
def twelve: native.CInt = !(p._1 + 60).cast[native.Ptr[native.CInt]]
78+
def twelve_=(value: native.CInt): Unit = !(p._1 + 60).cast[native.Ptr[native.CInt]] = value
6779
def thirteen: native.CInt = !(p._1 + 64).cast[native.Ptr[native.CInt]]
80+
def thirteen_=(value: native.CInt): Unit = !(p._1 + 64).cast[native.Ptr[native.CInt]] = value
6881
def fourteen: native.CInt = !(p._1 + 68).cast[native.Ptr[native.CInt]]
82+
def fourteen_=(value: native.CInt): Unit = !(p._1 + 68).cast[native.Ptr[native.CInt]] = value
6983
def fifteen: native.CInt = !(p._1 + 72).cast[native.Ptr[native.CInt]]
84+
def fifteen_=(value: native.CInt): Unit = !(p._1 + 72).cast[native.Ptr[native.CInt]] = value
7085
def sixteen: native.CInt = !(p._1 + 76).cast[native.Ptr[native.CInt]]
86+
def sixteen_=(value: native.CInt): Unit = !(p._1 + 76).cast[native.Ptr[native.CInt]] = value
7187
def seventeen: native.CInt = !(p._1 + 80).cast[native.Ptr[native.CInt]]
88+
def seventeen_=(value: native.CInt): Unit = !(p._1 + 80).cast[native.Ptr[native.CInt]] = value
7289
def eighteen: native.CInt = !(p._1 + 84).cast[native.Ptr[native.CInt]]
90+
def eighteen_=(value: native.CInt): Unit = !(p._1 + 84).cast[native.Ptr[native.CInt]] = value
7391
def nineteen: native.CInt = !(p._1 + 88).cast[native.Ptr[native.CInt]]
92+
def nineteen_=(value: native.CInt): Unit = !(p._1 + 88).cast[native.Ptr[native.CInt]] = value
7493
def twenty: native.CInt = !(p._1 + 92).cast[native.Ptr[native.CInt]]
94+
def twenty_=(value: native.CInt): Unit = !(p._1 + 92).cast[native.Ptr[native.CInt]] = value
7595
def twentyOne: native.CInt = !(p._1 + 96).cast[native.Ptr[native.CInt]]
96+
def twentyOne_=(value: native.CInt): Unit = !(p._1 + 96).cast[native.Ptr[native.CInt]] = value
7697
def twentyTwo: native.CInt = !(p._1 + 100).cast[native.Ptr[native.CInt]]
98+
def twentyTwo_=(value: native.CInt): Unit = !(p._1 + 100).cast[native.Ptr[native.CInt]] = value
7799
def twentyThree: native.CInt = !(p._1 + 104).cast[native.Ptr[native.CInt]]
100+
def twentyThree_=(value: native.CInt): Unit = !(p._1 + 104).cast[native.Ptr[native.CInt]] = value
78101
}
79102

80103
def struct_bigStruct()(implicit z: native.Zone): native.Ptr[struct_bigStruct] = native.alloc[struct_bigStruct]

0 commit comments

Comments
 (0)