All Downloads are FREE. Search and download functionalities are using the official Maven repository.

godot.core.Constructors.kt Maven / Gradle / Ivy

There is a newer version: 0.10.0-4.3.0
Show newest version
@file:Suppress("UNCHECKED_CAST", "unused")

package godot.core

import godot.core.memory.TransferContext
import godot.tools.common.constants.Constraints
import godot.util.VoidPtr
import godot.util.threadLocal

abstract class KtConstructor(
    vararg argsTypes: Pair
) {
    val parameterCount: Int = argsTypes.size
    val parameterTypes: Array = argsTypes.map { it.first }.toTypedArray()
    val parameterNullables: Array = argsTypes.map { it.second }.toTypedArray()

    abstract operator fun invoke(): T

    fun construct(rawPtr: VoidPtr, instanceId: Long) = KtObject.instantiateWith(rawPtr, instanceId) {
        TransferContext.readArguments(parameterTypes, parameterNullables, 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: Pair
) : KtConstructor(p0Type) {
    override fun invoke(): T {
        return constructor(
            paramsArray[0] as P0
        )
    }
}

class KtConstructor2(
    private val constructor: (P0, P1) -> T,
    p0Type: Pair,
    p1Type: Pair
) : 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: Pair,
    p1Type: Pair,
    p2Type: Pair
) : 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: Pair,
    p1Type: Pair,
    p2Type: Pair,
    p3Type: Pair
) : 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: Pair,
    p1Type: Pair,
    p2Type: Pair,
    p3Type: Pair,
    p4Type: Pair
) : 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: Pair,
    p1Type: Pair,
    p2Type: Pair,
    p3Type: Pair,
    p4Type: Pair,
    p5Type: Pair,
) : 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: Pair,
    p1Type: Pair,
    p2Type: Pair,
    p3Type: Pair,
    p4Type: Pair,
    p5Type: Pair,
    p6Type: Pair,
) : 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: Pair,
    p1Type: Pair,
    p2Type: Pair,
    p3Type: Pair,
    p4Type: Pair,
    p5Type: Pair,
    p6Type: Pair,
    p7Type: Pair,
) : 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,
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy