所以,我正在研究一个涉及自动写入ByteBuffer
类扩展内容的概念Bufferizable
abstract class Bufferizable {
abstract val fieldOrder: Array<String>
open fun size(): Int = TODO()
var initialized = false
open infix fun to(address: Long) {
if (initialized) {
addFunctions.forEach { }
} else {
addFunctions = Array(fieldOrder.size) { null }
members = Array(fieldOrder.size) { null }
fieldOrder.forEachIndexed { i, field ->
val member = this::class.declaredMemberProperties.find { it.name == field }!!
addFunctions[i] = when (member.returnType) {
Mat4::class.defaultType.javaType -> WithAddress::addMat4
...
else -> throw Error()
} as BufferizableAddFunctionType
members[i] = member.get(this) // error
}
initialized = true
}
}
infix fun from(address: Long): Unit = TODO()
var addFunctions = arrayOf<BufferizableAddFunctionType?>()
var members = arrayOf<Any?>()
}
typealias BufferizableAddFunctionType = (Any) -> Unit
object WithAddress {
var address = NULL
var offset = 0
fun addMat4(mat4: Mat4) {
for (i in 0..3)
for (j in 0..3) {
memPutFloat(address + offset, mat4[i, j])
offset += Float.BYTES
}
}
...
}
这个想法是声明,例如,以下
object uboVS : Bufferizable() {
var projectionMatrix = Mat4()
var modelMatrix = Mat4()
...
override val fieldOrder = arrayOf("projectionMatrix", "modelMatrix", "viewMatrix")
}
然后每当uboVS to address
第一次调用时,它都会被初始化,然后按照指定的顺序将其内容写入本机地址。
但是我唯一遇到的问题是收集成员以稍后作为参数传递给第 i 个addFunction
因为编译器在这里抱怨:
members[i] = member.get(this)
错误:(328, 37) Kotlin: Out-projected type 'KProperty1' 禁止使用'public abstract fun get(receiver: T): R defined in kotlin.reflect.KProperty1'
我该如何解决?