Created
December 20, 2016 07:33
-
-
Save ice1000/ba893cd9f08996bbffb023d7771bb901 to your computer and use it in GitHub Desktop.
Using Kotlin to generate JNI C++ codes for my own use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@file:JvmMultifileClass | |
@file:JvmName("CodeGen") | |
package org.algo4j.gen | |
import org.algo4j.test.print | |
/** | |
* Created by ice1000 on 2016/12/6. | |
* | |
* @author ice1000 | |
*/ | |
private data class Type( | |
val type: String, | |
val mark: String | |
) | |
private fun exe( | |
deep: List<Type>, | |
className: String, | |
methodName: String, | |
ret: String? = null, | |
retMark: String) { | |
deep.forEach { dark -> | |
val (type, mark) = dark | |
""" | |
JNIEXPORT auto JNICALL Java_org_algo4j_${className}_${methodName}___3${mark}I( | |
JNIEnv *env, | |
jclass, | |
${type}Array _data, | |
jint len) -> ${ret ?: type} { | |
__ice__() | |
} | |
""".print() | |
} | |
println() | |
println() | |
println() | |
println() | |
deep.forEach { dark -> | |
val (type, mark) = dark | |
""" | |
/** | |
* Class: org_algo4j_$className | |
* Method: $methodName | |
* Signature: ([${mark}I)${if (ret == null) mark else retMark} | |
*/ | |
JNIEXPORT auto JNICALL Java_org_algo4j_${className}_${methodName}___3${mark}I( | |
JNIEnv *, | |
jclass, | |
${type}Array, | |
jint | |
) -> ${ret ?: type}; | |
""".print() | |
} | |
} | |
fun main(args: Array<String>) { | |
val className = "util_Statistics" | |
val methodName = "stdDiv" | |
exe(listOf( | |
Type("jint", "I"), | |
Type("jlong", "J"), | |
Type("jfloat", "F"), | |
Type("jdouble", "D") | |
), className, methodName, "jdouble", "D") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment