protected fun constructFBInterfaceDefinition()

in code/4diac-integration/src/main/kotlin/org/fbme/ide/integration/fordiac/translator/cpp/AbstractTranslator.kt [231:348]


    protected fun constructFBInterfaceDefinition(): String {
        val sb = StringBuilder()

        val (eventInputWith, eventInputWithIndices) = calcEventPortWith(
            this.type().inputEvents,
            this.type().inputParameters.map { it.name }
        )

        val (eventOutputWith, eventOutputWithIndices) = calcEventPortWith(
            this.type().outputEvents,
            this.type().outputParameters.map { it.name }
        )

        val hasInputPorts = this.type().inputParameters.isNotEmpty()

        if (hasInputPorts) {
            sb.append("const CStringDictionary::TStringId ${this.constructFBClassName()}")
                .append("::scm_anDataInputNames[] = {")
                .append(this.constructFORTENameList(this.type().inputParameters))
                .appendLine("};")
                .append("const CStringDictionary::TStringId ${this.constructFBClassName()}")
                .append("::scm_anDataInputTypeIds[] = {")
                .append(this.constructFORTETypeList(this.type().inputParameters))
                .appendLine("};")
        }

        val hasOutputPorts = this.type().outputParameters.isNotEmpty()

        if (hasOutputPorts) {
            sb.append("const CStringDictionary::TStringId ${this.constructFBClassName()}")
                .append("::scm_anDataOutputNames[] = {")
                .append(this.constructFORTENameList(this.type().outputParameters))
                .appendLine("};")
                .append("const CStringDictionary::TStringId ${this.constructFBClassName()}")
                .append("::scm_anDataOutputTypeIds[] = {")
                .append(this.constructFORTETypeList(this.type().outputParameters))
                .appendLine("};")
        }

        val hasEventInputPorts = this.type().inputEvents.isNotEmpty()

        if (hasEventInputPorts) {
            if (eventInputWith.isNotEmpty()) {
                sb.append("const TDataIOID ")
                    .append(this.constructFBClassName())
                    .append("::scm_anEIWith[] = {")
                    .append(eventInputWith.joinToString(separator = ", "))
                    .appendLine("};")
            }
            sb.append("const TForteInt16 ")
                .append(this.constructFBClassName())
                .append("::scm_anEIWithIndexes[] = {")
                .append(eventInputWithIndices.joinToString(separator = ", "))
                .appendLine("};")
                .append("const CStringDictionary::TStringId ")
                .append(this.constructFBClassName())
                .append("::scm_anEventInputNames[] = {")
                .append(this.constructFORTENameList(this.type().inputEvents))
                .appendLine("};")
        }

        val hasEventOutputPorts = this.type().outputEvents.isNotEmpty()

        if (hasEventOutputPorts) {
            if (eventOutputWith.isNotEmpty()) {
                sb.append("const TDataIOID ")
                    .append(this.constructFBClassName())
                    .append("::scm_anEOWith[] = {")
                    .append(eventOutputWith.joinToString(separator = ", "))
                    .appendLine("};")
            }
            sb.append("const TForteInt16 ")
                .append(this.constructFBClassName())
                .append("::scm_anEOWithIndexes[] = {")
                .append(eventOutputWithIndices.joinToString(separator = ", "))
                .appendLine("};")
                .append("const CStringDictionary::TStringId ")
                .append(this.constructFBClassName())
                .append("::scm_anEventOutputNames[] = {")
                .append(this.constructFORTENameList(this.type().outputEvents))
                .appendLine("};")
        }

        val hasSocketPorts = this.type().templateTypeDescriptor.socketPorts.isNotEmpty()
        val hasPlugPorts = this.type().templateTypeDescriptor.plugPorts.isNotEmpty()

        if (hasSocketPorts || hasPlugPorts) {
            sb.append("const SAdapterInstanceDef ")
                .append(this.constructFBClassName())
                .appendLine("::scm_astAdapterInstances[] = {")
            val adapterPorts =
                this.type().templateTypeDescriptor.socketPorts + this.type().templateTypeDescriptor.plugPorts

            val ports = adapterPorts.map { port ->
                val s = StringBuilder()
                s.append("  {")

                if (port.declaration is SocketDeclaration) {
                    val socketTypeName = (port.declaration as SocketDeclaration).type.typeName
                    s.append(this.constructFORTEString(socketTypeName))
                } else {
                    val plugTypeName = (port.declaration as PlugDeclaration).type.typeName
                    s.append(this.constructFORTEString(plugTypeName))
                }

                s.append(", ")
                    .append(this.constructFORTEString(port.name))
                    .append(", ")
                    .append(!port.isInput) // todo: adapter is socket?
                    .append("}")
                s.toString()
            }
            sb.appendLine(ports.joinToString(",\n"))
                .appendLine("};")
        }

        return sb.toString()
    }