From 208d568e3ebf464119c4486e85082190c1cef8ba Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 10 Apr 2017 13:51:39 +0300 Subject: [PATCH] Update INTEROP.md Describe working with `CValue`. --- INTEROP.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/INTEROP.md b/INTEROP.md index ac427035b8e..5634c5ee058 100644 --- a/INTEROP.md +++ b/INTEROP.md @@ -245,6 +245,23 @@ In all cases the C string is supposed to be encoded as UTF-8. When C function takes or returns a struct `T` by value, the corresponding argument type or return type is represented as `CValue`. +`CValue` is an opaque type, so structure fields cannot be accessed with +appropriate Kotlin properties. It could be acceptable, if API uses structures +as handles, but if field access is required, there are following conversion +methods available: + +* `fun T.readValue(): CValue`. Converts (the lvalue) `T` to `CValue`. + So to construct the `CValue`, `T` can be allocated, filled and then + converted to `CValue`. + +* `CValue.useContents(block: T.() -> R): R`. Temporarily places the + `CValue` to the memory, and then runs the passed lambda with this placed + value `T` as receiver. So to read a single field, the following code can be + used: + ``` + val fieldValue = structValue.useContents { field } + ``` + ### Callbacks ### To convert Kotlin function to pointer to C function,