Vlad Balan
03/18/2020, 2:39 PMObject pinning
.
According to the documentation this can be done as follows:
fun readData(fd: Int): String {
val buffer = ByteArray(1024)
buffer.usePinned { pinned ->
while (true) {
val length = recv(fd, pinned.addressOf(0), buffer.size.convert(), 0).toInt()
if (length <= 0) {
break
}
// Now `buffer` has raw data obtained from the `recv()` call.
}
}
}
What about if I want to use it on something else than a buffer. For example on cValue<cairo_text_extents_t>
where cairo_text_extents_t
is:
@kotlinx.cinterop.internal.CStruct public final class cairo_text_extents_t public constructor(rawPtr: kotlinx.cinterop.NativePtr /* = kotlin.native.internal.NativePtr */) : kotlinx.cinterop.CStructVar {
public companion object : kotlinx.cinterop.CStructVar.Type {
}
public final var height: kotlin.Double /* compiled code */
public final var width: kotlin.Double /* compiled code */
public final var x_advance: kotlin.Double /* compiled code */
public final var x_bearing: kotlin.Double /* compiled code */
public final var y_advance: kotlin.Double /* compiled code */
public final var y_bearing: kotlin.Double /* compiled code */
}
and the function to which I want to provide it is:
@kotlinx.cinterop.internal.CCall public external fun cairo_glyph_extents(cr: kotlinx.cinterop.CValuesRef<libcairo.cairo_t /* = cnames.structs._cairo */>?, glyphs: kotlinx.cinterop.CValuesRef<libcairo.cairo_glyph_t>?, num_glyphs: <http://kotlin.Int|kotlin.Int>, extents: kotlinx.cinterop.CValuesRef<libcairo.cairo_text_extents_t>?): kotlin.Unit { /* compiled code */ }
My problem is that cValue<cairo_text_extents_t>
does not have the function addressOf
.
I provide a small code snippet to show what I mean:
val glyph = cValue<cairo_glyph_t>()
val extents = cValue<cairo_text_extents_t>()
extents.usePinned {pinned ->
memScoped {
cairo_glyph_extents(context.cairo, glyph.ptr, 1, pinned.<what>) // how to get the address
}
}
Does anybody know how I can do this?
Thanks in advance!Dominaezzz
03/18/2020, 3:39 PMcValue
are considered immutable, so you won't be able to mutate it like that.Dominaezzz
03/18/2020, 3:41 PMcopy
function or using cValue<cairo_text_extents_t> { // mutate here }
to mutate it at initialisation time.Vlad Balan
03/19/2020, 5:44 PM