godot.core.Constructors.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of godot-library-debug Show documentation
Show all versions of godot-library-debug Show documentation
Contains godot api as kotlin classes and jvm cpp interaction code.
@file:Suppress("UNCHECKED_CAST", "unused")
package godot.core
import godot.core.memory.MemoryManager
import godot.core.memory.TransferContext
import godot.tools.common.constants.Constraints
import godot.util.VoidPtr
import godot.util.threadLocal
abstract class KtConstructor(
vararg argsTypes: VariantConverter
) {
val parameterCount: Int = argsTypes.size
val parameterTypes: Array = argsTypes.toList().toTypedArray()
abstract operator fun invoke(): T
fun construct(rawPtr: VoidPtr, instanceId: Long) = MemoryManager.createScript(rawPtr, instanceId) {
TransferContext.readArguments(parameterTypes, paramsArray)
val instance = invoke()
resetParamsArray()
instance
}
companion object {
val paramsArray by threadLocal { arrayOfNulls(Constraints.MAX_CONSTRUCTOR_ARG_COUNT) }
fun resetParamsArray() {
paramsArray.fill(null)
}
}
}
class KtConstructor0(
private val constructor: () -> T
) : KtConstructor() {
override fun invoke(): T {
return constructor()
}
}
class KtConstructor1(
private val constructor: (P0) -> T,
p0Type: VariantConverter
) : KtConstructor(p0Type) {
override fun invoke(): T {
return constructor(
paramsArray[0] as P0
)
}
}
class KtConstructor2(
private val constructor: (P0, P1) -> T,
p0Type: VariantConverter,
p1Type: VariantConverter
) : KtConstructor(p0Type, p1Type) {
override fun invoke(): T {
return constructor(
paramsArray[0] as P0,
paramsArray[1] as P1
)
}
}
class KtConstructor3(
private val constructor: (P0, P1, P2) -> T,
p0Type: VariantConverter,
p1Type: VariantConverter,
p2Type: VariantConverter
) : KtConstructor(p0Type, p1Type, p2Type) {
override fun invoke(): T {
return constructor(
paramsArray[0] as P0,
paramsArray[1] as P1,
paramsArray[2] as P2
)
}
}
class KtConstructor4(
private val constructor: (P0, P1, P2, P3) -> T,
p0Type: VariantConverter,
p1Type: VariantConverter,
p2Type: VariantConverter,
p3Type: VariantConverter
) : KtConstructor(p0Type, p1Type, p2Type, p3Type) {
override fun invoke(): T {
return constructor(
paramsArray[0] as P0,
paramsArray[1] as P1,
paramsArray[2] as P2,
paramsArray[3] as P3
)
}
}
class KtConstructor5(
private val constructor: (P0, P1, P2, P3, P4) -> T,
p0Type: VariantConverter,
p1Type: VariantConverter,
p2Type: VariantConverter,
p3Type: VariantConverter,
p4Type: VariantConverter
) : KtConstructor(p0Type, p1Type, p2Type, p3Type, p4Type) {
override fun invoke(): T {
return constructor(
paramsArray[0] as P0,
paramsArray[1] as P1,
paramsArray[2] as P2,
paramsArray[3] as P3,
paramsArray[4] as P4
)
}
}
class KtConstructor6(
private val constructor: (P0, P1, P2, P3, P4, P5) -> T,
p0Type: VariantConverter,
p1Type: VariantConverter,
p2Type: VariantConverter,
p3Type: VariantConverter,
p4Type: VariantConverter,
p5Type: VariantConverter,
) : KtConstructor(p0Type, p1Type, p2Type, p3Type, p4Type, p5Type) {
override fun invoke(): T {
return constructor(
paramsArray[0] as P0,
paramsArray[1] as P1,
paramsArray[2] as P2,
paramsArray[3] as P3,
paramsArray[4] as P4,
paramsArray[5] as P5,
)
}
}
class KtConstructor7(
private val constructor: (P0, P1, P2, P3, P4, P5, P6) -> T,
p0Type: VariantConverter,
p1Type: VariantConverter,
p2Type: VariantConverter,
p3Type: VariantConverter,
p4Type: VariantConverter,
p5Type: VariantConverter,
p6Type: VariantConverter,
) : KtConstructor(p0Type, p1Type, p2Type, p3Type, p4Type, p5Type, p6Type) {
override fun invoke(): T {
return constructor(
paramsArray[0] as P0,
paramsArray[1] as P1,
paramsArray[2] as P2,
paramsArray[3] as P3,
paramsArray[4] as P4,
paramsArray[5] as P5,
paramsArray[6] as P6,
)
}
}
class KtConstructor8(
private val constructor: (P0, P1, P2, P3, P4, P5, P6, P7) -> T,
p0Type: VariantConverter,
p1Type: VariantConverter,
p2Type: VariantConverter,
p3Type: VariantConverter,
p4Type: VariantConverter,
p5Type: VariantConverter,
p6Type: VariantConverter,
p7Type: VariantConverter,
) : KtConstructor(p0Type, p1Type, p2Type, p3Type, p4Type, p5Type, p6Type, p7Type) {
override fun invoke(): T {
return constructor(
paramsArray[0] as P0,
paramsArray[1] as P1,
paramsArray[2] as P2,
paramsArray[3] as P3,
paramsArray[4] as P4,
paramsArray[5] as P5,
paramsArray[6] as P6,
paramsArray[7] as P7,
)
}
}