Simplify working with values in interop
* Access primitive interop array elements and struct fields without `.value` * Simplify C*Var* interop types names
This commit is contained in:
committed by
SvyatoslavScherbina
parent
f94a47518a
commit
50c3be3fc2
@@ -5,5 +5,5 @@ fun main(args: Array<String>) {
|
||||
val statBuf = nativeHeap.alloc<statStruct>()
|
||||
val res = stat("/", statBuf.ptr)
|
||||
println(res)
|
||||
println(statBuf.st_uid.value)
|
||||
println(statBuf.st_uid)
|
||||
}
|
||||
@@ -4,17 +4,17 @@ fun main(args: Array<String>) {
|
||||
memScoped {
|
||||
val count = 5
|
||||
|
||||
val values = allocArray<CInt32Var>(count)
|
||||
values[0].value = 14
|
||||
values[1].value = 12
|
||||
values[2].value = 9
|
||||
values[3].value = 13
|
||||
values[4].value = 8
|
||||
val values = allocArray<IntVar>(count)
|
||||
values[0] = 14
|
||||
values[1] = 12
|
||||
values[2] = 9
|
||||
values[3] = 13
|
||||
values[4] = 8
|
||||
|
||||
cstdlib.qsort(values, count.toLong(), CInt32Var.size, staticCFunction(::comparator))
|
||||
cstdlib.qsort(values, count.toLong(), IntVar.size, staticCFunction(::comparator))
|
||||
|
||||
for (i in 0 .. count - 1) {
|
||||
print(values[i].value)
|
||||
print(values[i])
|
||||
print(" ")
|
||||
}
|
||||
println()
|
||||
@@ -22,8 +22,8 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
|
||||
private fun comparator(a: COpaquePointer?, b: COpaquePointer?): Int {
|
||||
val aValue = a!!.reinterpret<CInt32Var>().pointed.value
|
||||
val bValue = b!!.reinterpret<CInt32Var>().pointed.value
|
||||
val aValue = a!!.reinterpret<IntVar>()[0]
|
||||
val bValue = b!!.reinterpret<IntVar>()[0]
|
||||
|
||||
return (aValue - bValue)
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ fun main(args: Array<String>) {
|
||||
"a", "b".cstr, (-1).toByte(), 2.toShort(), 3, Long.MAX_VALUE, 0.1.toFloat(), 0.2)
|
||||
|
||||
memScoped {
|
||||
val aVar = alloc<CInt32Var>()
|
||||
val bVar = alloc<CInt32Var>()
|
||||
val aVar = alloc<IntVar>()
|
||||
val bVar = alloc<IntVar>()
|
||||
val sscanfResult = sscanf("42", "%d%d", aVar.ptr, bVar.ptr)
|
||||
printf("%d %d\n", sscanfResult, aVar.value)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ fun main(args: Array<String>) {
|
||||
memScoped {
|
||||
|
||||
val bufferLength = 100L
|
||||
val buffer = allocArray<CInt8Var>(bufferLength)
|
||||
val buffer = allocArray<ByteVar>(bufferLength)
|
||||
val serverAddr = alloc<sockaddr_in>()
|
||||
|
||||
val listenFd = socket(AF_INET, SOCK_STREAM, 0)
|
||||
@@ -20,9 +20,9 @@ fun main(args: Array<String>) {
|
||||
|
||||
with(serverAddr) {
|
||||
memset(this.ptr, 0, sockaddr_in.size)
|
||||
sin_family.value = AF_INET.narrow()
|
||||
sin_addr.s_addr.value = htons(0).toInt()
|
||||
sin_port.value = htons(port)
|
||||
sin_family = AF_INET.narrow()
|
||||
sin_addr.s_addr = htons(0).toInt()
|
||||
sin_port = htons(port)
|
||||
}
|
||||
|
||||
bind(listenFd, serverAddr.ptr.reinterpret(), sockaddr_in.size.toInt())
|
||||
|
||||
@@ -83,7 +83,7 @@ fun initialize() {
|
||||
fun main(args: Array<String>) {
|
||||
// initialize and run program
|
||||
memScoped {
|
||||
val argc = alloc<CInt32Var>().apply { value = 0 }
|
||||
val argc = alloc<IntVar>().apply { value = 0 }
|
||||
glutInit(argc.ptr, null) // TODO: pass real args
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user